void PlayBuffer(short *PCMData, int size) { AUDIOPLAY_STATE_T *st; int32_t ret; int phase = 0; int inc = 256<<16; int dinc = 0; int buffer_bytes = (BUFFER_SIZE_SAMPLES * bitdepth * OUT_CHANNELS(nchannels))>>3; ret = audioplay_create(&st, samplerate, nchannels, bitdepth, bufferCount, buffer_bytes); ret = audioplay_set_dest(st, audio_dest[dest]); int sectorNumber= (size-1)/BUFFER_SIZE_SAMPLES+1; int i; for (i=0;i<sectorNumber;i++) { uint8_t *buf; int16_t *p; uint32_t latency; while((buf = audioplay_get_buffer(st)) == NULL) usleep(CTTW_SLEEP_TIME*1000); p = (int16_t *) buf; if ((i+1)*BUFFER_SIZE_SAMPLES<=size) { memcpy(p,PCMData+i*BUFFER_SIZE_SAMPLES,sizeof(short)*BUFFER_SIZE_SAMPLES); } else { memcpy(p,PCMData+i*BUFFER_SIZE_SAMPLES,sizeof(short)*(size-i*BUFFER_SIZE_SAMPLES)); memset(p+size-i*BUFFER_SIZE_SAMPLES,0,sizeof(short)*((i+1)*BUFFER_SIZE_SAMPLES-size)); } // try and wait for a minimum latency time (in ms) before // sending the next packet while((latency = audioplay_get_latency(st)) > (samplerate * (MIN_LATENCY_TIME + CTTW_SLEEP_TIME) / 1000)) usleep(CTTW_SLEEP_TIME*1000); //if (i==m_CurPlayStartSector) usleep(CTTW_SLEEP_TIME*1000); ret = audioplay_play_buffer(st, buf, buffer_bytes); } audioplay_delete(st); }
/* ------------------------------------------------- / sx_audio_sink_init() / Init audio decoder & render & other hardware component /---------------------------------------------------*/ void sx_audio_sink_init(void){ int rv; /* Init Broadcom host */ bcm_host_init(); /* Create audio decoder & render & other hardware component */ rv = audioplay_create(&st, AUDIO_CODEC_SAMPLE_RATE, AUDIO_CODEC_NUM_CHANNEL, AUDIO_CODEC_BIT_DEPTH, AUDIO_NUM_BUF, AUDIO_BUFFER_SIZE); assert(rv == 0); /* Set audio render destination */ rv = audioplay_set_dest(st, AUDIO_RENDER); assert(rv == 0); printf("(sx_audio_sink_init): Initialization completed...\n"); }