static void AudioCallback(void *context, int buffer) { int8_t components[3]; int32_t dx; static uint32_t phase = 0; uint32_t phasediff; int16_t *samples = GET_AUDIO_BUFFER(buffer); ReadRawAccelerometerData(components); dx = (components[0]-zero[0]) + 0x3f; phasediff = dx * 0x9; for(int i = 0; i < 128; i++) { phase += phasediff; if (!(phase & 0x8000)) samples[2*i+0] = samples[2*i+1] = 32767; else samples[2*i+0] = samples[2*i+1] = -32768; } ProvideAudioBuffer(samples, 256); }
/* * Called by the audio driver when it is time to provide data to * one of the audio buffers (while the other buffer is sent to the * CODEC using DMA). One mp3 frame is decoded at a time and * provided to the audio driver. */ static void AudioCallback(void *context, int buffer) { static int16_t audio_buffer0[4096]; static int16_t audio_buffer1[4096]; int i=0; int offset, err; int outOfData = 0; int16_t *samples; if (buffer) { samples = audio_buffer0; GPIO_SetBits(GPIOD, GPIO_Pin_13); GPIO_ResetBits(GPIOD, GPIO_Pin_14); } else { samples = audio_buffer1; GPIO_SetBits(GPIOD, GPIO_Pin_14); GPIO_ResetBits(GPIOD, GPIO_Pin_13); } offset = MP3FindSyncWord((unsigned char*)read_ptr, bytes_left); bytes_left -= offset; read_ptr += offset; err = MP3Decode(hMP3Decoder, (unsigned char**)&read_ptr, (int*)&bytes_left, samples, 0); if (err) { /* error occurred */ switch (err) { case ERR_MP3_INDATA_UNDERFLOW: outOfData = 1; break; case ERR_MP3_MAINDATA_UNDERFLOW: /* do nothing - next call to decode will provide more mainData */ break; case ERR_MP3_FREE_BITRATE_SYNC: default: outOfData = 1; break; } } else { // no error MP3GetLastFrameInfo(hMP3Decoder, &mp3FrameInfo); // Duplicate data in case of mono to maintain playback speed if (mp3FrameInfo.nChans == 1) { for(i = mp3FrameInfo.outputSamps;i >= 0;i--) { samples[2 * i]=samples[i]; samples[2 * i + 1]=samples[i]; } mp3FrameInfo.outputSamps *= 2; } } if (!outOfData) { ProvideAudioBuffer(samples, mp3FrameInfo.outputSamps); } }
/* * Called by the audio driver when it is time to provide data to * one of the audio buffers (while the other buffer is sent to the * CODEC using DMA). One mp3 frame is decoded at a time and * provided to the audio driver. */ static void AudioCallback(void *context, int buffer) { static int16_t audio_buffer0[4096]; static int16_t audio_buffer1[4096]; int offset, err; int outOfData = 0; static const char *read_ptr = mp3_data; static int bytes_left = MP3_SIZE; int16_t *samples; if (buffer) { samples = audio_buffer0; } else { samples = audio_buffer1; } offset = MP3FindSyncWord((unsigned char*)read_ptr, bytes_left); bytes_left -= offset; if (bytes_left <= 10000) { read_ptr = mp3_data; bytes_left = MP3_SIZE; offset = MP3FindSyncWord((unsigned char*)read_ptr, bytes_left); } read_ptr += offset; err = MP3Decode(hMP3Decoder, (unsigned char**)&read_ptr, &bytes_left, samples, 0); if (err) { /* error occurred */ switch (err) { case ERR_MP3_INDATA_UNDERFLOW: outOfData = 1; break; case ERR_MP3_MAINDATA_UNDERFLOW: /* do nothing - next call to decode will provide more mainData */ break; case ERR_MP3_FREE_BITRATE_SYNC: default: outOfData = 1; break; } } else { /* no error */ MP3GetLastFrameInfo(hMP3Decoder, &mp3FrameInfo); } if (!outOfData) { ProvideAudioBuffer(samples, mp3FrameInfo.outputSamps); } }
static void AudioCallback(void *context, int buffer) { static int16_t audio_buffer0[4096]; static int16_t audio_buffer1[4096]; int offset; int outOfData = 0; static int bytes_left = MP3_SIZE; int16_t *samples; if (buffer) { samples = audio_buffer0; GPIO_SetBits(GPIOD, GPIO_Pin_13); GPIO_ResetBits(GPIOD, GPIO_Pin_14); } else { samples = audio_buffer1; GPIO_SetBits(GPIOD, GPIO_Pin_14); GPIO_ResetBits(GPIOD, GPIO_Pin_13); } offset = MP3FindSyncWord((unsigned char*)read_ptr, bytes_left); if (offset != -1) { bytes_left -= offset; } /* else { bytes_left = 0; //end of buffer, toggle buffers }*/ //Played the entire buffer, loop back to play from the front of the buffer if (bytes_left <= 1000) { flipBuffers(); bytes_left = MP3_SIZE; offset = MP3FindSyncWord((unsigned char*)read_ptr, bytes_left); } if(offset < MP3_SIZE && offset >= 0) { read_ptr += offset; } //if (*read_ptr == 0xFF) //{ err = MP3Decode(hMP3Decoder, (unsigned char**)&read_ptr, &bytes_left, samples, 0); //} if (err && (err != -9)) { // error occurred switch (err) { case ERR_MP3_INDATA_UNDERFLOW: outOfData = 1; break; case ERR_MP3_MAINDATA_UNDERFLOW: MP3GetLastFrameInfo(hMP3Decoder, &mp3FrameInfo); //do nothing - next call to decode will provide more mainData break; case (-6): bytes_left -= 5; read_ptr += 5; //MP3GetLastFrameInfo(hMP3Decoder, &mp3FrameInfo); break; case (-8): break; case ERR_MP3_FREE_BITRATE_SYNC: break; default: outOfData = 1; bytes_left -= 5; read_ptr += 5; break; } /* StopAudio(); rxIndex = 0; dataRxComplete = 0; bytes_left = MP3_SIZE; flipBuffers(); PlayAudioWithCallback(AudioCallback, 0);*/ } else { // no error MP3GetLastFrameInfo(hMP3Decoder, &mp3FrameInfo); } //MP3GetLastFrameInfo(hMP3Decoder, &mp3FrameInfo); if (mp3FrameInfo.outputSamps > 0) { ProvideAudioBuffer(samples, mp3FrameInfo.outputSamps); } else { ProvideAudioBuffer(samples, 4096); } }