NuMaker audio playback

Committer:
Chuck Young
Date:
Fri Mar 03 17:54:37 2017 +0800
Revision:
4:0b5c342628ff
Parent:
1:c3f10403b3db
Child:
5:492ea7d38e59
Removed Serial object

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ccli8 1:c3f10403b3db 1 #include "mbed.h"
ccli8 1:c3f10403b3db 2
ccli8 1:c3f10403b3db 3 #include "NAU8822L.h"
ccli8 1:c3f10403b3db 4
ccli8 1:c3f10403b3db 5 #if defined(TARGET_NUMAKER_PFM_NUC472)
ccli8 1:c3f10403b3db 6 #include "NuSDFileSystem.h"
ccli8 1:c3f10403b3db 7
ccli8 1:c3f10403b3db 8 NAU8822L audio(PC_9, PA_15, 0x1A, PG_7, PG_8, PG_9, PI_11, PI_12); // NAU8822L object
ccli8 1:c3f10403b3db 9 DigitalOut hp_enable(PH_2);
ccli8 1:c3f10403b3db 10 NuSDFileSystem Nu_SD(PF_6, PF_7, PF_8, PF_5 ,PF_4, PF_3, PF_2, "sd");
ccli8 1:c3f10403b3db 11 #elif defined(TARGET_NUMAKER_PFM_M453)
ccli8 1:c3f10403b3db 12 NAU8822L audio(PD_4, PD_5, 0x1A, PA_5, PA_6, PA_7, PD_0, PA_4); // NAU8822L object
ccli8 1:c3f10403b3db 13 DigitalOut hp_enable(PE_1);
ccli8 1:c3f10403b3db 14 #endif
Chuck Young 4:0b5c342628ff 15
ccli8 1:c3f10403b3db 16 InterruptIn button(SW2); // button SW2
ccli8 1:c3f10403b3db 17 DigitalOut led(LED1); // flashing LED1(rgbled1)
ccli8 1:c3f10403b3db 18
ccli8 1:c3f10403b3db 19 int readPtr = 0;
ccli8 1:c3f10403b3db 20
ccli8 1:c3f10403b3db 21 #if defined(TARGET_NUMAKER_PFM_NUC472)
ccli8 1:c3f10403b3db 22 FILE *fp;
ccli8 1:c3f10403b3db 23
ccli8 1:c3f10403b3db 24 int audioBuf[4096];
ccli8 1:c3f10403b3db 25
ccli8 1:c3f10403b3db 26 int writePtr = 0;
ccli8 1:c3f10403b3db 27 int theta = 0;
ccli8 1:c3f10403b3db 28 #elif defined(TARGET_NUMAKER_PFM_M453)
ccli8 1:c3f10403b3db 29 // 1k sine wave@sampling rate 16kHz stereo 16-bit
ccli8 1:c3f10403b3db 30 const char sine1k[] = {
ccli8 1:c3f10403b3db 31 0x00, 0x00, 0x00, 0x00, 0x1F, 0x06, 0x1F, 0x06, 0x50, 0x0B, 0x50, 0x0B, 0xC7, 0x0E, 0xC7, 0x0E,
ccli8 1:c3f10403b3db 32 0xFF, 0x0F, 0xFF, 0x0F, 0xC7, 0x0E, 0xC7, 0x0E, 0x50, 0x0B, 0x50, 0x0B, 0x1F, 0x06, 0x1F, 0x06,
ccli8 1:c3f10403b3db 33 0x00, 0x00, 0x00, 0x00, 0xE1, 0xF9, 0xE1, 0xF9, 0xB0, 0xF4, 0xB0, 0xF4, 0x39, 0xF1, 0x39, 0xF1,
ccli8 1:c3f10403b3db 34 0x01, 0xF0, 0x01, 0xF0, 0x39, 0xF1, 0x39, 0xF1, 0xB0, 0xF4, 0xB0, 0xF4, 0xE1, 0xF9, 0xE1, 0xF9
ccli8 1:c3f10403b3db 35 };
ccli8 1:c3f10403b3db 36 #endif
ccli8 1:c3f10403b3db 37
ccli8 1:c3f10403b3db 38 void flip(void) {
ccli8 1:c3f10403b3db 39 led = !led;
ccli8 1:c3f10403b3db 40 }
ccli8 1:c3f10403b3db 41
ccli8 1:c3f10403b3db 42 void playback(void) {
ccli8 1:c3f10403b3db 43 #if defined(TARGET_NUMAKER_PFM_NUC472)
ccli8 1:c3f10403b3db 44 audio.write(audioBuf, readPtr, 4);
ccli8 1:c3f10403b3db 45 readPtr += 4;
ccli8 1:c3f10403b3db 46 readPtr &= 0xFFF;
ccli8 1:c3f10403b3db 47 theta -= 4;
ccli8 1:c3f10403b3db 48 #elif defined(TARGET_NUMAKER_PFM_M453)
ccli8 1:c3f10403b3db 49 audio.write((int *)sine1k, readPtr, 2);
ccli8 1:c3f10403b3db 50 readPtr += 2;
ccli8 1:c3f10403b3db 51 readPtr &= 0xF;
ccli8 1:c3f10403b3db 52 #endif
ccli8 1:c3f10403b3db 53 }
ccli8 1:c3f10403b3db 54
ccli8 1:c3f10403b3db 55 void fillAudioBuf(void) {
ccli8 1:c3f10403b3db 56 #if defined(TARGET_NUMAKER_PFM_NUC472)
ccli8 1:c3f10403b3db 57 while (!feof(fp)) {
ccli8 1:c3f10403b3db 58 if (theta < 4096) {
ccli8 1:c3f10403b3db 59 // read 2 integers
ccli8 1:c3f10403b3db 60 fread(&audioBuf[writePtr], 4, 2, fp);
ccli8 1:c3f10403b3db 61 NVIC_DisableIRQ(I2S1_IRQn); // FIXME
ccli8 1:c3f10403b3db 62 theta += 2;
ccli8 1:c3f10403b3db 63 NVIC_EnableIRQ(I2S1_IRQn); // FIXME
ccli8 1:c3f10403b3db 64 writePtr += 2;
ccli8 1:c3f10403b3db 65 writePtr &= 0xFFF;
ccli8 1:c3f10403b3db 66 }
ccli8 1:c3f10403b3db 67 }
ccli8 1:c3f10403b3db 68 #elif defined(TARGET_NUMAKER_PFM_M453)
ccli8 1:c3f10403b3db 69 while (1);
ccli8 1:c3f10403b3db 70 #endif
ccli8 1:c3f10403b3db 71 }
ccli8 1:c3f10403b3db 72
ccli8 1:c3f10403b3db 73 int main(void) {
ccli8 1:c3f10403b3db 74 led = 1;
ccli8 1:c3f10403b3db 75
ccli8 1:c3f10403b3db 76 // disable headphone
ccli8 1:c3f10403b3db 77 hp_enable = 1;
ccli8 1:c3f10403b3db 78
ccli8 1:c3f10403b3db 79 button.rise(&flip);
ccli8 1:c3f10403b3db 80 #if defined(TARGET_NUMAKER_PFM_NUC472)
ccli8 1:c3f10403b3db 81 fp = fopen("/sd/test.pcm", "r");
ccli8 1:c3f10403b3db 82 if (fp == NULL) {
Chuck Young 4:0b5c342628ff 83 perror("Error opening file!\n");
ccli8 1:c3f10403b3db 84 return -1;
ccli8 1:c3f10403b3db 85 }
ccli8 1:c3f10403b3db 86
ccli8 1:c3f10403b3db 87 for (int i = 0; i < 4096; i++) {
ccli8 1:c3f10403b3db 88 audioBuf[i] = 0;
ccli8 1:c3f10403b3db 89 }
ccli8 1:c3f10403b3db 90 #endif
ccli8 1:c3f10403b3db 91 audio.attach(&playback);
ccli8 1:c3f10403b3db 92
Chuck Young 4:0b5c342628ff 93 printf("Start playing...\n");
ccli8 1:c3f10403b3db 94 audio.start();
ccli8 1:c3f10403b3db 95
ccli8 1:c3f10403b3db 96 // enable headphone
ccli8 1:c3f10403b3db 97 hp_enable = 0;
ccli8 1:c3f10403b3db 98
ccli8 1:c3f10403b3db 99 fillAudioBuf();
ccli8 1:c3f10403b3db 100
Chuck Young 4:0b5c342628ff 101 printf("Stop playing.\n");
ccli8 1:c3f10403b3db 102 audio.stop();
ccli8 1:c3f10403b3db 103
ccli8 1:c3f10403b3db 104 led = 0;
ccli8 1:c3f10403b3db 105 }