For MAX32630FTHR microcontroller: Plays piano notes in Nokia composer format text file placed in the SD card
Dependencies: USBMSD_BD SDFileSystem max32630fthr USBDevice
main.cpp@6:d4cba969e419, 2019-07-19 (annotated)
- Committer:
- Lugs
- Date:
- Fri Jul 19 06:48:36 2019 +0000
- Revision:
- 6:d4cba969e419
- Parent:
- 5:8ba2f1e291b9
working. doesn't have support for dotted notes.
Who changed what in which revision?
User | Revision | Line number | New 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 | 4:24086b80928e | 6 | #include "ctype.h" |
Lugs | 3:fcf745cd4f6d | 7 | |
Lugs | 0:ad5ce0aff429 | 8 | #define BUFFER_SIZE 128 |
Lugs | 0:ad5ce0aff429 | 9 | #define HALF_BUFFER 64 |
Lugs | 0:ad5ce0aff429 | 10 | |
Lugs | 6:d4cba969e419 | 11 | //add an input timeout function says kuya ben |
Lugs | 6:d4cba969e419 | 12 | |
Lugs | 0:ad5ce0aff429 | 13 | DigitalOut rLED(LED1); |
Lugs | 0:ad5ce0aff429 | 14 | DigitalOut gLED(LED2); |
Lugs | 0:ad5ce0aff429 | 15 | DigitalOut bLED(LED3); |
Lugs | 0:ad5ce0aff429 | 16 | |
Lugs | 0:ad5ce0aff429 | 17 | DigitalIn Button(P2_3); |
Lugs | 0:ad5ce0aff429 | 18 | |
Lugs | 0:ad5ce0aff429 | 19 | MAX32630FTHR pegasus(MAX32630FTHR::VIO_3V3); |
Lugs | 0:ad5ce0aff429 | 20 | PwmOut PWM(P5_6); |
Lugs | 0:ad5ce0aff429 | 21 | AnalogIn POT(AIN_0); |
Lugs | 0:ad5ce0aff429 | 22 | volatile int bufferPOS = 0; |
Lugs | 0:ad5ce0aff429 | 23 | |
Lugs | 0:ad5ce0aff429 | 24 | Serial daplink(P2_1,P2_0); |
Lugs | 0:ad5ce0aff429 | 25 | USBSerial microUSB; |
Lugs | 0:ad5ce0aff429 | 26 | |
Lugs | 0:ad5ce0aff429 | 27 | float audioDataBuffer[BUFFER_SIZE]; |
Lugs | 0:ad5ce0aff429 | 28 | |
Lugs | 4:24086b80928e | 29 | struct WavFile { |
Lugs | 0:ad5ce0aff429 | 30 | long int size; |
Lugs | 0:ad5ce0aff429 | 31 | int channels; |
Lugs | 0:ad5ce0aff429 | 32 | int sampleRate; |
Lugs | 0:ad5ce0aff429 | 33 | int bitsPerSample; |
Lugs | 0:ad5ce0aff429 | 34 | }; |
Lugs | 0:ad5ce0aff429 | 35 | |
Lugs | 0:ad5ce0aff429 | 36 | float potval,reading; |
Lugs | 0:ad5ce0aff429 | 37 | |
Lugs | 0:ad5ce0aff429 | 38 | void placeNewSample(void) |
Lugs | 0:ad5ce0aff429 | 39 | { |
Lugs | 2:93da96b41127 | 40 | PWM.write(audioDataBuffer[bufferPOS++]); //multiply by POT value for volume. |
Lugs | 0:ad5ce0aff429 | 41 | bufferPOS = (bufferPOS+1) & 0x07F; |
Lugs | 0:ad5ce0aff429 | 42 | } |
Lugs | 0:ad5ce0aff429 | 43 | |
Lugs | 0:ad5ce0aff429 | 44 | int main() |
Lugs | 4:24086b80928e | 45 | { |
Lugs | 0:ad5ce0aff429 | 46 | WavFile Track; |
Lugs | 0:ad5ce0aff429 | 47 | Ticker SampleTime; |
Lugs | 4:24086b80928e | 48 | |
Lugs | 3:fcf745cd4f6d | 49 | daplink.printf("\f---DAPLINK SERIAL PORT---\r\n\r\nMINI PIANO PLAYER ver 1 \r\n\r\n\r\n"); |
Lugs | 0:ad5ce0aff429 | 50 | microUSB.printf("micro USB serial port\r\n"); |
Lugs | 0:ad5ce0aff429 | 51 | rLED = LED_ON; |
Lugs | 0:ad5ce0aff429 | 52 | wait_ms(500); |
Lugs | 0:ad5ce0aff429 | 53 | rLED = LED_OFF; |
Lugs | 0:ad5ce0aff429 | 54 | gLED = LED_ON; |
Lugs | 0:ad5ce0aff429 | 55 | wait_ms(500); |
Lugs | 0:ad5ce0aff429 | 56 | bLED = LED_ON; |
Lugs | 0:ad5ce0aff429 | 57 | gLED = LED_OFF; |
Lugs | 4:24086b80928e | 58 | |
Lugs | 3:fcf745cd4f6d | 59 | typedef enum { |
Lugs | 3:fcf745cd4f6d | 60 | C2,Cs2,D2,Ds2,E2,F2,Fs2,G2,Gs2,A2,As2,B2, //C2:0 |
Lugs | 3:fcf745cd4f6d | 61 | C3,Cs3,D3,Ds3,E3,F3,Fs3,G3,Gs3,A3,As3,B3, //C3:12 |
Lugs | 3:fcf745cd4f6d | 62 | C4,Cs4,D4,Ds4,E4,F4,Fs4,G4,Gs4,A4,As4,B4, //C4:24 |
Lugs | 3:fcf745cd4f6d | 63 | C5,Cs5,D5,Ds5,E5,F5,Fs5,G5,Gs5,A5,As5,B5, //C5:36 |
Lugs | 3:fcf745cd4f6d | 64 | C6,Cs6,D6,Ds6,E6,F6,Fs6,G6,Gs6, //C6:48 |
Lugs | 3:fcf745cd4f6d | 65 | rest, |
Lugs | 3:fcf745cd4f6d | 66 | END |
Lugs | 4:24086b80928e | 67 | } pitchname; |
Lugs | 4:24086b80928e | 68 | |
Lugs | 3:fcf745cd4f6d | 69 | typedef struct { |
Lugs | 4:24086b80928e | 70 | int length; |
Lugs | 3:fcf745cd4f6d | 71 | pitchname pitch; |
Lugs | 3:fcf745cd4f6d | 72 | } note; |
Lugs | 4:24086b80928e | 73 | |
Lugs | 4:24086b80928e | 74 | //input iterator vars |
Lugs | 4:24086b80928e | 75 | int i; |
Lugs | 4:24086b80928e | 76 | char c; |
Lugs | 6:d4cba969e419 | 77 | |
Lugs | 4:24086b80928e | 78 | //input holder arrays |
Lugs | 6:d4cba969e419 | 79 | char buffer[2048]; |
Lugs | 6:d4cba969e419 | 80 | char inputtype[10]; |
Lugs | 4:24086b80928e | 81 | typedef enum { |
Lugs | 4:24086b80928e | 82 | nokiacomposer, |
Lugs | 4:24086b80928e | 83 | normal |
Lugs | 4:24086b80928e | 84 | } inputtypes; |
Lugs | 4:24086b80928e | 85 | inputtypes current_inputtype; |
Lugs | 4:24086b80928e | 86 | |
Lugs | 4:24086b80928e | 87 | //parse input type |
Lugs | 4:24086b80928e | 88 | printf("Please specify the input type.\r\nAvailable types:\r\nnokiacomposer\r\nnormal\r\n\r\n>"); |
Lugs | 6:d4cba969e419 | 89 | for(i=0; i<10 && c != '\r'; i++) { |
Lugs | 4:24086b80928e | 90 | c=daplink.getc(); |
Lugs | 4:24086b80928e | 91 | daplink.putc(c); |
Lugs | 4:24086b80928e | 92 | inputtype[i]=c; |
Lugs | 4:24086b80928e | 93 | } |
Lugs | 6:d4cba969e419 | 94 | if(inputtype == "nokiacomposer") { |
Lugs | 4:24086b80928e | 95 | current_inputtype = nokiacomposer; |
Lugs | 6:d4cba969e419 | 96 | } else if(inputtype == "normal") { |
Lugs | 4:24086b80928e | 97 | current_inputtype = normal; |
Lugs | 4:24086b80928e | 98 | } else { |
Lugs | 6:d4cba969e419 | 99 | printf("\r\nInvalid input type. Try again.\033[A"); //prints message below input area |
Lugs | 4:24086b80928e | 100 | } |
Lugs | 6:d4cba969e419 | 101 | printf("\r\n"); |
Lugs | 4:24086b80928e | 102 | |
Lugs | 3:fcf745cd4f6d | 103 | //open file |
Lugs | 4:24086b80928e | 104 | toolarge: |
Lugs | 4:24086b80928e | 105 | printf("Please copy the text file of the song then right click on the PuTTY screen.\r\n"); |
Lugs | 6:d4cba969e419 | 106 | for(i=0; daplink.readable(); i++) { |
Lugs | 4:24086b80928e | 107 | c=daplink.getc(); |
Lugs | 4:24086b80928e | 108 | daplink.putc(c); |
Lugs | 4:24086b80928e | 109 | buffer[i]=c; |
Lugs | 6:d4cba969e419 | 110 | if(i>=2048) { |
Lugs | 4:24086b80928e | 111 | printf("Note data is greater than 2kB! Impossible. Try again.\033[A"); |
Lugs | 4:24086b80928e | 112 | goto toolarge; |
Lugs | 4:24086b80928e | 113 | } |
Lugs | 4:24086b80928e | 114 | } |
Lugs | 6:d4cba969e419 | 115 | buffer[i] = '\0'; //set EOF marker. |
Lugs | 4:24086b80928e | 116 | |
Lugs | 4:24086b80928e | 117 | //kunwari open na yung file |
Lugs | 6:d4cba969e419 | 118 | printf("File recieved. Parsing...\r\n"); |
Lugs | 3:fcf745cd4f6d | 119 | //parse file into song in heap(remove sharps, put into enums.) |
Lugs | 6:d4cba969e419 | 120 | |
Lugs | 4:24086b80928e | 121 | //useful benchmarks to find which characters are valid. |
Lugs | 4:24086b80928e | 122 | int candidate_number; |
Lugs | 6:d4cba969e419 | 123 | char *songpos; //song position |
Lugs | 6:d4cba969e419 | 124 | note song[2048]; |
Lugs | 4:24086b80928e | 125 | int pn_det; |
Lugs | 4:24086b80928e | 126 | |
Lugs | 4:24086b80928e | 127 | if(current_inputtype == nokiacomposer) { |
Lugs | 4:24086b80928e | 128 | //for every [note] slot in [song] |
Lugs | 6:d4cba969e419 | 129 | for(i=0; i<2048; i++) { |
Lugs | 4:24086b80928e | 130 | //take the first base 10 integer you see. |
Lugs | 4:24086b80928e | 131 | //this initializes songpos ALWAYS. lol. |
Lugs | 6:d4cba969e419 | 132 | candidate_number = strtol(songpos,&songpos,10); |
Lugs | 6:d4cba969e419 | 133 | songpos--; //just for printing |
Lugs | 4:24086b80928e | 134 | //if it's 1,2,4,8,16 or 32, VALID. |
Lugs | 6:d4cba969e419 | 135 | printf("%c\n",*songpos); |
Lugs | 6:d4cba969e419 | 136 | if(candidate_number==1||candidate_number==2||candidate_number==4||candidate_number==8||candidate_number==16||candidate_number==32) { |
Lugs | 4:24086b80928e | 137 | song[i].length = candidate_number; |
Lugs | 6:d4cba969e419 | 138 | printf("Entered candidate [%i] into song[%i].length\n",candidate_number,i); |
Lugs | 4:24086b80928e | 139 | } else { |
Lugs | 6:d4cba969e419 | 140 | printf("Found invalid NOTE LENGTH value [%c] at position %li. Skipping until next whitespace...\n",*songpos,songpos-buffer); |
Lugs | 4:24086b80928e | 141 | //skip to after next whitespace |
Lugs | 4:24086b80928e | 142 | do { |
Lugs | 4:24086b80928e | 143 | songpos++; |
Lugs | 4:24086b80928e | 144 | } while(!isspace(*(songpos))); |
Lugs | 4:24086b80928e | 145 | do { |
Lugs | 4:24086b80928e | 146 | songpos++; |
Lugs | 4:24086b80928e | 147 | } while(!isalnum(*(songpos))); |
Lugs | 4:24086b80928e | 148 | //then do next iteration of LENGTH,PITCH,OCTAVE parse |
Lugs | 4:24086b80928e | 149 | i--; |
Lugs | 4:24086b80928e | 150 | continue; |
Lugs | 4:24086b80928e | 151 | } |
Lugs | 4:24086b80928e | 152 | //parse next character |
Lugs | 6:d4cba969e419 | 153 | songpos++; |
Lugs | 6:d4cba969e419 | 154 | printf("%c\n",*songpos); |
Lugs | 4:24086b80928e | 155 | pn_det=0; |
Lugs | 4:24086b80928e | 156 | //watch out for sharps |
Lugs | 6:d4cba969e419 | 157 | if(!(*songpos=='#'||'a'<=*songpos<='z')) { |
Lugs | 6:d4cba969e419 | 158 | printf("Found invalid PITCH NAME value [%c] at position %li. Skipping word...\n",*songpos,songpos-buffer); |
Lugs | 4:24086b80928e | 159 | //skip to after next whitespace |
Lugs | 4:24086b80928e | 160 | do { |
Lugs | 4:24086b80928e | 161 | songpos++; |
Lugs | 4:24086b80928e | 162 | } while(!isspace(*(songpos))); |
Lugs | 4:24086b80928e | 163 | do { |
Lugs | 4:24086b80928e | 164 | songpos++; |
Lugs | 4:24086b80928e | 165 | } while(!isalnum(*(songpos))); |
Lugs | 4:24086b80928e | 166 | //then do next iteration of LENGTH,PITCH,OCTAVE parse |
Lugs | 4:24086b80928e | 167 | i--; |
Lugs | 4:24086b80928e | 168 | continue; |
Lugs | 4:24086b80928e | 169 | } |
Lugs | 4:24086b80928e | 170 | if(*songpos=='#') { |
Lugs | 4:24086b80928e | 171 | pn_det+=1; |
Lugs | 4:24086b80928e | 172 | songpos++; |
Lugs | 6:d4cba969e419 | 173 | printf("Sharp found.\n"); |
Lugs | 4:24086b80928e | 174 | } |
Lugs | 4:24086b80928e | 175 | switch(*songpos) { |
Lugs | 4:24086b80928e | 176 | case 'c': |
Lugs | 4:24086b80928e | 177 | pn_det+=0; |
Lugs | 4:24086b80928e | 178 | break; |
Lugs | 4:24086b80928e | 179 | case 'd': |
Lugs | 4:24086b80928e | 180 | pn_det+=2; |
Lugs | 4:24086b80928e | 181 | break; |
Lugs | 4:24086b80928e | 182 | case 'e': |
Lugs | 4:24086b80928e | 183 | pn_det+=4; |
Lugs | 4:24086b80928e | 184 | break; |
Lugs | 4:24086b80928e | 185 | case 'f': |
Lugs | 4:24086b80928e | 186 | pn_det+=5; |
Lugs | 4:24086b80928e | 187 | break; |
Lugs | 4:24086b80928e | 188 | case 'g': |
Lugs | 4:24086b80928e | 189 | pn_det+=7; |
Lugs | 4:24086b80928e | 190 | break; |
Lugs | 4:24086b80928e | 191 | case 'a': |
Lugs | 4:24086b80928e | 192 | pn_det+=9; |
Lugs | 4:24086b80928e | 193 | break; |
Lugs | 4:24086b80928e | 194 | case 'b': |
Lugs | 4:24086b80928e | 195 | pn_det+=11; |
Lugs | 4:24086b80928e | 196 | break; |
Lugs | 4:24086b80928e | 197 | } |
Lugs | 4:24086b80928e | 198 | //check for invalid character at current position |
Lugs | 4:24086b80928e | 199 | //means that character was valid. move on. |
Lugs | 4:24086b80928e | 200 | songpos++; |
Lugs | 4:24086b80928e | 201 | printf("%c\n",*songpos); |
Lugs | 4:24086b80928e | 202 | if('0'<=*songpos<='9') { |
Lugs | 4:24086b80928e | 203 | int num = strtol(songpos,&songpos,10); |
Lugs | 4:24086b80928e | 204 | num+=3;//shift up thrice. board can't handle things this low. |
Lugs | 6:d4cba969e419 | 205 | printf("octave parsed: %i \t-> adder:%i\n",num,((num-2)*12)); |
Lugs | 4:24086b80928e | 206 | pn_det=pn_det+((num-2)*12); |
Lugs | 4:24086b80928e | 207 | } else { |
Lugs | 6:d4cba969e419 | 208 | printf("Found invalid OCTAVE value [%c] at position %li. Skipping word...\n",*songpos,songpos-buffer); |
Lugs | 4:24086b80928e | 209 | //skip to after next whitespace |
Lugs | 4:24086b80928e | 210 | while(!isspace(*(songpos))) { |
Lugs | 4:24086b80928e | 211 | songpos++; |
Lugs | 4:24086b80928e | 212 | } |
Lugs | 4:24086b80928e | 213 | while(!isalnum(*(songpos))) { |
Lugs | 4:24086b80928e | 214 | songpos++; |
Lugs | 4:24086b80928e | 215 | } |
Lugs | 4:24086b80928e | 216 | //then do next iteration of LENGTH,PITCH,OCTAVE parse |
Lugs | 4:24086b80928e | 217 | i--; |
Lugs | 4:24086b80928e | 218 | continue; |
Lugs | 4:24086b80928e | 219 | } |
Lugs | 4:24086b80928e | 220 | song[i].pitch = (pitchname)pn_det; |
Lugs | 6:d4cba969e419 | 221 | printf("Entered pitch [%i] into song[%i].pitch\n",song[i].pitch,i); |
Lugs | 6:d4cba969e419 | 222 | printf("Final: {%i,%i}\n",song[i].length,song[i].pitch); |
Lugs | 6:d4cba969e419 | 223 | printf("-------------------\n"); |
Lugs | 4:24086b80928e | 224 | //make sure you're after the next whitespace |
Lugs | 4:24086b80928e | 225 | while(!isspace(*(songpos))) { |
Lugs | 4:24086b80928e | 226 | songpos++; |
Lugs | 4:24086b80928e | 227 | } |
Lugs | 4:24086b80928e | 228 | while(isspace(*(songpos))) { |
Lugs | 4:24086b80928e | 229 | songpos++; |
Lugs | 4:24086b80928e | 230 | }; |
Lugs | 6:d4cba969e419 | 231 | if(*songpos == '\0') { |
Lugs | 6:d4cba969e419 | 232 | goto out; |
Lugs | 4:24086b80928e | 233 | } |
Lugs | 4:24086b80928e | 234 | } |
Lugs | 4:24086b80928e | 235 | } else if(current_inputtype == normal) { |
Lugs | 4:24086b80928e | 236 | //add normaltype parser here. |
Lugs | 4:24086b80928e | 237 | } |
Lugs | 4:24086b80928e | 238 | |
Lugs | 4:24086b80928e | 239 | //add end term |
Lugs | 4:24086b80928e | 240 | i++; |
Lugs | 4:24086b80928e | 241 | song[i].length = 1; |
Lugs | 4:24086b80928e | 242 | song[i].pitch = END; |
Lugs | 6:d4cba969e419 | 243 | |
Lugs | 6:d4cba969e419 | 244 | //parsing is done |
Lugs | 6:d4cba969e419 | 245 | out: |
Lugs | 6:d4cba969e419 | 246 | int finalval = i; |
Lugs | 6:d4cba969e419 | 247 | for(i=0; i<finalval; i++) { |
Lugs | 6:d4cba969e419 | 248 | printf("{%i,%i},",song[i].length,song[i].pitch); |
Lugs | 6:d4cba969e419 | 249 | if((i+1)%5==0) { |
Lugs | 6:d4cba969e419 | 250 | printf("\n"); |
Lugs | 6:d4cba969e419 | 251 | } |
Lugs | 4:24086b80928e | 252 | } |
Lugs | 4:24086b80928e | 253 | |
Lugs | 0:ad5ce0aff429 | 254 | printf("Generating sine...\r\n"); |
Lugs | 4:24086b80928e | 255 | for(i=0; i<128; i++) { |
Lugs | 0:ad5ce0aff429 | 256 | audioDataBuffer[i] =((1.0 + sin((double(i)/16.0*6.28318530717959)))/2.0); //formula copied from mbed example |
Lugs | 0:ad5ce0aff429 | 257 | } |
Lugs | 4:24086b80928e | 258 | |
Lugs | 4:24086b80928e | 259 | /* HERE'S WHERE THE MUSIC STARTS */ |
Lugs | 4:24086b80928e | 260 | /* LITERALLY. */ |
Lugs | 4:24086b80928e | 261 | |
Lugs | 4:24086b80928e | 262 | //FORMAT: { NUM_BEATS, PITCH } |
Lugs | 4:24086b80928e | 263 | char bpminp[5]; |
Lugs | 4:24086b80928e | 264 | int PlayingFreq; |
Lugs | 4:24086b80928e | 265 | |
Lugs | 6:d4cba969e419 | 266 | restart: |
Lugs | 4:24086b80928e | 267 | |
Lugs | 4:24086b80928e | 268 | printf("Please enter desired BPM:\r\n"); |
Lugs | 4:24086b80928e | 269 | |
Lugs | 4:24086b80928e | 270 | for(i=0; i<5 && c!='\r'; i++) { |
Lugs | 4:24086b80928e | 271 | c = daplink.getc(); |
Lugs | 4:24086b80928e | 272 | bpminp[i] = c; |
Lugs | 4:24086b80928e | 273 | } |
Lugs | 4:24086b80928e | 274 | int BPM = strtol(bpminp,NULL,0); |
Lugs | 6:d4cba969e419 | 275 | float SPB = 60/(float)BPM; |
Lugs | 4:24086b80928e | 276 | |
Lugs | 4:24086b80928e | 277 | /* |
Lugs | 4:24086b80928e | 278 | HARVEST MOON SONG IN NORMAL FORMAT |
Lugs | 4:24086b80928e | 279 | |
Lugs | 3:fcf745cd4f6d | 280 | note song[] = { |
Lugs | 3:fcf745cd4f6d | 281 | //batch 1 |
Lugs | 3:fcf745cd4f6d | 282 | {4,(pitchname)38},{4,E5},{4,Fs5},{4,D5},{4,Fs5},{4,G5},{4,A5},{4,Fs5},{4,D5}, |
Lugs | 3:fcf745cd4f6d | 283 | {4,A5},{4,B5},{4,A5},{4,G5},{4,B5},{4,A5}, |
Lugs | 3:fcf745cd4f6d | 284 | {4,rest}, |
Lugs | 3:fcf745cd4f6d | 285 | //batch 2 |
Lugs | 3:fcf745cd4f6d | 286 | {4,D5},{4,E5},{4,Fs5},{4,D5},{4,Fs5},{4,G5},{4,A5},{4,Fs5},{4,D5}, |
Lugs | 3:fcf745cd4f6d | 287 | {4,A5},{4,G5},{4,D5},{4,Fs5},{4,D5},{4,E5}, |
Lugs | 3:fcf745cd4f6d | 288 | {4,rest}, |
Lugs | 3:fcf745cd4f6d | 289 | //batch 1 |
Lugs | 3:fcf745cd4f6d | 290 | {4,D5},{4,E5},{4,Fs5},{4,D5},{4,Fs5},{4,G5},{4,A5},{4,Fs5},{4,D5}, |
Lugs | 3:fcf745cd4f6d | 291 | {4,A5},{4,B5},{4,A5},{4,G5},{4,B5},{4,A5}, |
Lugs | 3:fcf745cd4f6d | 292 | {4,rest}, |
Lugs | 3:fcf745cd4f6d | 293 | //batch 3 |
Lugs | 3:fcf745cd4f6d | 294 | {2,G5},{4,G5},{4,E5},{4,Fs5},{4,E5},{4,D5},{4,Cs5},{4,D5}, |
Lugs | 3:fcf745cd4f6d | 295 | //END |
Lugs | 3:fcf745cd4f6d | 296 | {1,END} |
Lugs | 3:fcf745cd4f6d | 297 | }; |
Lugs | 4:24086b80928e | 298 | */ |
Lugs | 4:24086b80928e | 299 | |
Lugs | 4:24086b80928e | 300 | for(i = 0; 1; i++) { |
Lugs | 4:24086b80928e | 301 | switch(song[i].pitch) { |
Lugs | 3:fcf745cd4f6d | 302 | case rest: |
Lugs | 6:d4cba969e419 | 303 | wait(song[i].length*SPB); |
Lugs | 3:fcf745cd4f6d | 304 | continue; |
Lugs | 3:fcf745cd4f6d | 305 | case D2: |
Lugs | 3:fcf745cd4f6d | 306 | PlayingFreq = 73; |
Lugs | 3:fcf745cd4f6d | 307 | break; |
Lugs | 3:fcf745cd4f6d | 308 | case Ds2: |
Lugs | 3:fcf745cd4f6d | 309 | PlayingFreq = 78; |
Lugs | 3:fcf745cd4f6d | 310 | break; |
Lugs | 3:fcf745cd4f6d | 311 | case E2: |
Lugs | 3:fcf745cd4f6d | 312 | PlayingFreq = 82; |
Lugs | 3:fcf745cd4f6d | 313 | break; |
Lugs | 3:fcf745cd4f6d | 314 | case F2: |
Lugs | 3:fcf745cd4f6d | 315 | PlayingFreq = 87; |
Lugs | 3:fcf745cd4f6d | 316 | break; |
Lugs | 3:fcf745cd4f6d | 317 | case Fs2: |
Lugs | 3:fcf745cd4f6d | 318 | PlayingFreq = 92; |
Lugs | 3:fcf745cd4f6d | 319 | break; |
Lugs | 3:fcf745cd4f6d | 320 | case G2: |
Lugs | 2:93da96b41127 | 321 | PlayingFreq = 98; |
Lugs | 2:93da96b41127 | 322 | break; |
Lugs | 3:fcf745cd4f6d | 323 | case Gs2: |
Lugs | 3:fcf745cd4f6d | 324 | PlayingFreq = 104; |
Lugs | 2:93da96b41127 | 325 | break; |
Lugs | 3:fcf745cd4f6d | 326 | case A2: |
Lugs | 2:93da96b41127 | 327 | PlayingFreq = 110; |
Lugs | 2:93da96b41127 | 328 | break; |
Lugs | 3:fcf745cd4f6d | 329 | case As2: |
Lugs | 2:93da96b41127 | 330 | PlayingFreq = 117; |
Lugs | 2:93da96b41127 | 331 | break; |
Lugs | 3:fcf745cd4f6d | 332 | case B2: |
Lugs | 2:93da96b41127 | 333 | PlayingFreq = 123; |
Lugs | 2:93da96b41127 | 334 | break; |
Lugs | 3:fcf745cd4f6d | 335 | case C3: |
Lugs | 2:93da96b41127 | 336 | PlayingFreq = 131; |
Lugs | 2:93da96b41127 | 337 | break; |
Lugs | 3:fcf745cd4f6d | 338 | case Cs3: |
Lugs | 2:93da96b41127 | 339 | PlayingFreq = 139; |
Lugs | 2:93da96b41127 | 340 | break; |
Lugs | 3:fcf745cd4f6d | 341 | case D3: |
Lugs | 2:93da96b41127 | 342 | PlayingFreq = 147; |
Lugs | 2:93da96b41127 | 343 | break; |
Lugs | 3:fcf745cd4f6d | 344 | case Ds3: |
Lugs | 2:93da96b41127 | 345 | PlayingFreq = 156; |
Lugs | 2:93da96b41127 | 346 | break; |
Lugs | 3:fcf745cd4f6d | 347 | case E3: |
Lugs | 2:93da96b41127 | 348 | PlayingFreq = 165; |
Lugs | 2:93da96b41127 | 349 | break; |
Lugs | 3:fcf745cd4f6d | 350 | case F3: |
Lugs | 2:93da96b41127 | 351 | PlayingFreq = 175; |
Lugs | 2:93da96b41127 | 352 | break; |
Lugs | 3:fcf745cd4f6d | 353 | case Fs3: |
Lugs | 2:93da96b41127 | 354 | PlayingFreq = 185; |
Lugs | 2:93da96b41127 | 355 | break; |
Lugs | 3:fcf745cd4f6d | 356 | case G3: |
Lugs | 2:93da96b41127 | 357 | PlayingFreq = 196; |
Lugs | 2:93da96b41127 | 358 | break; |
Lugs | 3:fcf745cd4f6d | 359 | case Gs3: |
Lugs | 2:93da96b41127 | 360 | PlayingFreq = 208; |
Lugs | 2:93da96b41127 | 361 | break; |
Lugs | 3:fcf745cd4f6d | 362 | case A3: |
Lugs | 2:93da96b41127 | 363 | PlayingFreq = 220; |
Lugs | 2:93da96b41127 | 364 | break; |
Lugs | 3:fcf745cd4f6d | 365 | case As3: |
Lugs | 2:93da96b41127 | 366 | PlayingFreq = 233; |
Lugs | 2:93da96b41127 | 367 | break; |
Lugs | 3:fcf745cd4f6d | 368 | case B3: |
Lugs | 2:93da96b41127 | 369 | PlayingFreq = 247; |
Lugs | 2:93da96b41127 | 370 | break; |
Lugs | 3:fcf745cd4f6d | 371 | case C4: |
Lugs | 2:93da96b41127 | 372 | PlayingFreq = 262; |
Lugs | 2:93da96b41127 | 373 | break; |
Lugs | 3:fcf745cd4f6d | 374 | case Cs4: |
Lugs | 2:93da96b41127 | 375 | PlayingFreq = 277; |
Lugs | 2:93da96b41127 | 376 | break; |
Lugs | 3:fcf745cd4f6d | 377 | case D4: |
Lugs | 2:93da96b41127 | 378 | PlayingFreq = 294; |
Lugs | 2:93da96b41127 | 379 | break; |
Lugs | 3:fcf745cd4f6d | 380 | case Ds4: |
Lugs | 2:93da96b41127 | 381 | PlayingFreq = 311; |
Lugs | 2:93da96b41127 | 382 | break; |
Lugs | 3:fcf745cd4f6d | 383 | case E4: |
Lugs | 2:93da96b41127 | 384 | PlayingFreq = 330; |
Lugs | 2:93da96b41127 | 385 | break; |
Lugs | 3:fcf745cd4f6d | 386 | case F4: |
Lugs | 2:93da96b41127 | 387 | PlayingFreq = 349; |
Lugs | 2:93da96b41127 | 388 | break; |
Lugs | 3:fcf745cd4f6d | 389 | case Fs4: |
Lugs | 2:93da96b41127 | 390 | PlayingFreq = 370; |
Lugs | 2:93da96b41127 | 391 | break; |
Lugs | 3:fcf745cd4f6d | 392 | case G4: |
Lugs | 2:93da96b41127 | 393 | PlayingFreq = 392; |
Lugs | 2:93da96b41127 | 394 | break; |
Lugs | 3:fcf745cd4f6d | 395 | case Gs4: |
Lugs | 2:93da96b41127 | 396 | PlayingFreq = 415; |
Lugs | 2:93da96b41127 | 397 | break; |
Lugs | 3:fcf745cd4f6d | 398 | case A4: |
Lugs | 2:93da96b41127 | 399 | PlayingFreq = 440; |
Lugs | 2:93da96b41127 | 400 | break; |
Lugs | 3:fcf745cd4f6d | 401 | case As4: |
Lugs | 2:93da96b41127 | 402 | PlayingFreq = 466; |
Lugs | 2:93da96b41127 | 403 | break; |
Lugs | 3:fcf745cd4f6d | 404 | case B4: |
Lugs | 2:93da96b41127 | 405 | PlayingFreq = 494; |
Lugs | 2:93da96b41127 | 406 | break; |
Lugs | 3:fcf745cd4f6d | 407 | case C5: |
Lugs | 2:93da96b41127 | 408 | PlayingFreq = 523; |
Lugs | 2:93da96b41127 | 409 | break; |
Lugs | 3:fcf745cd4f6d | 410 | case Cs5: |
Lugs | 2:93da96b41127 | 411 | PlayingFreq = 554; |
Lugs | 2:93da96b41127 | 412 | break; |
Lugs | 3:fcf745cd4f6d | 413 | case D5: |
Lugs | 2:93da96b41127 | 414 | PlayingFreq = 587; |
Lugs | 2:93da96b41127 | 415 | break; |
Lugs | 3:fcf745cd4f6d | 416 | case Ds5: |
Lugs | 2:93da96b41127 | 417 | PlayingFreq = 622; |
Lugs | 2:93da96b41127 | 418 | break; |
Lugs | 3:fcf745cd4f6d | 419 | case E5: |
Lugs | 2:93da96b41127 | 420 | PlayingFreq = 659; |
Lugs | 2:93da96b41127 | 421 | break; |
Lugs | 3:fcf745cd4f6d | 422 | case F5: |
Lugs | 2:93da96b41127 | 423 | PlayingFreq = 698; |
Lugs | 2:93da96b41127 | 424 | break; |
Lugs | 3:fcf745cd4f6d | 425 | case Fs5: |
Lugs | 2:93da96b41127 | 426 | PlayingFreq = 740; |
Lugs | 2:93da96b41127 | 427 | break; |
Lugs | 3:fcf745cd4f6d | 428 | case G5: |
Lugs | 2:93da96b41127 | 429 | PlayingFreq = 784; |
Lugs | 2:93da96b41127 | 430 | break; |
Lugs | 3:fcf745cd4f6d | 431 | case Gs5: |
Lugs | 2:93da96b41127 | 432 | PlayingFreq = 831; |
Lugs | 2:93da96b41127 | 433 | break; |
Lugs | 3:fcf745cd4f6d | 434 | case A5: |
Lugs | 2:93da96b41127 | 435 | PlayingFreq = 880; |
Lugs | 2:93da96b41127 | 436 | break; |
Lugs | 3:fcf745cd4f6d | 437 | case As5: |
Lugs | 2:93da96b41127 | 438 | PlayingFreq = 932; |
Lugs | 2:93da96b41127 | 439 | break; |
Lugs | 3:fcf745cd4f6d | 440 | case B5: |
Lugs | 2:93da96b41127 | 441 | PlayingFreq = 988; |
Lugs | 2:93da96b41127 | 442 | break; |
Lugs | 3:fcf745cd4f6d | 443 | case C6: |
Lugs | 2:93da96b41127 | 444 | PlayingFreq = 1047; |
Lugs | 2:93da96b41127 | 445 | break; |
Lugs | 3:fcf745cd4f6d | 446 | case Cs6: |
Lugs | 2:93da96b41127 | 447 | PlayingFreq = 1109; |
Lugs | 2:93da96b41127 | 448 | break; |
Lugs | 3:fcf745cd4f6d | 449 | case D6: |
Lugs | 2:93da96b41127 | 450 | PlayingFreq = 1175; |
Lugs | 2:93da96b41127 | 451 | break; |
Lugs | 3:fcf745cd4f6d | 452 | case Ds6: |
Lugs | 2:93da96b41127 | 453 | PlayingFreq = 1245; |
Lugs | 2:93da96b41127 | 454 | break; |
Lugs | 3:fcf745cd4f6d | 455 | case E6: |
Lugs | 2:93da96b41127 | 456 | PlayingFreq = 1319; |
Lugs | 2:93da96b41127 | 457 | break; |
Lugs | 3:fcf745cd4f6d | 458 | case F6: |
Lugs | 2:93da96b41127 | 459 | PlayingFreq = 1397; |
Lugs | 2:93da96b41127 | 460 | break; |
Lugs | 3:fcf745cd4f6d | 461 | case Fs6: |
Lugs | 2:93da96b41127 | 462 | PlayingFreq = 1480; |
Lugs | 2:93da96b41127 | 463 | break; |
Lugs | 3:fcf745cd4f6d | 464 | case G6: |
Lugs | 2:93da96b41127 | 465 | PlayingFreq = 1568; |
Lugs | 2:93da96b41127 | 466 | break; |
Lugs | 3:fcf745cd4f6d | 467 | case Gs6: |
Lugs | 2:93da96b41127 | 468 | PlayingFreq = 1661; |
Lugs | 2:93da96b41127 | 469 | break; |
Lugs | 3:fcf745cd4f6d | 470 | case END: |
Lugs | 3:fcf745cd4f6d | 471 | i = 0; |
Lugs | 3:fcf745cd4f6d | 472 | goto restart; |
Lugs | 2:93da96b41127 | 473 | } |
Lugs | 4:24086b80928e | 474 | |
Lugs | 3:fcf745cd4f6d | 475 | Track.sampleRate = PlayingFreq * 16; //TONE FREQ = SAMPLE RATE / SAMPLES PER CYCLE |
Lugs | 3:fcf745cd4f6d | 476 | PWM.period_us(1); //1MHz |
Lugs | 3:fcf745cd4f6d | 477 | float ticker_period = (float) 1/(Track.sampleRate); |
Lugs | 3:fcf745cd4f6d | 478 | 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 | 479 | |
Lugs | 3:fcf745cd4f6d | 480 | SampleTime.attach(&placeNewSample,ticker_period); |
Lugs | 6:d4cba969e419 | 481 | wait( (1/song[i].length) *SPB); |
Lugs | 3:fcf745cd4f6d | 482 | SampleTime.detach(); |
Lugs | 4:24086b80928e | 483 | |
Lugs | 2:93da96b41127 | 484 | printf("\033[A\033[A\033[A\033[A"); |
Lugs | 0:ad5ce0aff429 | 485 | } |
Lugs | 3:fcf745cd4f6d | 486 | } |
Lugs | 0:ad5ce0aff429 | 487 |