// // playAudio.m // myhome // // Created by user on 12-11-20. // Copyright (c) 2012年 __MyCompanyName__. All rights reserved. // #import "playAudio.h" #import "AppDelegate.h" #define SAMPLE_RATE (8000) #define WEBRTC_AUDIO_BUF_SIZE (160*3*2) //30ms (10ms=160samples) @implementation playAudio { AppDelegate * app; } @synthesize Abuf; @synthesize Abuf_p; @synthesize Abuf_len; @synthesize Version; @synthesize vol; static int BYTES_PER_SAMPLES; static bool isAudioStop; - (id)init { Abuf_p = 0; Abuf_len = 0; Abuf = (Byte*)malloc(ABUF_NUM * 64); if ( self = [super init] ) { } return self; } /* max number of samples per frame (= 60 ms frame) */ //回调 每当一组音频数据读取完毕之后会自动触发回调方法,读取下一帧数据 void buffer_callback(void *inUserData, AudioQueueRef q, AudioQueueBufferRef buf) { //__bridge //NSLog(@"buffer_callback"); playAudio* player = (__bridge playAudio*)inUserData; [player audioQueueOutputWithQueue:q queueBuffer:buf]; } - (short)G711Decode_u_law_2_linear:(Byte)b { //u律 8126 short t; b = ~b; t = ((b&0xf)<<3)+0x84; t <<= ((unsigned)b&0x70)>>4; return ((b&0x80)?(0x84-t):(t-0x84)); } - (short)G711Decode_a_law_2_linear:(Byte)b { //a律 8300 int t; int seg; b ^= 0x55; t = (b&0xf)<<4; seg = ((unsigned)b&0x70)>>4;//seg=((unsigned)b&0x70)>>4; switch (seg){ case 0:{ t += 8; } break; case 1:{ t += 0x108; } break; default:{ t += 0x108; t <<= seg-1; } break; } return ((b&0x80)?t:-t); } - (void)audioQueueOutputWithQueue:(AudioQueueRef)q queueBuffer:(AudioQueueBufferRef)buf { //NSLog(@"audioQueueOutputWithQueue: _AudiodecodeType = %d", _AudiodecodeType); if (_AudiodecodeType == k711) { buf->mAudioDataByteSize = BYTES_PER_SAMPLES; short *b = buf->mAudioData; if (Abuf_len>0) { //NSLog(@"8130 audio play ..."); int pos = Abuf_p*64; for (int i=0; i