For MAX32630FTHR microcontroller: Plays piano notes in Nokia composer format text file placed in the SD card

Dependencies:   USBMSD_BD SDFileSystem max32630fthr USBDevice

Committer:
Lugs
Date:
Wed Jul 17 06:11:33 2019 +0000
Revision:
3:fcf745cd4f6d
Parent:
2:93da96b41127
Child:
4:24086b80928e
working v1 before major update

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Lugs 0:ad5ce0aff429 1 #include "mbed.h"
Lugs 0:ad5ce0aff429 2 #include "max32630fthr.h"
Lugs 0:ad5ce0aff429 3 #include "USBSerial.h"
Lugs 0:ad5ce0aff429 4 #include "stdio.h"
Lugs 0:ad5ce0aff429 5 #include "SDFileSystem.h"
Lugs 0:ad5ce0aff429 6
Lugs 0:ad5ce0aff429 7 //still needs BITS PER SAMPLE PARSING.
Lugs 0:ad5ce0aff429 8 //do this like so: PWM.write(WavValue/2^BPS)
Lugs 0:ad5ce0aff429 9
Lugs 3:fcf745cd4f6d 10 //MAPPING IS WRONG. P = 440 PLZ. Y = MIDDLE D.
Lugs 3:fcf745cd4f6d 11
Lugs 0:ad5ce0aff429 12 #define BUFFER_SIZE 128
Lugs 0:ad5ce0aff429 13 #define HALF_BUFFER 64
Lugs 0:ad5ce0aff429 14
Lugs 0:ad5ce0aff429 15 DigitalOut rLED(LED1);
Lugs 0:ad5ce0aff429 16 DigitalOut gLED(LED2);
Lugs 0:ad5ce0aff429 17 DigitalOut bLED(LED3);
Lugs 0:ad5ce0aff429 18
Lugs 0:ad5ce0aff429 19 DigitalIn Button(P2_3);
Lugs 0:ad5ce0aff429 20
Lugs 0:ad5ce0aff429 21 MAX32630FTHR pegasus(MAX32630FTHR::VIO_3V3);
Lugs 0:ad5ce0aff429 22 PwmOut PWM(P5_6);
Lugs 0:ad5ce0aff429 23 AnalogIn POT(AIN_0);
Lugs 0:ad5ce0aff429 24 volatile int bufferPOS = 0;
Lugs 0:ad5ce0aff429 25 volatile unsigned int g=0;
Lugs 0:ad5ce0aff429 26
Lugs 0:ad5ce0aff429 27 Serial daplink(P2_1,P2_0);
Lugs 0:ad5ce0aff429 28 USBSerial microUSB;
Lugs 0:ad5ce0aff429 29 SDFileSystem sd(P0_5, P0_6, P0_4, P0_7, "sd");
Lugs 0:ad5ce0aff429 30
Lugs 0:ad5ce0aff429 31 float audioDataBuffer[BUFFER_SIZE];
Lugs 0:ad5ce0aff429 32
Lugs 0:ad5ce0aff429 33 struct WavFile
Lugs 0:ad5ce0aff429 34 {
Lugs 0:ad5ce0aff429 35 long int size;
Lugs 0:ad5ce0aff429 36 int channels;
Lugs 0:ad5ce0aff429 37 int sampleRate;
Lugs 0:ad5ce0aff429 38 int bitsPerSample;
Lugs 0:ad5ce0aff429 39 };
Lugs 0:ad5ce0aff429 40
Lugs 0:ad5ce0aff429 41 float potval,reading;
Lugs 0:ad5ce0aff429 42
Lugs 0:ad5ce0aff429 43 void placeNewSample(void)
Lugs 0:ad5ce0aff429 44 {
Lugs 2:93da96b41127 45 PWM.write(audioDataBuffer[bufferPOS++]); //multiply by POT value for volume.
Lugs 0:ad5ce0aff429 46 bufferPOS = (bufferPOS+1) & 0x07F;
Lugs 0:ad5ce0aff429 47 }
Lugs 0:ad5ce0aff429 48
Lugs 0:ad5ce0aff429 49 int main()
Lugs 0:ad5ce0aff429 50 {
Lugs 0:ad5ce0aff429 51 WavFile Track;
Lugs 0:ad5ce0aff429 52 Ticker SampleTime;
Lugs 0:ad5ce0aff429 53
Lugs 3:fcf745cd4f6d 54 daplink.printf("\f---DAPLINK SERIAL PORT---\r\n\r\nMINI PIANO PLAYER ver 1 \r\n\r\n\r\n");
Lugs 0:ad5ce0aff429 55 microUSB.printf("micro USB serial port\r\n");
Lugs 0:ad5ce0aff429 56 rLED = LED_ON;
Lugs 0:ad5ce0aff429 57 wait_ms(500);
Lugs 0:ad5ce0aff429 58 rLED = LED_OFF;
Lugs 0:ad5ce0aff429 59 gLED = LED_ON;
Lugs 0:ad5ce0aff429 60 wait_ms(500);
Lugs 0:ad5ce0aff429 61 bLED = LED_ON;
Lugs 0:ad5ce0aff429 62 gLED = LED_OFF;
Lugs 0:ad5ce0aff429 63
Lugs 3:fcf745cd4f6d 64 typedef enum {
Lugs 3:fcf745cd4f6d 65 C2,Cs2,D2,Ds2,E2,F2,Fs2,G2,Gs2,A2,As2,B2, //C2:0
Lugs 3:fcf745cd4f6d 66 C3,Cs3,D3,Ds3,E3,F3,Fs3,G3,Gs3,A3,As3,B3, //C3:12
Lugs 3:fcf745cd4f6d 67 C4,Cs4,D4,Ds4,E4,F4,Fs4,G4,Gs4,A4,As4,B4, //C4:24
Lugs 3:fcf745cd4f6d 68 C5,Cs5,D5,Ds5,E5,F5,Fs5,G5,Gs5,A5,As5,B5, //C5:36
Lugs 3:fcf745cd4f6d 69 C6,Cs6,D6,Ds6,E6,F6,Fs6,G6,Gs6, //C6:48
Lugs 3:fcf745cd4f6d 70 rest,
Lugs 3:fcf745cd4f6d 71 END
Lugs 3:fcf745cd4f6d 72 }pitchname;
Lugs 3:fcf745cd4f6d 73
Lugs 3:fcf745cd4f6d 74 typedef struct {
Lugs 3:fcf745cd4f6d 75 float length;
Lugs 3:fcf745cd4f6d 76 pitchname pitch;
Lugs 3:fcf745cd4f6d 77 } note;
Lugs 3:fcf745cd4f6d 78
Lugs 3:fcf745cd4f6d 79 //open file
Lugs 3:fcf745cd4f6d 80
Lugs 3:fcf745cd4f6d 81 //parse file into song in heap(remove sharps, put into enums.)
Lugs 3:fcf745cd4f6d 82
Lugs 0:ad5ce0aff429 83 printf("Generating sine...\r\n");
Lugs 0:ad5ce0aff429 84 int i;
Lugs 0:ad5ce0aff429 85 for(i=0; i<128;i++)
Lugs 0:ad5ce0aff429 86 {
Lugs 0:ad5ce0aff429 87 audioDataBuffer[i] =((1.0 + sin((double(i)/16.0*6.28318530717959)))/2.0); //formula copied from mbed example
Lugs 0:ad5ce0aff429 88 }
Lugs 0:ad5ce0aff429 89
Lugs 3:fcf745cd4f6d 90 /* HERE'S WHERE THE MUSIC STARTS */
Lugs 3:fcf745cd4f6d 91 /* LITERALLY. */
Lugs 0:ad5ce0aff429 92
Lugs 3:fcf745cd4f6d 93 //FORMAT: { NUM_BEATS, PITCH }
Lugs 3:fcf745cd4f6d 94 char c;
Lugs 3:fcf745cd4f6d 95 char bpminp[5];
Lugs 3:fcf745cd4f6d 96 int PlayingFreq;
Lugs 0:ad5ce0aff429 97
Lugs 3:fcf745cd4f6d 98 restart:
Lugs 3:fcf745cd4f6d 99
Lugs 3:fcf745cd4f6d 100 printf("Please enter desired BPM:\r\n");
Lugs 0:ad5ce0aff429 101
Lugs 3:fcf745cd4f6d 102 for(i=0;i<5 && c!='\r';i++)
Lugs 3:fcf745cd4f6d 103 {
Lugs 3:fcf745cd4f6d 104 c = daplink.getc();
Lugs 3:fcf745cd4f6d 105 bpminp[i] = c;
Lugs 3:fcf745cd4f6d 106 }
Lugs 3:fcf745cd4f6d 107 int BPM = strtol(bpminp,NULL,0);
Lugs 3:fcf745cd4f6d 108 float SPB = 60/(float)BPM;
Lugs 3:fcf745cd4f6d 109 note song[] = {
Lugs 3:fcf745cd4f6d 110 //batch 1
Lugs 3:fcf745cd4f6d 111 {4,(pitchname)38},{4,E5},{4,Fs5},{4,D5},{4,Fs5},{4,G5},{4,A5},{4,Fs5},{4,D5},
Lugs 3:fcf745cd4f6d 112 {4,A5},{4,B5},{4,A5},{4,G5},{4,B5},{4,A5},
Lugs 3:fcf745cd4f6d 113 {4,rest},
Lugs 3:fcf745cd4f6d 114 //batch 2
Lugs 3:fcf745cd4f6d 115 {4,D5},{4,E5},{4,Fs5},{4,D5},{4,Fs5},{4,G5},{4,A5},{4,Fs5},{4,D5},
Lugs 3:fcf745cd4f6d 116 {4,A5},{4,G5},{4,D5},{4,Fs5},{4,D5},{4,E5},
Lugs 3:fcf745cd4f6d 117 {4,rest},
Lugs 3:fcf745cd4f6d 118 //batch 1
Lugs 3:fcf745cd4f6d 119 {4,D5},{4,E5},{4,Fs5},{4,D5},{4,Fs5},{4,G5},{4,A5},{4,Fs5},{4,D5},
Lugs 3:fcf745cd4f6d 120 {4,A5},{4,B5},{4,A5},{4,G5},{4,B5},{4,A5},
Lugs 3:fcf745cd4f6d 121 {4,rest},
Lugs 3:fcf745cd4f6d 122 //batch 3
Lugs 3:fcf745cd4f6d 123 {2,G5},{4,G5},{4,E5},{4,Fs5},{4,E5},{4,D5},{4,Cs5},{4,D5},
Lugs 3:fcf745cd4f6d 124 //END
Lugs 3:fcf745cd4f6d 125 {1,END}
Lugs 3:fcf745cd4f6d 126 };
Lugs 3:fcf745cd4f6d 127
Lugs 3:fcf745cd4f6d 128 for(i = 0;1;i++)
Lugs 3:fcf745cd4f6d 129 {
Lugs 3:fcf745cd4f6d 130 switch(song[i].pitch)
Lugs 2:93da96b41127 131 {
Lugs 3:fcf745cd4f6d 132 case rest:
Lugs 3:fcf745cd4f6d 133 wait(song[i].length*SPB);
Lugs 3:fcf745cd4f6d 134 continue;
Lugs 3:fcf745cd4f6d 135 case D2:
Lugs 3:fcf745cd4f6d 136 PlayingFreq = 73;
Lugs 3:fcf745cd4f6d 137 break;
Lugs 3:fcf745cd4f6d 138 case Ds2:
Lugs 3:fcf745cd4f6d 139 PlayingFreq = 78;
Lugs 3:fcf745cd4f6d 140 break;
Lugs 3:fcf745cd4f6d 141 case E2:
Lugs 3:fcf745cd4f6d 142 PlayingFreq = 82;
Lugs 3:fcf745cd4f6d 143 break;
Lugs 3:fcf745cd4f6d 144 case F2:
Lugs 3:fcf745cd4f6d 145 PlayingFreq = 87;
Lugs 3:fcf745cd4f6d 146 break;
Lugs 3:fcf745cd4f6d 147 case Fs2:
Lugs 3:fcf745cd4f6d 148 PlayingFreq = 92;
Lugs 3:fcf745cd4f6d 149 break;
Lugs 3:fcf745cd4f6d 150 case G2:
Lugs 2:93da96b41127 151 PlayingFreq = 98;
Lugs 2:93da96b41127 152 break;
Lugs 3:fcf745cd4f6d 153 case Gs2:
Lugs 3:fcf745cd4f6d 154 PlayingFreq = 104;
Lugs 2:93da96b41127 155 break;
Lugs 3:fcf745cd4f6d 156 case A2:
Lugs 2:93da96b41127 157 PlayingFreq = 110;
Lugs 2:93da96b41127 158 break;
Lugs 3:fcf745cd4f6d 159 case As2:
Lugs 2:93da96b41127 160 PlayingFreq = 117;
Lugs 2:93da96b41127 161 break;
Lugs 3:fcf745cd4f6d 162 case B2:
Lugs 2:93da96b41127 163 PlayingFreq = 123;
Lugs 2:93da96b41127 164 break;
Lugs 3:fcf745cd4f6d 165 case C3:
Lugs 2:93da96b41127 166 PlayingFreq = 131;
Lugs 2:93da96b41127 167 break;
Lugs 3:fcf745cd4f6d 168 case Cs3:
Lugs 2:93da96b41127 169 PlayingFreq = 139;
Lugs 2:93da96b41127 170 break;
Lugs 3:fcf745cd4f6d 171 case D3:
Lugs 2:93da96b41127 172 PlayingFreq = 147;
Lugs 2:93da96b41127 173 break;
Lugs 3:fcf745cd4f6d 174 case Ds3:
Lugs 2:93da96b41127 175 PlayingFreq = 156;
Lugs 2:93da96b41127 176 break;
Lugs 3:fcf745cd4f6d 177 case E3:
Lugs 2:93da96b41127 178 PlayingFreq = 165;
Lugs 2:93da96b41127 179 break;
Lugs 3:fcf745cd4f6d 180 case F3:
Lugs 2:93da96b41127 181 PlayingFreq = 175;
Lugs 2:93da96b41127 182 break;
Lugs 3:fcf745cd4f6d 183 case Fs3:
Lugs 2:93da96b41127 184 PlayingFreq = 185;
Lugs 2:93da96b41127 185 break;
Lugs 3:fcf745cd4f6d 186 case G3:
Lugs 2:93da96b41127 187 PlayingFreq = 196;
Lugs 2:93da96b41127 188 break;
Lugs 3:fcf745cd4f6d 189 case Gs3:
Lugs 2:93da96b41127 190 PlayingFreq = 208;
Lugs 2:93da96b41127 191 break;
Lugs 3:fcf745cd4f6d 192 case A3:
Lugs 2:93da96b41127 193 PlayingFreq = 220;
Lugs 2:93da96b41127 194 break;
Lugs 3:fcf745cd4f6d 195 case As3:
Lugs 2:93da96b41127 196 PlayingFreq = 233;
Lugs 2:93da96b41127 197 break;
Lugs 3:fcf745cd4f6d 198 case B3:
Lugs 2:93da96b41127 199 PlayingFreq = 247;
Lugs 2:93da96b41127 200 break;
Lugs 3:fcf745cd4f6d 201 case C4:
Lugs 2:93da96b41127 202 PlayingFreq = 262;
Lugs 2:93da96b41127 203 break;
Lugs 3:fcf745cd4f6d 204 case Cs4:
Lugs 2:93da96b41127 205 PlayingFreq = 277;
Lugs 2:93da96b41127 206 break;
Lugs 3:fcf745cd4f6d 207 case D4:
Lugs 2:93da96b41127 208 PlayingFreq = 294;
Lugs 2:93da96b41127 209 break;
Lugs 3:fcf745cd4f6d 210 case Ds4:
Lugs 2:93da96b41127 211 PlayingFreq = 311;
Lugs 2:93da96b41127 212 break;
Lugs 3:fcf745cd4f6d 213 case E4:
Lugs 2:93da96b41127 214 PlayingFreq = 330;
Lugs 2:93da96b41127 215 break;
Lugs 3:fcf745cd4f6d 216 case F4:
Lugs 2:93da96b41127 217 PlayingFreq = 349;
Lugs 2:93da96b41127 218 break;
Lugs 3:fcf745cd4f6d 219 case Fs4:
Lugs 2:93da96b41127 220 PlayingFreq = 370;
Lugs 2:93da96b41127 221 break;
Lugs 3:fcf745cd4f6d 222 case G4:
Lugs 2:93da96b41127 223 PlayingFreq = 392;
Lugs 2:93da96b41127 224 break;
Lugs 3:fcf745cd4f6d 225 case Gs4:
Lugs 2:93da96b41127 226 PlayingFreq = 415;
Lugs 2:93da96b41127 227 break;
Lugs 3:fcf745cd4f6d 228 case A4:
Lugs 2:93da96b41127 229 PlayingFreq = 440;
Lugs 2:93da96b41127 230 break;
Lugs 3:fcf745cd4f6d 231 case As4:
Lugs 2:93da96b41127 232 PlayingFreq = 466;
Lugs 2:93da96b41127 233 break;
Lugs 3:fcf745cd4f6d 234 case B4:
Lugs 2:93da96b41127 235 PlayingFreq = 494;
Lugs 2:93da96b41127 236 break;
Lugs 3:fcf745cd4f6d 237 case C5:
Lugs 2:93da96b41127 238 PlayingFreq = 523;
Lugs 2:93da96b41127 239 break;
Lugs 3:fcf745cd4f6d 240 case Cs5:
Lugs 2:93da96b41127 241 PlayingFreq = 554;
Lugs 2:93da96b41127 242 break;
Lugs 3:fcf745cd4f6d 243 case D5:
Lugs 2:93da96b41127 244 PlayingFreq = 587;
Lugs 2:93da96b41127 245 break;
Lugs 3:fcf745cd4f6d 246 case Ds5:
Lugs 2:93da96b41127 247 PlayingFreq = 622;
Lugs 2:93da96b41127 248 break;
Lugs 3:fcf745cd4f6d 249 case E5:
Lugs 2:93da96b41127 250 PlayingFreq = 659;
Lugs 2:93da96b41127 251 break;
Lugs 3:fcf745cd4f6d 252 case F5:
Lugs 2:93da96b41127 253 PlayingFreq = 698;
Lugs 2:93da96b41127 254 break;
Lugs 3:fcf745cd4f6d 255 case Fs5:
Lugs 2:93da96b41127 256 PlayingFreq = 740;
Lugs 2:93da96b41127 257 break;
Lugs 3:fcf745cd4f6d 258 case G5:
Lugs 2:93da96b41127 259 PlayingFreq = 784;
Lugs 2:93da96b41127 260 break;
Lugs 3:fcf745cd4f6d 261 case Gs5:
Lugs 2:93da96b41127 262 PlayingFreq = 831;
Lugs 2:93da96b41127 263 break;
Lugs 3:fcf745cd4f6d 264 case A5:
Lugs 2:93da96b41127 265 PlayingFreq = 880;
Lugs 2:93da96b41127 266 break;
Lugs 3:fcf745cd4f6d 267 case As5:
Lugs 2:93da96b41127 268 PlayingFreq = 932;
Lugs 2:93da96b41127 269 break;
Lugs 3:fcf745cd4f6d 270 case B5:
Lugs 2:93da96b41127 271 PlayingFreq = 988;
Lugs 2:93da96b41127 272 break;
Lugs 3:fcf745cd4f6d 273 case C6:
Lugs 2:93da96b41127 274 PlayingFreq = 1047;
Lugs 2:93da96b41127 275 break;
Lugs 3:fcf745cd4f6d 276 case Cs6:
Lugs 2:93da96b41127 277 PlayingFreq = 1109;
Lugs 2:93da96b41127 278 break;
Lugs 3:fcf745cd4f6d 279 case D6:
Lugs 2:93da96b41127 280 PlayingFreq = 1175;
Lugs 2:93da96b41127 281 break;
Lugs 3:fcf745cd4f6d 282 case Ds6:
Lugs 2:93da96b41127 283 PlayingFreq = 1245;
Lugs 2:93da96b41127 284 break;
Lugs 3:fcf745cd4f6d 285 case E6:
Lugs 2:93da96b41127 286 PlayingFreq = 1319;
Lugs 2:93da96b41127 287 break;
Lugs 3:fcf745cd4f6d 288 case F6:
Lugs 2:93da96b41127 289 PlayingFreq = 1397;
Lugs 2:93da96b41127 290 break;
Lugs 3:fcf745cd4f6d 291 case Fs6:
Lugs 2:93da96b41127 292 PlayingFreq = 1480;
Lugs 2:93da96b41127 293 break;
Lugs 3:fcf745cd4f6d 294 case G6:
Lugs 2:93da96b41127 295 PlayingFreq = 1568;
Lugs 2:93da96b41127 296 break;
Lugs 3:fcf745cd4f6d 297 case Gs6:
Lugs 2:93da96b41127 298 PlayingFreq = 1661;
Lugs 2:93da96b41127 299 break;
Lugs 3:fcf745cd4f6d 300 case END:
Lugs 3:fcf745cd4f6d 301 i = 0;
Lugs 3:fcf745cd4f6d 302 goto restart;
Lugs 2:93da96b41127 303 }
Lugs 3:fcf745cd4f6d 304
Lugs 3:fcf745cd4f6d 305 Track.sampleRate = PlayingFreq * 16; //TONE FREQ = SAMPLE RATE / SAMPLES PER CYCLE
Lugs 3:fcf745cd4f6d 306 PWM.period_us(1); //1MHz
Lugs 3:fcf745cd4f6d 307 float ticker_period = (float) 1/(Track.sampleRate);
Lugs 3:fcf745cd4f6d 308 printf("\r\nTicker Period: %f\tTicker Freq: %f\r\nTarget Freq: %i \r\n\r\n",ticker_period, 1/ticker_period, PlayingFreq);
Lugs 2:93da96b41127 309
Lugs 3:fcf745cd4f6d 310 SampleTime.attach(&placeNewSample,ticker_period);
Lugs 3:fcf745cd4f6d 311 wait(song[i].length*SPB);
Lugs 3:fcf745cd4f6d 312 SampleTime.detach();
Lugs 1:78ca0566c062 313
Lugs 2:93da96b41127 314 printf("\033[A\033[A\033[A\033[A");
Lugs 0:ad5ce0aff429 315 }
Lugs 3:fcf745cd4f6d 316 }
Lugs 0:ad5ce0aff429 317