Nuvoton / Mbed OS NuMaker-mbed-AudioPlayback-example
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 
00003 #if defined(TARGET_NUMAKER_PFM_NUC472) || defined(TARGET_NUMAKER_PFM_M487) || defined(TARGET_NUMAKER_IOT_M487)
00004 #include "FATFileSystem.h"
00005 #include "NuSDBlockDevice.h"
00006 #include <stdio.h>
00007 #include <errno.h>
00008 
00009 NuSDBlockDevice bd(MBED_CONF_APP_SD_DAT0, MBED_CONF_APP_SD_DAT1, MBED_CONF_APP_SD_DAT2, MBED_CONF_APP_SD_DAT3,  // SD DAT0-3
00010     MBED_CONF_APP_SD_CMD, MBED_CONF_APP_SD_CLK, MBED_CONF_APP_SD_CD);                                           // SD CMD/CLK/CD
00011 FATFileSystem fs("fs");
00012 #endif
00013 
00014 #if defined(TARGET_NUMAKER_PFM_NUC472)
00015 #include "NAU8822L.h"
00016 
00017 NAU8822L audio(MBED_CONF_APP_I2C_SDA, MBED_CONF_APP_I2C_SCL, MBED_CONF_APP_I2C_ADDR, MBED_CONF_APP_I2S_DO,
00018     MBED_CONF_APP_I2S_DI, MBED_CONF_APP_I2S_BCLK, MBED_CONF_APP_I2S_MCLK, MBED_CONF_APP_I2S_LRCK); // NAU8822L object
00019 DigitalOut hp_enable(PH_2);
00020 #elif defined(TARGET_NUMAKER_PFM_M453)
00021 #include "NAU8822L.h"
00022 
00023 NAU8822L audio(MBED_CONF_APP_I2C_SDA, MBED_CONF_APP_I2C_SCL, MBED_CONF_APP_I2C_ADDR, MBED_CONF_APP_I2S_DO,
00024     MBED_CONF_APP_I2S_DI, MBED_CONF_APP_I2S_BCLK, MBED_CONF_APP_I2S_MCLK, MBED_CONF_APP_I2S_LRCK); // NAU8822L object
00025 DigitalOut hp_enable(PE_1);
00026 #elif defined(TARGET_NUMAKER_PFM_M487)
00027 #include "NAU88L25.h"
00028 
00029 NAU88L25 audio(MBED_CONF_APP_I2C_SDA, MBED_CONF_APP_I2C_SCL, MBED_CONF_APP_I2C_ADDR, MBED_CONF_APP_I2S_DO,
00030     MBED_CONF_APP_I2S_DI, MBED_CONF_APP_I2S_BCLK, MBED_CONF_APP_I2S_MCLK, MBED_CONF_APP_I2S_LRCK); // NAU88L25 object
00031 DigitalOut hp_enable(PE_13);
00032 #elif defined(TARGET_NUMAKER_PFM_NANO130)
00033 #include "NAU88L25.h"
00034 
00035 NAU88L25 audio(MBED_CONF_APP_I2C_SDA, MBED_CONF_APP_I2C_SCL, MBED_CONF_APP_I2C_ADDR, MBED_CONF_APP_I2S_DO,
00036     MBED_CONF_APP_I2S_DI, MBED_CONF_APP_I2S_BCLK, MBED_CONF_APP_I2S_MCLK, MBED_CONF_APP_I2S_LRCK); // NAU88L25 object
00037 DigitalOut hp_enable(LED2); //dummy function
00038 #elif defined(TARGET_NUMAKER_IOT_M487)
00039 #include "NAU88L25.h"
00040 
00041 NAU88L25 audio(MBED_CONF_APP_I2C_SDA, MBED_CONF_APP_I2C_SCL, MBED_CONF_APP_I2C_ADDR, MBED_CONF_APP_I2S_DO,
00042     MBED_CONF_APP_I2S_DI, MBED_CONF_APP_I2S_BCLK, MBED_CONF_APP_I2S_MCLK, MBED_CONF_APP_I2S_LRCK); // NAU88L25 object
00043 DigitalOut hp_enable(PE_13);
00044 #endif
00045 
00046 InterruptIn button(SW2);    // button SW2
00047 DigitalOut led(LED1);       // flashing LED1(rgbled1)
00048 
00049 #if defined(TARGET_NUMAKER_PFM_NANO130)
00050 int audioBuf[128];
00051 #else
00052 int audioBuf[4096];
00053 #endif
00054 
00055 int readPtr = 0;
00056 int writePtr = 0;
00057 
00058 int samplingRate = 8000;
00059 char channelCount = 2;
00060 char sampleBitLength = 16;
00061 
00062 #if defined(TARGET_NUMAKER_PFM_NUC472) || defined(TARGET_NUMAKER_PFM_M487) || defined(TARGET_NUMAKER_IOT_M487)
00063 FILE *fd;
00064 
00065 int theta = 0;
00066 
00067 void return_error(int ret_val) {
00068     if (ret_val)
00069         printf("Failure. %d\r\n", ret_val);
00070     else
00071         printf("done.\r\n");
00072 }
00073 
00074 void errno_error(void* ret_val) {
00075     if (ret_val == NULL)
00076         printf(" Failure. %d \r\n", errno);
00077     else
00078         printf(" done.\r\n");
00079 }
00080 
00081 #elif defined(TARGET_NUMAKER_PFM_M453) || defined(TARGET_NUMAKER_PFM_NANO130)
00082 // 1k sine wave@sampling rate 8kHz stereo 16-bit
00083 const char sine1k[] = {
00084     0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x08, 0x50, 0x0B, 0x50, 0x0B, 0x00, 0x08, 0x00, 0x08,
00085     0x00, 0x00, 0x00, 0x00, 0x00, 0xF8, 0x00, 0xF8, 0xB0, 0xF4, 0xB0, 0xF4, 0x00, 0xF8, 0x00, 0xF8
00086 };
00087 
00088 int flag = 0;
00089 #endif
00090 
00091 void flip(void) {
00092     led = !led;
00093 }
00094 
00095 void play(void) {
00096 #if defined(TARGET_NUMAKER_PFM_NUC472)
00097     audio.write(audioBuf, readPtr, 4);
00098     readPtr += 4;
00099     readPtr &= 0xFFF;
00100     theta -= 4;
00101 #elif defined(TARGET_NUMAKER_PFM_M453)
00102     if (flag == 0) {
00103         audio.write((int *)sine1k, readPtr, 1);
00104         readPtr += 1;
00105         readPtr &= 0x7; // sine1k / 4 - 1
00106     } else {
00107         audio.write(audioBuf, readPtr, 1);
00108         readPtr += 1;
00109         readPtr &= 0xFFF;
00110     }
00111 #elif defined(TARGET_NUMAKER_PFM_M487)
00112     audio.write(audioBuf, readPtr, 4);
00113     readPtr += 4;
00114     readPtr &= 0xFFF;
00115     theta -= 4;
00116 #elif defined(TARGET_NUMAKER_PFM_NANO130)
00117     audio.write((int *)audioBuf, readPtr, 1);
00118     readPtr += 1;
00119     readPtr &= 0x7; // sine1k / 4 - 1
00120 #elif defined(TARGET_NUMAKER_IOT_M487)
00121     audio.write(audioBuf, readPtr, 4);
00122     readPtr += 4;
00123     readPtr &= 0xFFF;
00124     theta -= 4;
00125 #endif
00126 }
00127 
00128 void record(void) {
00129     audio.read();
00130     
00131 #if defined(TARGET_NUMAKER_PFM_NUC472)
00132     audioBuf[writePtr] = audio.rxBuffer[0];
00133     audioBuf[++writePtr] = audio.rxBuffer[1];
00134     audioBuf[++writePtr] = audio.rxBuffer[2];
00135     audioBuf[++writePtr] = audio.rxBuffer[3];
00136     ++writePtr;
00137     theta += 4;
00138     if (writePtr > 4094) {
00139         writePtr = 0;
00140     }
00141 #elif defined(TARGET_NUMAKER_PFM_M453)
00142     audioBuf[writePtr] = audio.rxBuffer[0];
00143     audioBuf[++writePtr] = audio.rxBuffer[1];
00144     ++writePtr;
00145     if (writePtr > 4094) {
00146         writePtr = 0;
00147         flag = 1;
00148     }
00149 #elif defined(TARGET_NUMAKER_PFM_M487)
00150     audioBuf[writePtr] = audio.rxBuffer[0];
00151     //audioBuf[++writePtr] = audio.rxBuffer[1];
00152     //audioBuf[++writePtr] = audio.rxBuffer[2];
00153     //audioBuf[++writePtr] = audio.rxBuffer[3];
00154     //audioBuf[++writePtr] = audio.rxBuffer[4];
00155     //audioBuf[++writePtr] = audio.rxBuffer[5];
00156     //audioBuf[++writePtr] = audio.rxBuffer[6];
00157     //audioBuf[++writePtr] = audio.rxBuffer[7];
00158     ++writePtr;
00159     theta += 1;
00160     if (writePtr > 4094) {
00161         writePtr = 0;
00162     }
00163 #elif defined(TARGET_NUMAKER_PFM_NANO130)
00164     /* note
00165        due to SRAM size limitation
00166        just demo loopback
00167     */
00168 #elif defined(TARGET_NUMAKER_IOT_M487)
00169     audioBuf[writePtr] = audio.rxBuffer[0];
00170     //audioBuf[++writePtr] = audio.rxBuffer[1];
00171     //audioBuf[++writePtr] = audio.rxBuffer[2];
00172     //audioBuf[++writePtr] = audio.rxBuffer[3];
00173     //audioBuf[++writePtr] = audio.rxBuffer[4];
00174     //audioBuf[++writePtr] = audio.rxBuffer[5];
00175     //audioBuf[++writePtr] = audio.rxBuffer[6];
00176     //audioBuf[++writePtr] = audio.rxBuffer[7];
00177     ++writePtr;
00178     theta += 1;
00179     if (writePtr > 4094) {
00180         writePtr = 0;
00181     }
00182 #endif
00183 }
00184 
00185 void loopback(void) {
00186     audio.readwrite();
00187 }
00188 
00189 void fillAudioBuf(void) {
00190 #if defined(TARGET_NUMAKER_PFM_NUC472)
00191     while (!feof(fd)) {
00192         if (theta < 4096) {
00193             // read 2 integers
00194             fread(&audioBuf[writePtr], 4, 2, fd);
00195             audio.lock();   // protect shared variable
00196             theta += 2;
00197             audio.unlock(); // protect shared variable
00198             writePtr += 2;
00199             writePtr &= 0xFFF;
00200         }
00201     }
00202 #elif defined(TARGET_NUMAKER_PFM_M453)
00203     while (1) {
00204 #if MBED_MAJOR_VERSION >= 6
00205     ThisThread::sleep_for(500);
00206 #else
00207         Thread::wait(500);
00208 #endif
00209 
00210         printf("fill\r\n");
00211     }
00212 #elif defined(TARGET_NUMAKER_PFM_M487)
00213     while (!feof(fd)) {
00214         if (theta < 4096) {
00215             // read 2 integers
00216             fread(&audioBuf[writePtr], 4, 2, fd);
00217             audio.lock();   // protect shared variable
00218             theta += 2;
00219             audio.unlock(); // protect shared variable
00220             writePtr += 2;
00221             writePtr &= 0xFFF;
00222         }
00223     }
00224 #elif defined(TARGET_NUMAKER_PFM_NANO130)
00225     while (1) {
00226 #if MBED_MAJOR_VERSION >= 6
00227     ThisThread::sleep_for(500);
00228 #else
00229         Thread::wait(500);
00230 #endif
00231         printf("fill\r\n");
00232     }
00233 #elif defined(TARGET_NUMAKER_IOT_M487)
00234     while (!feof(fd)) {
00235         if (theta < 4096) {
00236             // read 2 integers
00237             fread(&audioBuf[writePtr], 4, 2, fd);
00238             audio.lock();   // protect shared variable
00239             theta += 2;
00240             audio.unlock(); // protect shared variable
00241             writePtr += 2;
00242             writePtr &= 0xFFF;
00243         }
00244     }
00245 #endif
00246 }
00247 
00248 void drainAudioBuf(void) {
00249 #if defined(TARGET_NUMAKER_PFM_NUC472)
00250     int i = 0;
00251     while (1) {
00252         if (theta > 512 ) {
00253             fwrite(&audioBuf[readPtr], 4, 128, fd);
00254             audio.lock();   // protect shared variable
00255             theta -= 128;
00256             audio.unlock(); // protect shared variable
00257             readPtr += 128;
00258             if (readPtr > 4094)
00259                 readPtr = 0;
00260             
00261             i += 512;
00262         }
00263         
00264         /* record about 10 seconds PCM */
00265         if (i >= samplingRate*channelCount*sampleBitLength/8*10)
00266             break;
00267     }
00268 #elif defined(TARGET_NUMAKER_PFM_M453)
00269     while (flag == 0) {
00270 #if MBED_MAJOR_VERSION >= 6
00271     ThisThread::sleep_for(500);
00272 #else
00273         Thread::wait(500);
00274 #endif
00275     }
00276 #elif defined(TARGET_NUMAKER_PFM_M487)
00277     int i = 0;
00278     while (1) {
00279         if (theta > 512 ) {
00280             fwrite(&audioBuf[readPtr], 4, 128, fd);
00281             audio.lock();   // protect shared variable
00282             theta -= 128;
00283             audio.unlock(); // protect shared variable
00284             readPtr += 128;
00285             if (readPtr > 4094)
00286                 readPtr = 0;
00287             
00288             i += 512;
00289         }
00290         
00291         /* record about 10 seconds PCM */
00292         if (i >= samplingRate*channelCount*sampleBitLength/8*10)
00293             break;
00294     }
00295 #elif defined(TARGET_NUMAKER_PFM_NANO130)
00296     while (flag == 0) {
00297 #if MBED_MAJOR_VERSION >= 6
00298     ThisThread::sleep_for(500);
00299 #else
00300         Thread::wait(500);
00301 #endif
00302     }
00303 #elif defined(TARGET_NUMAKER_IOT_M487)
00304     int i = 0;
00305     while (1) {
00306         if (theta > 512 ) {
00307             fwrite(&audioBuf[readPtr], 4, 128, fd);
00308             audio.lock();   // protect shared variable
00309             theta -= 128;
00310             audio.unlock(); // protect shared variable
00311             readPtr += 128;
00312             if (readPtr > 4094)
00313                 readPtr = 0;
00314             
00315             i += 512;
00316         }
00317         
00318         /* record about 10 seconds PCM */
00319         if (i >= samplingRate*channelCount*sampleBitLength/8*10)
00320             break;
00321     }
00322 #endif
00323 }
00324 
00325 void demo_record(void) {
00326     /* Init global varibles first */
00327     readPtr = 0;
00328     writePtr = 0;
00329     theta = 0;
00330 
00331 #if defined(TARGET_NUMAKER_PFM_NANO130)
00332     for (int i = 0; i < 128; i++) {
00333 #else
00334     for (int i = 0; i < 4096; i++) {
00335 #endif
00336         audioBuf[i] = 0;
00337     }
00338     
00339 #if defined(TARGET_NUMAKER_PFM_NUC472) || defined(TARGET_NUMAKER_PFM_M487) || defined(TARGET_NUMAKER_IOT_M487)
00340     printf("Opening a new file test.pcm");
00341     fd = fopen("/fs/test.pcm", "w");
00342     errno_error(fd);
00343 #endif
00344     
00345     audio.attach(&record);
00346     audio.format(samplingRate, channelCount, sampleBitLength);
00347     
00348     printf("Start recording...\r\n");
00349     audio.record();
00350     
00351     drainAudioBuf();
00352     
00353 #if defined(TARGET_NUMAKER_PFM_NUC472) || defined(TARGET_NUMAKER_PFM_M487) || defined(TARGET_NUMAKER_IOT_M487)
00354     fclose(fd);
00355 #endif
00356     
00357     printf("Stop recording.\r\n");
00358     audio.stop();
00359 }
00360 
00361 void demo_play(void) {
00362     /* Init global varibles first */
00363     readPtr = 0;
00364     writePtr = 0;
00365     theta = 0;
00366 
00367 #if defined(TARGET_NUMAKER_PFM_NUC472) || defined(TARGET_NUMAKER_PFM_M487) || defined(TARGET_NUMAKER_IOT_M487)
00368     printf("Opening file test.pcm read-only");
00369     fd = fopen("/fs/test.pcm", "r");
00370     //fd = fopen("/fs/82.wav", "r");
00371     errno_error(fd);
00372     
00373     //fseek(fd, 44, SEEK_SET);
00374     
00375     for (int i = 0; i < 4096; i++) {
00376         audioBuf[i] = 0;
00377     }
00378 #else
00379     for (int i = 0; i < 8; i++) {
00380         audioBuf[i] = (sine1k[i * 4 + 3] << 24) | (sine1k[i * 4 + 2] << 16) | (sine1k[i * 4 + 1] << 8) | (sine1k[i * 4 + 0] << 0);
00381         //printf("0x%08X\r\n", audioBuf[i]);
00382     }
00383 #endif
00384     
00385     // disable headphone
00386     hp_enable = 1;
00387     
00388     audio.attach(&play);
00389     audio.format(samplingRate, channelCount, sampleBitLength);
00390     
00391     // enable headphone
00392     hp_enable = 0;
00393     
00394     printf("Start playing...\r\n");
00395     audio.start();
00396     
00397     fillAudioBuf();
00398     
00399 #if defined(TARGET_NUMAKER_PFM_NUC472) || defined(TARGET_NUMAKER_PFM_M487) || defined(TARGET_NUMAKER_IOT_M487)
00400     fclose(fd);
00401 #endif
00402     
00403     printf("Stop playing.\r\n");
00404     audio.stop();
00405     
00406     // disable headphone
00407     hp_enable = 1;
00408 }
00409 
00410 void demo_loopback(void) {
00411     /* Init global varibles first */
00412     readPtr = 0;
00413     writePtr = 0;
00414     theta = 0;
00415 
00416     // disable headphone
00417     hp_enable = 1;
00418     
00419     audio.attach(&loopback);
00420     audio.format(samplingRate, channelCount, sampleBitLength);
00421     
00422     // enable headphone
00423     hp_enable = 0;
00424     
00425     printf("Start loopback...\r\n");
00426     audio.loopback();
00427     
00428     while (1) {
00429 #if MBED_MAJOR_VERSION >= 6
00430     ThisThread::sleep_for(500);
00431 #else
00432         Thread::wait(500);
00433 #endif
00434         printf("loopback\r\n");
00435     }
00436     
00437     printf("Stop loopback.\r\n");
00438     audio.stop();
00439     
00440     // disable headphone
00441     hp_enable = 1;
00442 }
00443 
00444 int main(void) {
00445     led = 1;
00446 #ifdef MBED_MAJOR_VERSION
00447     printf("Mbed OS version %d.%d.%d\r\n\n", MBED_MAJOR_VERSION, MBED_MINOR_VERSION, MBED_PATCH_VERSION);
00448 #endif    
00449     button.rise(&flip);
00450     
00451 #if defined(TARGET_NUMAKER_PFM_NUC472) || defined(TARGET_NUMAKER_PFM_M487) || defined(TARGET_NUMAKER_IOT_M487)
00452     int error = 0;
00453     
00454     printf("Mounting the filesystem on \"/fs\" ");
00455     error = fs.mount(&bd);
00456     return_error(error);
00457 #endif
00458     
00459     //demo_record();
00460     
00461     //demo_play();
00462     
00463     demo_loopback();
00464     
00465     led = 0;
00466 }