Headers for MAX32630FTHR Demo Board sample programs
noteplayer.cpp@0:a6a8578e95af, 2019-07-30 (annotated)
- Committer:
- Lugs
- Date:
- Tue Jul 30 02:44:30 2019 +0000
- Revision:
- 0:a6a8578e95af
actually working header-using version of mini piano player
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Lugs | 0:a6a8578e95af | 1 | #include "noteplayer.h" |
Lugs | 0:a6a8578e95af | 2 | |
Lugs | 0:a6a8578e95af | 3 | float *audioDataBuffer = new float[128]; |
Lugs | 0:a6a8578e95af | 4 | volatile int bufferPOS = 0; |
Lugs | 0:a6a8578e95af | 5 | Ticker SampleTime; |
Lugs | 0:a6a8578e95af | 6 | |
Lugs | 0:a6a8578e95af | 7 | void placeNewSample() |
Lugs | 0:a6a8578e95af | 8 | { |
Lugs | 0:a6a8578e95af | 9 | speaker.write(audioDataBuffer[bufferPOS++]); //multiply by POT value for volume. |
Lugs | 0:a6a8578e95af | 10 | bufferPOS = (bufferPOS+1) & 0x07F; |
Lugs | 0:a6a8578e95af | 11 | } |
Lugs | 0:a6a8578e95af | 12 | |
Lugs | 0:a6a8578e95af | 13 | void loadBuffer() |
Lugs | 0:a6a8578e95af | 14 | { |
Lugs | 0:a6a8578e95af | 15 | printf("Generating sine...\r\n"); |
Lugs | 0:a6a8578e95af | 16 | for(int i=0; i<128; i++) { |
Lugs | 0:a6a8578e95af | 17 | audioDataBuffer[i] =((1.0 + sin((double(i)/16.0*6.28318530717959)))/2.0); //formula copied from mbed example |
Lugs | 0:a6a8578e95af | 18 | } |
Lugs | 0:a6a8578e95af | 19 | } |
Lugs | 0:a6a8578e95af | 20 | |
Lugs | 0:a6a8578e95af | 21 | note makeNote(unsigned char length, pitchname pitch2make) |
Lugs | 0:a6a8578e95af | 22 | { |
Lugs | 0:a6a8578e95af | 23 | note madenote; |
Lugs | 0:a6a8578e95af | 24 | madenote.length = length; |
Lugs | 0:a6a8578e95af | 25 | madenote.pitch = pitch2make; |
Lugs | 0:a6a8578e95af | 26 | return madenote; |
Lugs | 0:a6a8578e95af | 27 | } |
Lugs | 0:a6a8578e95af | 28 | |
Lugs | 0:a6a8578e95af | 29 | void playNote(note note2play) |
Lugs | 0:a6a8578e95af | 30 | { |
Lugs | 0:a6a8578e95af | 31 | float SPB = 60.0*4.0/(120.0); |
Lugs | 0:a6a8578e95af | 32 | int PlayingFreq = pitch2freq(note2play.pitch); |
Lugs | 0:a6a8578e95af | 33 | if(PlayingFreq == 0) { //rest |
Lugs | 0:a6a8578e95af | 34 | wait((1/(float)note2play.length)*SPB); |
Lugs | 0:a6a8578e95af | 35 | } else { |
Lugs | 0:a6a8578e95af | 36 | int sampleRate = PlayingFreq * 16; |
Lugs | 0:a6a8578e95af | 37 | speaker.period_us(1); //1MHz |
Lugs | 0:a6a8578e95af | 38 | float ticker_period = (float) 1/(sampleRate); |
Lugs | 0:a6a8578e95af | 39 | printf("\r\nTicker Period: %f\tTicker Freq: %f\r\nTarget Freq: %i \r\n\r\n",ticker_period, 1/ticker_period, PlayingFreq); |
Lugs | 0:a6a8578e95af | 40 | |
Lugs | 0:a6a8578e95af | 41 | SampleTime.attach(&placeNewSample,ticker_period); |
Lugs | 0:a6a8578e95af | 42 | wait((1/(float)note2play.length)*SPB); |
Lugs | 0:a6a8578e95af | 43 | SampleTime.detach(); |
Lugs | 0:a6a8578e95af | 44 | |
Lugs | 0:a6a8578e95af | 45 | printf("\033[A\033[A\033[A\033[A"); |
Lugs | 0:a6a8578e95af | 46 | } |
Lugs | 0:a6a8578e95af | 47 | } |
Lugs | 0:a6a8578e95af | 48 | |
Lugs | 0:a6a8578e95af | 49 | int pitch2freq(pitchname pitch2switch) |
Lugs | 0:a6a8578e95af | 50 | { |
Lugs | 0:a6a8578e95af | 51 | switch(pitch2switch) { |
Lugs | 0:a6a8578e95af | 52 | case rest: |
Lugs | 0:a6a8578e95af | 53 | return 0; |
Lugs | 0:a6a8578e95af | 54 | case D2: |
Lugs | 0:a6a8578e95af | 55 | return 73; |
Lugs | 0:a6a8578e95af | 56 | case Ds2: |
Lugs | 0:a6a8578e95af | 57 | return 78; |
Lugs | 0:a6a8578e95af | 58 | case E2: |
Lugs | 0:a6a8578e95af | 59 | return 82; |
Lugs | 0:a6a8578e95af | 60 | case F2: |
Lugs | 0:a6a8578e95af | 61 | return 87; |
Lugs | 0:a6a8578e95af | 62 | case Fs2: |
Lugs | 0:a6a8578e95af | 63 | return 92; |
Lugs | 0:a6a8578e95af | 64 | case G2: |
Lugs | 0:a6a8578e95af | 65 | return 98; |
Lugs | 0:a6a8578e95af | 66 | case Gs2: |
Lugs | 0:a6a8578e95af | 67 | return 104; |
Lugs | 0:a6a8578e95af | 68 | case A2: |
Lugs | 0:a6a8578e95af | 69 | return 110; |
Lugs | 0:a6a8578e95af | 70 | case As2: |
Lugs | 0:a6a8578e95af | 71 | return 117; |
Lugs | 0:a6a8578e95af | 72 | case B2: |
Lugs | 0:a6a8578e95af | 73 | return 123; |
Lugs | 0:a6a8578e95af | 74 | case C3: |
Lugs | 0:a6a8578e95af | 75 | return 131; |
Lugs | 0:a6a8578e95af | 76 | case Cs3: |
Lugs | 0:a6a8578e95af | 77 | return 139; |
Lugs | 0:a6a8578e95af | 78 | case D3: |
Lugs | 0:a6a8578e95af | 79 | return 147; |
Lugs | 0:a6a8578e95af | 80 | case Ds3: |
Lugs | 0:a6a8578e95af | 81 | return 156; |
Lugs | 0:a6a8578e95af | 82 | case E3: |
Lugs | 0:a6a8578e95af | 83 | return 165; |
Lugs | 0:a6a8578e95af | 84 | case F3: |
Lugs | 0:a6a8578e95af | 85 | return 175; |
Lugs | 0:a6a8578e95af | 86 | case Fs3: |
Lugs | 0:a6a8578e95af | 87 | return 185; |
Lugs | 0:a6a8578e95af | 88 | case G3: |
Lugs | 0:a6a8578e95af | 89 | return 196; |
Lugs | 0:a6a8578e95af | 90 | case Gs3: |
Lugs | 0:a6a8578e95af | 91 | return 208; |
Lugs | 0:a6a8578e95af | 92 | case A3: |
Lugs | 0:a6a8578e95af | 93 | return 220; |
Lugs | 0:a6a8578e95af | 94 | case As3: |
Lugs | 0:a6a8578e95af | 95 | return 233; |
Lugs | 0:a6a8578e95af | 96 | case B3: |
Lugs | 0:a6a8578e95af | 97 | return 247; |
Lugs | 0:a6a8578e95af | 98 | case C4: |
Lugs | 0:a6a8578e95af | 99 | return 262; |
Lugs | 0:a6a8578e95af | 100 | case Cs4: |
Lugs | 0:a6a8578e95af | 101 | return 277; |
Lugs | 0:a6a8578e95af | 102 | case D4: |
Lugs | 0:a6a8578e95af | 103 | return 294; |
Lugs | 0:a6a8578e95af | 104 | case Ds4: |
Lugs | 0:a6a8578e95af | 105 | return 311; |
Lugs | 0:a6a8578e95af | 106 | case E4: |
Lugs | 0:a6a8578e95af | 107 | return 330; |
Lugs | 0:a6a8578e95af | 108 | case F4: |
Lugs | 0:a6a8578e95af | 109 | return 349; |
Lugs | 0:a6a8578e95af | 110 | case Fs4: |
Lugs | 0:a6a8578e95af | 111 | return 370; |
Lugs | 0:a6a8578e95af | 112 | |
Lugs | 0:a6a8578e95af | 113 | case G4: |
Lugs | 0:a6a8578e95af | 114 | return 392; |
Lugs | 0:a6a8578e95af | 115 | |
Lugs | 0:a6a8578e95af | 116 | case Gs4: |
Lugs | 0:a6a8578e95af | 117 | return 415; |
Lugs | 0:a6a8578e95af | 118 | |
Lugs | 0:a6a8578e95af | 119 | case A4: |
Lugs | 0:a6a8578e95af | 120 | return 440; |
Lugs | 0:a6a8578e95af | 121 | |
Lugs | 0:a6a8578e95af | 122 | case As4: |
Lugs | 0:a6a8578e95af | 123 | return 466; |
Lugs | 0:a6a8578e95af | 124 | |
Lugs | 0:a6a8578e95af | 125 | case B4: |
Lugs | 0:a6a8578e95af | 126 | return 494; |
Lugs | 0:a6a8578e95af | 127 | |
Lugs | 0:a6a8578e95af | 128 | case C5: |
Lugs | 0:a6a8578e95af | 129 | return 523; |
Lugs | 0:a6a8578e95af | 130 | |
Lugs | 0:a6a8578e95af | 131 | case Cs5: |
Lugs | 0:a6a8578e95af | 132 | return 554; |
Lugs | 0:a6a8578e95af | 133 | |
Lugs | 0:a6a8578e95af | 134 | case D5: |
Lugs | 0:a6a8578e95af | 135 | return 587; |
Lugs | 0:a6a8578e95af | 136 | |
Lugs | 0:a6a8578e95af | 137 | case Ds5: |
Lugs | 0:a6a8578e95af | 138 | return 622; |
Lugs | 0:a6a8578e95af | 139 | |
Lugs | 0:a6a8578e95af | 140 | case E5: |
Lugs | 0:a6a8578e95af | 141 | return 659; |
Lugs | 0:a6a8578e95af | 142 | |
Lugs | 0:a6a8578e95af | 143 | case F5: |
Lugs | 0:a6a8578e95af | 144 | return 698; |
Lugs | 0:a6a8578e95af | 145 | |
Lugs | 0:a6a8578e95af | 146 | case Fs5: |
Lugs | 0:a6a8578e95af | 147 | return 740; |
Lugs | 0:a6a8578e95af | 148 | |
Lugs | 0:a6a8578e95af | 149 | case G5: |
Lugs | 0:a6a8578e95af | 150 | return 784; |
Lugs | 0:a6a8578e95af | 151 | |
Lugs | 0:a6a8578e95af | 152 | case Gs5: |
Lugs | 0:a6a8578e95af | 153 | return 831; |
Lugs | 0:a6a8578e95af | 154 | |
Lugs | 0:a6a8578e95af | 155 | case A5: |
Lugs | 0:a6a8578e95af | 156 | return 880; |
Lugs | 0:a6a8578e95af | 157 | |
Lugs | 0:a6a8578e95af | 158 | case As5: |
Lugs | 0:a6a8578e95af | 159 | return 932; |
Lugs | 0:a6a8578e95af | 160 | |
Lugs | 0:a6a8578e95af | 161 | case B5: |
Lugs | 0:a6a8578e95af | 162 | return 988; |
Lugs | 0:a6a8578e95af | 163 | |
Lugs | 0:a6a8578e95af | 164 | case C6: |
Lugs | 0:a6a8578e95af | 165 | return 1047; |
Lugs | 0:a6a8578e95af | 166 | |
Lugs | 0:a6a8578e95af | 167 | case Cs6: |
Lugs | 0:a6a8578e95af | 168 | return 1109; |
Lugs | 0:a6a8578e95af | 169 | |
Lugs | 0:a6a8578e95af | 170 | case D6: |
Lugs | 0:a6a8578e95af | 171 | return 1175; |
Lugs | 0:a6a8578e95af | 172 | |
Lugs | 0:a6a8578e95af | 173 | case Ds6: |
Lugs | 0:a6a8578e95af | 174 | return 1245; |
Lugs | 0:a6a8578e95af | 175 | |
Lugs | 0:a6a8578e95af | 176 | case E6: |
Lugs | 0:a6a8578e95af | 177 | return 1319; |
Lugs | 0:a6a8578e95af | 178 | |
Lugs | 0:a6a8578e95af | 179 | case F6: |
Lugs | 0:a6a8578e95af | 180 | return 1397; |
Lugs | 0:a6a8578e95af | 181 | |
Lugs | 0:a6a8578e95af | 182 | case Fs6: |
Lugs | 0:a6a8578e95af | 183 | return 1480; |
Lugs | 0:a6a8578e95af | 184 | |
Lugs | 0:a6a8578e95af | 185 | case G6: |
Lugs | 0:a6a8578e95af | 186 | return 1568; |
Lugs | 0:a6a8578e95af | 187 | |
Lugs | 0:a6a8578e95af | 188 | case Gs6: |
Lugs | 0:a6a8578e95af | 189 | return 1661; |
Lugs | 0:a6a8578e95af | 190 | |
Lugs | 0:a6a8578e95af | 191 | case END: |
Lugs | 0:a6a8578e95af | 192 | return -1; |
Lugs | 0:a6a8578e95af | 193 | } |
Lugs | 0:a6a8578e95af | 194 | } |