void WavePlayBack(uint32_t AudioFreq) { /* note: these mallocs are NOT freed at the moment */ sp_create(&sp); sp->sr = AudioFreq; sp_ftbl_create(sp, &ft, 8192); sp_gen_sine(sp, ft); sp_osc_create(&osc); sp_osc_init(sp, osc, ft, 0); osc->freq = 0.2f; osc->amp = 1.f; uint32_t i; for(i = 0; i < NOSCS; i++) { sp_fosc_create(&fosc[i]); sp_fosc_init(sp, fosc[i], ft); fosc[i]->freq = sp_midi2cps(scale[i]); fosc[i]->amp = 0.1f; } for(i = 0; i < MY_BUFSIZE; i++) { buf0[i] = 0; buf1[i] = 0; } sp_revsc_create(&revsc); sp_revsc_init(sp, revsc); /* Initialize wave player (Codec, DMA, I2C) */ WavePlayerInit(AudioFreq); nextbuf = 1; compute_buffer(buf1, MY_BUFSIZE); Audio_MAL_Play((uint32_t)buf0, MY_BUFSIZE / 2); EVAL_AUDIO_Mute(AUDIO_MUTE_ON); while(1) { while(nextbuf == 1); if(please_play == 0) { EVAL_AUDIO_Mute(AUDIO_MUTE_OFF); please_play = 1; } compute_buffer(buf0, MY_BUFSIZE); while(nextbuf == 0); compute_buffer(buf1, MY_BUFSIZE); }; }
/** * @brief MuteCtl * Mute or Unmute the audio current output * @param cmd: can be 0 to unmute, or 1 to mute. * @retval AUDIO_OK if all operations succeed, AUDIO_FAIL else. */ static uint8_t MuteCtl (uint8_t cmd) { /* Call low layer mute setting function */ if (EVAL_AUDIO_Mute(cmd) != 0) { AudioState = AUDIO_STATE_ERROR; return AUDIO_FAIL; } return AUDIO_OK; }
/** * @brief Main program. * @param None * @retval None */ int main(void) { /* Initialize LEDS */ /* Red Led On: buffer overflow */ STM_EVAL_LEDInit(LED3); /* Green Led On: fdmdv+codec2 enabled */ STM_EVAL_LEDInit(LED4); STM_EVAL_LEDInit(LED5); /* Blue Led On: start of application */ STM_EVAL_LEDInit(LED6); STM_EVAL_LEDOn(LED6); /* transparent mode switcher */ STM_EVAL_PBInit(BUTTON_USER, BUTTON_MODE_EXTI); if(Transparent_mode) STM_EVAL_LEDOff(LED4); else STM_EVAL_LEDOn(LED4); /* SysTick end of count event each 10ms */ RCC_GetClocksFreq(&RCC_Clocks); SysTick_Config(RCC_Clocks.HCLK_Frequency / 100); USART_InitTypeDef USART_InitStructure; USART_InitStructure.USART_BaudRate = 115200; USART_InitStructure.USART_WordLength = USART_WordLength_8b; USART_InitStructure.USART_StopBits = USART_StopBits_1; USART_InitStructure.USART_Parity = USART_Parity_No; USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; STM_EVAL_USART2Init(&USART_InitStructure); // turn off buffers, so IO occurs immediately setvbuf(stdin, NULL, _IONBF, 0); setvbuf(stdout, NULL, _IONBF, 0); setvbuf(stderr, NULL, _IONBF, 0); /* Output a message on Hyperterminal using printf function */ printf("\r\nFloating-Point Based Codec2 encoder for Cortex-M4F\r\n"); /* Configure TIM4 Peripheral to manage LEDs lighting */ unsigned int idx = 0; Time_Rec_Base=0; int i, buffId; codec2_initialize_all(SPEAKER_FREQ == 48000 ? 1 : 0); /* fill output fifo */ fifoBufferFullness=0; fifoBufferCurrent=0; Switch = 0; /* modulate silence once for padding if happens */ memset(padBuffer, 0x00, (320*(SPEAKER_FREQ/MIC_FREQ)*2)*sizeof(short)); memset(fifoBuffer, 0x00, MODULATOR_QUEUE_SIZE * (SPEAKER_FREQ/MIC_FREQ)*320*2*sizeof(short)); //codec2_modulate((short *) padBuffer, (short *) padBuffer, Transparent_mode); /* Initialize I2S interface */ EVAL_AUDIO_SetAudioInterface(AUDIO_INTERFACE_I2S); /* Initialize the Audio codec and all related peripherals (I2S, I2C, IOExpander, IOs...) */ //EVAL_AUDIO_Init(OUTPUT_DEVICE_AUTO, 0, SPEAKER_FREQ); EVAL_AUDIO_Init(OUTPUT_DEVICE_AUTO, 0, SPEAKER_FREQ); EVAL_AUDIO_PauseResume(AUDIO_PAUSE); Audio_MAL_Play((uint32_t) padBuffer, (320*(SPEAKER_FREQ/MIC_FREQ))*2*sizeof(short)); EVAL_AUDIO_PauseResume(AUDIO_RESUME); /* Start the record */ MicListenerInit(32000,16, 1); MicListenerStart(RecBuf_8Khz, PCM_OUT_SIZE); /* GLOBAL SCHEDULER * DO NOT USE LOOPS INSIDE IT! * */ while(1) { /* we have frame from mike */ if(Data_Status == 0) continue; /* Switch the buffers*/ if (Switch ==1) { pAudioRecBuf_8Khz = RecBuf_8Khz; writebuffer = RecBuf1_8Khz; Switch = 0; } else { pAudioRecBuf_8Khz = RecBuf1_8Khz; writebuffer = RecBuf_8Khz; Switch = 1; } #ifdef USE_ST_FILTER //Downsampling 16Khz => 8Khz (this is input for codec, it sampled with 8KHz) for(i=0; i<320; i++) writebuffer[i] = writebuffer[2*i]; #endif //TODO: modulate, even if no data from mike! if(fifoBufferFullness < MODULATOR_QUEUE_SIZE-1) { /* get the next free buffer */ buffId = fifoBufferCurrent + fifoBufferFullness; if(buffId >= MODULATOR_QUEUE_SIZE) buffId -= MODULATOR_QUEUE_SIZE; assert(buffId >= 0 && buffId < MODULATOR_QUEUE_SIZE); codec2_modulate((short *) writebuffer, (short *) fifoBuffer[buffId], Transparent_mode); fifoBufferFullness++; if(idx % 32 == 0) printf("."); if(idx % (32*32) == 0) printf("\r\n"); /* this is hack to remove loud noise at startup */ if(volume_set==1) { STM_EVAL_LEDOff(LED3); EVAL_AUDIO_VolumeCtl(90); volume_set=2; } } else { STM_EVAL_LEDToggle(LED3); printf("x"); if(idx % (32) == 0) printf("\r\n"); } idx++; Data_Status = 0; } EVAL_AUDIO_Mute(AUDIO_MUTE_ON); EVAL_AUDIO_Stop(CODEC_PDWN_HW); MicListenerStop(); //float samples_time = idx*samplesPerFrame/((float) WAVE_Format.NumChannels*WAVE_Format.SampleRate); #if 0 float samples_time = idx*mod->samplesPerFrame/((float) 1*MIC_FREQ); float cpu_time = Time_Rec_Base/((float ) 100); printf("\r\n%8.3f s audio file encoded in %8.3f s\r\n", (double) samples_time, (double) cpu_time); #endif while(1) { STM_EVAL_LEDToggle(LED5); Delay(10); } }