24bit 48kHz Audio Delay

Dependencies:   mbed-dsp mbed shimabara unzen_nucleo_f746

Fork of unzen_sample_nucleo_f746 by seiichi horie

雲仙フレームワークを使ったオーディオディレイのsampleです。

Committer:
GSMCustomEffects
Date:
Fri Dec 16 10:43:24 2016 +0000
Revision:
3:d9fd88ea0ff5
Parent:
1:5b735bc30c17
add delay program

Who changed what in which revision?

UserRevisionLine numberNew contents of line
shorie 0:a837eeab3ca6 1 #include "unzen.h" // audio framework include file
shorie 0:a837eeab3ca6 2 #include "umb_adau1361a.h" // audio codec contoler include file
shorie 0:a837eeab3ca6 3 #include "mbed.h"
GSMCustomEffects 1:5b735bc30c17 4 #include "arm_math.h"
shorie 0:a837eeab3ca6 5 #define CODEC_I2C_ADDR 0x38
GSMCustomEffects 3:d9fd88ea0ff5 6 #define DELAY_TIME (550) /* delay time in ms */
GSMCustomEffects 3:d9fd88ea0ff5 7 #define DELAY_BUF_SIZE (44100 * DELAY_TIME / 1000)/* ring buffer size */
GSMCustomEffects 3:d9fd88ea0ff5 8 #define FEEDBACK 0.5; /* feedback control */
shorie 0:a837eeab3ca6 9 DigitalOut myled1(LED1);
GSMCustomEffects 1:5b735bc30c17 10 DigitalIn User_b1(PC_13);
GSMCustomEffects 1:5b735bc30c17 11 Serial uart(SERIAL_TX,SERIAL_RX);
GSMCustomEffects 3:d9fd88ea0ff5 12 float32_t delay_buf[DELAY_BUF_SIZE];
GSMCustomEffects 3:d9fd88ea0ff5 13 unsigned int delay_buf_idx = 0;
GSMCustomEffects 3:d9fd88ea0ff5 14 float32_t l_lc[]={0};
GSMCustomEffects 3:d9fd88ea0ff5 15 float32_t l_x[]={0};
GSMCustomEffects 3:d9fd88ea0ff5 16 float32_t l_in[]={0};
GSMCustomEffects 3:d9fd88ea0ff5 17 float32_t l_delay[]={0};
GSMCustomEffects 1:5b735bc30c17 18 int flag = 1;
GSMCustomEffects 1:5b735bc30c17 19 char uart_read_value = 0;
shorie 0:a837eeab3ca6 20 // customer signal processing initialization call back.
shorie 0:a837eeab3ca6 21 void init_callback(
shorie 0:a837eeab3ca6 22 unsigned int block_size // block size [sample]
shorie 0:a837eeab3ca6 23 )
shorie 0:a837eeab3ca6 24 {
shorie 0:a837eeab3ca6 25 // place initialization code here
shorie 0:a837eeab3ca6 26 }
shorie 0:a837eeab3ca6 27
shorie 0:a837eeab3ca6 28
shorie 0:a837eeab3ca6 29 // customer signal processing call back.
shorie 0:a837eeab3ca6 30 void process_callback(
shorie 0:a837eeab3ca6 31 float rx_left_buffer[], // array of the left input samples
shorie 0:a837eeab3ca6 32 float rx_right_buffer[], // array of the right input samples
shorie 0:a837eeab3ca6 33 float tx_left_buffer[], // place to write the left output samples
shorie 0:a837eeab3ca6 34 float tx_right_buffer[], // place to write the left output samples
shorie 0:a837eeab3ca6 35 unsigned int block_size // block size [sample]
shorie 0:a837eeab3ca6 36 )
shorie 0:a837eeab3ca6 37 {
GSMCustomEffects 3:d9fd88ea0ff5 38
GSMCustomEffects 1:5b735bc30c17 39
GSMCustomEffects 1:5b735bc30c17 40 if(flag == 1){
shorie 0:a837eeab3ca6 41 // Sample processing
GSMCustomEffects 3:d9fd88ea0ff5 42 for ( int i=0; i<block_size; i++){
GSMCustomEffects 3:d9fd88ea0ff5 43 /*Bypass Sound*/
GSMCustomEffects 3:d9fd88ea0ff5 44 tx_left_buffer[i] = rx_left_buffer[i]; //copy from input to output
GSMCustomEffects 3:d9fd88ea0ff5 45 tx_right_buffer[i] = rx_left_buffer[i];//for guitar volume
GSMCustomEffects 1:5b735bc30c17 46
GSMCustomEffects 1:5b735bc30c17 47 }
GSMCustomEffects 1:5b735bc30c17 48 }
GSMCustomEffects 1:5b735bc30c17 49 else if(flag == 0){
GSMCustomEffects 3:d9fd88ea0ff5 50 for ( int i=0; i<block_size; i++){
GSMCustomEffects 3:d9fd88ea0ff5 51
GSMCustomEffects 3:d9fd88ea0ff5 52 /*Echo Effect*/
GSMCustomEffects 3:d9fd88ea0ff5 53 l_in[0] = rx_left_buffer[i]/2.0;//原音を保存
GSMCustomEffects 3:d9fd88ea0ff5 54 l_delay[0] = delay_buf[delay_buf_idx]*FEEDBACK;//フィードバック量の調整(FEEDBACK < 1をまもらないと発振する)
GSMCustomEffects 3:d9fd88ea0ff5 55 arm_add_f32( l_in, l_delay, l_x, 1 );//ディレイ音と原音を合成(l_xに計算結果が格納)
GSMCustomEffects 3:d9fd88ea0ff5 56 delay_buf[delay_buf_idx] = l_x[0];//フィードバック
GSMCustomEffects 3:d9fd88ea0ff5 57 delay_buf_idx = (int)(delay_buf_idx + 1)%DELAY_BUF_SIZE;//ring bufferの更新
GSMCustomEffects 3:d9fd88ea0ff5 58 tx_left_buffer[i] = l_x[0]*2.0; // copy from L to L
GSMCustomEffects 3:d9fd88ea0ff5 59 tx_right_buffer[i] = l_x[0]*2.0; // copy from L to R
GSMCustomEffects 1:5b735bc30c17 60 }
shorie 0:a837eeab3ca6 61 }
shorie 0:a837eeab3ca6 62 }
shorie 0:a837eeab3ca6 63
shorie 0:a837eeab3ca6 64
shorie 0:a837eeab3ca6 65
shorie 0:a837eeab3ca6 66 int main()
shorie 0:a837eeab3ca6 67 {
GSMCustomEffects 3:d9fd88ea0ff5 68 // I2C is essential to talk with ADAU1361
shorie 0:a837eeab3ca6 69 I2C i2c(D14, D15);
GSMCustomEffects 1:5b735bc30c17 70
GSMCustomEffects 1:5b735bc30c17 71
GSMCustomEffects 3:d9fd88ea0ff5 72 // create an audio codec contoler
GSMCustomEffects 3:d9fd88ea0ff5 73 //shimabara::UMB_ADAU1361A codec(shimabara::Fs_32, i2c, CODEC_I2C_ADDR ); // Default Fs is 48kHz
GSMCustomEffects 1:5b735bc30c17 74 shimabara::UMB_ADAU1361A codec(shimabara::Fs_441, i2c, CODEC_I2C_ADDR );
GSMCustomEffects 3:d9fd88ea0ff5 75 //shimabara::UMB_ADAU1361A codec(shimabara::Fs_48, i2c, CODEC_I2C_ADDR );
GSMCustomEffects 3:d9fd88ea0ff5 76 //shimabara::UMB_ADAU1361A codec(shimabara::Fs_96, i2c, CODEC_I2C_ADDR );
shorie 0:a837eeab3ca6 77
shorie 0:a837eeab3ca6 78 // create an audio framework by singlton pattern
shorie 0:a837eeab3ca6 79 unzen::Framework audio;
shorie 0:a837eeab3ca6 80
shorie 0:a837eeab3ca6 81 // Set I3C clock to 100kHz
shorie 0:a837eeab3ca6 82 i2c.frequency( 100000 );
shorie 0:a837eeab3ca6 83
shorie 0:a837eeab3ca6 84
shorie 0:a837eeab3ca6 85 // Configure the optional block size of signal processing. By default, it is 1[Sample]
GSMCustomEffects 3:d9fd88ea0ff5 86 //audio.set_block_size(16);
shorie 0:a837eeab3ca6 87
shorie 0:a837eeab3ca6 88
shorie 0:a837eeab3ca6 89 // Start the ADAU1361. Audio codec starts to generate the I2C signals
shorie 0:a837eeab3ca6 90 codec.start();
shorie 0:a837eeab3ca6 91
shorie 0:a837eeab3ca6 92 // Start the audio framework on ARM processor.
shorie 0:a837eeab3ca6 93 audio.start( init_callback, process_callback); // path the initializaiton and process call back to framework
shorie 0:a837eeab3ca6 94
shorie 0:a837eeab3ca6 95
shorie 0:a837eeab3ca6 96 // periodically changing gain for test
shorie 0:a837eeab3ca6 97 while(1)
shorie 0:a837eeab3ca6 98 {
GSMCustomEffects 1:5b735bc30c17 99 codec.set_hp_output_gain( 3, 3);
GSMCustomEffects 1:5b735bc30c17 100 codec.set_line_output_gain( 3, 3 );
GSMCustomEffects 3:d9fd88ea0ff5 101 if(User_b1 == 1){//スイッチ入力に対応
GSMCustomEffects 3:d9fd88ea0ff5 102 if(flag == 1){flag = 0;myled1 =1;uart.printf("Effect On \r\n");}
GSMCustomEffects 3:d9fd88ea0ff5 103 else if(flag == 0){flag = 1;myled1 =0;uart.printf("Effect Off \r\n");}
GSMCustomEffects 3:d9fd88ea0ff5 104 while(User_b1 == 1){}
GSMCustomEffects 3:d9fd88ea0ff5 105 }
GSMCustomEffects 3:d9fd88ea0ff5 106 if(uart.readable()== 1){uart_read_value = uart.getc();//Uart Output
GSMCustomEffects 3:d9fd88ea0ff5 107 if(uart_read_value == 'e'){flag = 1;myled1 =0;uart.printf("Effect Off \r\n");}
GSMCustomEffects 3:d9fd88ea0ff5 108 if(uart_read_value == 'd'){flag = 0;myled1 =1;uart.printf("Effect On \r\n");}
GSMCustomEffects 1:5b735bc30c17 109 }
shorie 0:a837eeab3ca6 110 }
shorie 0:a837eeab3ca6 111 }