Riproduce un messaggio sonoro
Dependencies: mbed BSP_DISCO_L496AG
main.cpp@1:4baec1317561, 2019-04-27 (annotated)
- Committer:
- pinofal
- Date:
- Sat Apr 27 14:29:22 2019 +0000
- Revision:
- 1:4baec1317561
- Parent:
- 0:817b136c4489
Riproduce un messaggio sonoro DISCO-L496
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Vito1704 | 0:817b136c4489 | 1 | #include "mbed.h" |
Vito1704 | 0:817b136c4489 | 2 | #include "stm32l496g_discovery_lcd.h" |
Vito1704 | 0:817b136c4489 | 3 | #include "stm32l496g_discovery_audio.h" |
pinofal | 1:4baec1317561 | 4 | #include "SampledSoundDontTouch.h" |
Vito1704 | 0:817b136c4489 | 5 | |
Vito1704 | 0:817b136c4489 | 6 | #define BSP_AUDIO_OUT_STEREOMODE ((uint32_t)0x00000004) /* STEREO MODE */ |
Vito1704 | 0:817b136c4489 | 7 | #define BSP_AUDIO_FREQUENCY_44K SAI_AUDIO_FREQUENCY_44K // Frequenza di campionamento |
Vito1704 | 0:817b136c4489 | 8 | #define AUDIODATA_SIZE 2 /* 16-bits audio data size */ |
Vito1704 | 0:817b136c4489 | 9 | #define DEFAULT_AUDIO_OUT_CHANNEL_NBR ((uint8_t)2) // Stereo 2 |
Vito1704 | 0:817b136c4489 | 10 | |
Vito1704 | 0:817b136c4489 | 11 | // definizione durata note |
Vito1704 | 0:817b136c4489 | 12 | #define SEMIBREVE 2000000 // durata della semibreve in microsecondi = 4sec |
Vito1704 | 0:817b136c4489 | 13 | #define MINIMA SEMIBREVE/2 |
Vito1704 | 0:817b136c4489 | 14 | #define SEMIMINIMA SEMIBREVE/4 |
Vito1704 | 0:817b136c4489 | 15 | #define CROMA SEMIBREVE/8 |
Vito1704 | 0:817b136c4489 | 16 | #define SEMICROMA CROMA/16 |
Vito1704 | 0:817b136c4489 | 17 | #define BISCROMA SEMIBREVE/32 |
Vito1704 | 0:817b136c4489 | 18 | #define SEMIBISCROMA SEMIBREVE/64 |
Vito1704 | 0:817b136c4489 | 19 | #define SBRMIN SEMIBREVE+SEMIMINIMA |
Vito1704 | 0:817b136c4489 | 20 | #define MINSEMIN MINIMA+SEMIMINIMA |
Vito1704 | 0:817b136c4489 | 21 | #define SEMCROMA SEMIMINIMA+CROMA |
Vito1704 | 0:817b136c4489 | 22 | #define SEMCROMCROM SEMICROMA+CROMA |
Vito1704 | 0:817b136c4489 | 23 | |
Vito1704 | 0:817b136c4489 | 24 | // definizione della frequenza delle note ottava centrale del pianoforte |
Vito1704 | 0:817b136c4489 | 25 | #define m 0 |
Vito1704 | 0:817b136c4489 | 26 | #define C4 261.63 |
Vito1704 | 0:817b136c4489 | 27 | #define C1d 277.18 |
Vito1704 | 0:817b136c4489 | 28 | #define D1b 277.18 |
Vito1704 | 0:817b136c4489 | 29 | #define D1 293.66 |
Vito1704 | 0:817b136c4489 | 30 | #define D1d 311.13 |
Vito1704 | 0:817b136c4489 | 31 | #define E1b 311.13 |
Vito1704 | 0:817b136c4489 | 32 | #define E1 329.63 |
Vito1704 | 0:817b136c4489 | 33 | #define F1 349.23 |
Vito1704 | 0:817b136c4489 | 34 | #define F1d 369.99 |
Vito1704 | 0:817b136c4489 | 35 | #define G1b 369.99 |
Vito1704 | 0:817b136c4489 | 36 | #define G4 392.0 |
Vito1704 | 0:817b136c4489 | 37 | #define G1d 415.3 |
Vito1704 | 0:817b136c4489 | 38 | #define A1b 415.3 |
Vito1704 | 0:817b136c4489 | 39 | #define A1 440.0 |
Vito1704 | 0:817b136c4489 | 40 | #define A1d 466.16 |
Vito1704 | 0:817b136c4489 | 41 | #define B1b 466.16 |
Vito1704 | 0:817b136c4489 | 42 | #define B1 493.18 |
Vito1704 | 0:817b136c4489 | 43 | #define F5 698.0 |
Vito1704 | 0:817b136c4489 | 44 | #define G5 784.0 |
Vito1704 | 0:817b136c4489 | 45 | #define E5b 622.0 |
Vito1704 | 0:817b136c4489 | 46 | #define C5 523.0 |
Vito1704 | 0:817b136c4489 | 47 | #define LA4 440.0 |
Vito1704 | 0:817b136c4489 | 48 | #define LA4d 466.0 |
Vito1704 | 0:817b136c4489 | 49 | #define D4 294.0 |
Vito1704 | 0:817b136c4489 | 50 | #define D4d 311.0 |
Vito1704 | 0:817b136c4489 | 51 | #define B4 494.0 |
Vito1704 | 0:817b136c4489 | 52 | #define F4 349.0 |
Vito1704 | 0:817b136c4489 | 53 | #define F4d 370.0 |
Vito1704 | 0:817b136c4489 | 54 | #define B4b 466.0 |
Vito1704 | 0:817b136c4489 | 55 | #define A4b 415.0 |
Vito1704 | 0:817b136c4489 | 56 | #define E4 330.0 |
Vito1704 | 0:817b136c4489 | 57 | #define C6 1046.0 |
Vito1704 | 0:817b136c4489 | 58 | #define C7 2093.0 |
Vito1704 | 0:817b136c4489 | 59 | #define B5 988.0 |
Vito1704 | 0:817b136c4489 | 60 | #define G7 3136.0 |
Vito1704 | 0:817b136c4489 | 61 | #define D6 1175.0 |
Vito1704 | 0:817b136c4489 | 62 | #define E4b 311.0 |
Vito1704 | 0:817b136c4489 | 63 | #define E5 659.0 |
Vito1704 | 0:817b136c4489 | 64 | #define G5 784.0 |
Vito1704 | 0:817b136c4489 | 65 | #define A5b 831.0 |
Vito1704 | 0:817b136c4489 | 66 | #define B5b 932.0 |
Vito1704 | 0:817b136c4489 | 67 | #define C8 4186.0 |
Vito1704 | 0:817b136c4489 | 68 | #define G4 392.0 |
Vito1704 | 0:817b136c4489 | 69 | #define F4d 370.0 |
Vito1704 | 0:817b136c4489 | 70 | #define C4d 277.0 |
Vito1704 | 0:817b136c4489 | 71 | #define A4d 466.0 |
Vito1704 | 0:817b136c4489 | 72 | #define C5d 554.0 |
Vito1704 | 0:817b136c4489 | 73 | #define F4d 370.0 |
Vito1704 | 0:817b136c4489 | 74 | #define F5d 698.0 |
Vito1704 | 0:817b136c4489 | 75 | #define A4 440.0 |
Vito1704 | 0:817b136c4489 | 76 | #define D5 587.0 |
Vito1704 | 0:817b136c4489 | 77 | #define A5d 932.0 |
Vito1704 | 0:817b136c4489 | 78 | #define D5d 622.0 |
Vito1704 | 0:817b136c4489 | 79 | |
Vito1704 | 0:817b136c4489 | 80 | // numero di campioni che compongono un periodo della sinusoide in Output sull'ADC |
Vito1704 | 0:817b136c4489 | 81 | #define SAMPLESINENUM 45// consigliabile avere multipli di 45 |
Vito1704 | 0:817b136c4489 | 82 | |
Vito1704 | 0:817b136c4489 | 83 | // parametri dell'onda coseno da generare |
Vito1704 | 0:817b136c4489 | 84 | #define PI (3.141592653589793238462) |
Vito1704 | 0:817b136c4489 | 85 | #define AMPLITUDE 32767 //(1.0) // x * 3.3V |
Vito1704 | 0:817b136c4489 | 86 | #define PHASE (PI/2) // 2*pi è un periodo |
Vito1704 | 0:817b136c4489 | 87 | #define OFFSET 32767 //(0x7FFF) |
Vito1704 | 0:817b136c4489 | 88 | |
Vito1704 | 0:817b136c4489 | 89 | // numero di note componenti la scala diatonica |
Vito1704 | 0:817b136c4489 | 90 | #define NUMTONE 120 |
Vito1704 | 0:817b136c4489 | 91 | |
Vito1704 | 0:817b136c4489 | 92 | // ticker per la generazione dell'onda con DAC |
Vito1704 | 0:817b136c4489 | 93 | Ticker SampleOutTicker; |
Vito1704 | 0:817b136c4489 | 94 | |
Vito1704 | 0:817b136c4489 | 95 | // Buffer contenente la sinusoide da porre in output. |
Vito1704 | 0:817b136c4489 | 96 | unsigned short usaSine[SAMPLESINENUM]; |
Vito1704 | 0:817b136c4489 | 97 | |
Vito1704 | 0:817b136c4489 | 98 | // prototipo di funzione che genera i campioni della sinusoide da utilizzare per la generazione tramite DAC |
Vito1704 | 0:817b136c4489 | 99 | void CalculateSinewave(void); |
Vito1704 | 0:817b136c4489 | 100 | // cSoundWave = 0 for sine and = 1 for square |
Vito1704 | 0:817b136c4489 | 101 | char cSoundWave; |
Vito1704 | 0:817b136c4489 | 102 | |
Vito1704 | 0:817b136c4489 | 103 | void CalculateSinewave(int nOffset, int nAmplitude, double fPhase) |
Vito1704 | 0:817b136c4489 | 104 | { |
Vito1704 | 0:817b136c4489 | 105 | // variabile contenente l'angolo in radianti |
Vito1704 | 0:817b136c4489 | 106 | double fRads; |
Vito1704 | 0:817b136c4489 | 107 | // indici per i cicli |
Vito1704 | 0:817b136c4489 | 108 | int nIndex; |
Vito1704 | 0:817b136c4489 | 109 | // passo in frequenza fissato dal numero di campioni in cui voglio dividere un periodo di sinusoide: DeltaF = 360°/NUMSAMPLE |
Vito1704 | 0:817b136c4489 | 110 | double fDeltaF; |
Vito1704 | 0:817b136c4489 | 111 | // angolo per il quale bisogna calcolare il valore di sinusoide: fAngle = nIndex*DeltaF |
Vito1704 | 0:817b136c4489 | 112 | double fAngle; |
Vito1704 | 0:817b136c4489 | 113 | |
Vito1704 | 0:817b136c4489 | 114 | // a seconda della selezione, genera una diversa forma d'onda |
Vito1704 | 0:817b136c4489 | 115 | // ATTENZIONE ----- SAREBBE MEGLIO CAMBIARE IL NOME DELL'ARRAY in usaWave[] !!!!! ---- |
Vito1704 | 0:817b136c4489 | 116 | if(cSoundWave=='0') |
Vito1704 | 0:817b136c4489 | 117 | { |
Vito1704 | 0:817b136c4489 | 118 | // genera forma d'onda sinusoidale |
Vito1704 | 0:817b136c4489 | 119 | fDeltaF = 360.0/SAMPLESINENUM; |
Vito1704 | 0:817b136c4489 | 120 | for (nIndex = 0; nIndex < SAMPLESINENUM; nIndex++) |
Vito1704 | 0:817b136c4489 | 121 | { |
Vito1704 | 0:817b136c4489 | 122 | fAngle = nIndex*fDeltaF; // angolo per il quale bisogna calcolare il campione di sinusoide |
Vito1704 | 0:817b136c4489 | 123 | fRads = (PI * fAngle)/180.0; // Convert degree in radian |
Vito1704 | 0:817b136c4489 | 124 | //usaSine[nIndex] = AMPLITUDE * cos(fRads + PHASE) + OFFSET; |
Vito1704 | 0:817b136c4489 | 125 | usaSine[nIndex] = nAmplitude * cos(fRads + fPhase) + nOffset; |
Vito1704 | 0:817b136c4489 | 126 | } |
Vito1704 | 0:817b136c4489 | 127 | } |
Vito1704 | 0:817b136c4489 | 128 | else |
Vito1704 | 0:817b136c4489 | 129 | { |
Vito1704 | 0:817b136c4489 | 130 | // genera forma d'onda quadra. |
Vito1704 | 0:817b136c4489 | 131 | for (nIndex = 0; nIndex < SAMPLESINENUM/2; nIndex++) |
Vito1704 | 0:817b136c4489 | 132 | { |
Vito1704 | 0:817b136c4489 | 133 | usaSine[nIndex] = nAmplitude*(1.0)+ nOffset; |
Vito1704 | 0:817b136c4489 | 134 | } |
Vito1704 | 0:817b136c4489 | 135 | for (nIndex = SAMPLESINENUM/2; nIndex < SAMPLESINENUM; nIndex++) |
Vito1704 | 0:817b136c4489 | 136 | { |
Vito1704 | 0:817b136c4489 | 137 | usaSine[nIndex] = nAmplitude*(-1.0)+ nOffset; |
Vito1704 | 0:817b136c4489 | 138 | } |
Vito1704 | 0:817b136c4489 | 139 | } |
Vito1704 | 0:817b136c4489 | 140 | } |
Vito1704 | 0:817b136c4489 | 141 | |
Vito1704 | 0:817b136c4489 | 142 | |
Vito1704 | 0:817b136c4489 | 143 | int main() |
Vito1704 | 0:817b136c4489 | 144 | { |
Vito1704 | 0:817b136c4489 | 145 | BSP_LCD_Init(); |
Vito1704 | 0:817b136c4489 | 146 | |
Vito1704 | 0:817b136c4489 | 147 | // indice per i cicli |
Vito1704 | 0:817b136c4489 | 148 | int nIndex; |
Vito1704 | 0:817b136c4489 | 149 | |
Vito1704 | 0:817b136c4489 | 150 | // numero di note che compongono il brano |
Vito1704 | 0:817b136c4489 | 151 | #define WHENDURATION 32 // numero di note che compongono il brano |
Vito1704 | 0:817b136c4489 | 152 | // note del brano |
Vito1704 | 0:817b136c4489 | 153 | float fNoteWhen [WHENDURATION] = {D4, F4d, G4, A1, D4, F4d, G4, A1, D4, F4d, G4, A1, F4d, |
Vito1704 | 0:817b136c4489 | 154 | D4, F4d, E4, F4d, F4d, E4, D4, D4, F4d, A1, A1, G4, F4d, G4, A1, F4d, D4, E4, D4}; |
Vito1704 | 0:817b136c4489 | 155 | // durata delle note del brano |
Vito1704 | 0:817b136c4489 | 156 | float fLengthWhen[WHENDURATION] ={SEMIMINIMA, SEMIMINIMA, SEMIMINIMA, SBRMIN, SEMIMINIMA, |
Vito1704 | 0:817b136c4489 | 157 | SEMIMINIMA, SEMIMINIMA, SBRMIN, SEMIMINIMA, SEMIMINIMA, SEMIMINIMA, MINIMA, MINIMA, |
Vito1704 | 0:817b136c4489 | 158 | MINIMA, MINIMA, SBRMIN, SEMIMINIMA, SEMIMINIMA, SEMIMINIMA, SBRMIN, SBRMIN, MINIMA, MINIMA, SEMIMINIMA, |
Vito1704 | 0:817b136c4489 | 159 | SBRMIN, SEMIMINIMA, SEMIMINIMA, MINIMA, MINIMA, MINIMA, MINIMA, SEMIBREVE}; |
Vito1704 | 0:817b136c4489 | 160 | |
pinofal | 1:4baec1317561 | 161 | |
Vito1704 | 0:817b136c4489 | 162 | while(1) { |
Vito1704 | 0:817b136c4489 | 163 | BSP_LCD_SetFont(&Font12); |
Vito1704 | 0:817b136c4489 | 164 | BSP_LCD_Clear(LCD_COLOR_MAGENTA); |
Vito1704 | 0:817b136c4489 | 165 | BSP_LCD_SetBackColor(LCD_COLOR_MAGENTA); |
Vito1704 | 0:817b136c4489 | 166 | BSP_LCD_SetTextColor(LCD_COLOR_ORANGE); |
Vito1704 | 0:817b136c4489 | 167 | BSP_LCD_DisplayStringAt(0, LINE(4), (uint8_t *)"DISCO_L496AG", CENTER_MODE); |
Vito1704 | 0:817b136c4489 | 168 | BSP_LCD_DisplayStringAt(0, LINE(5), (uint8_t *)"VIOLATER", CENTER_MODE); |
Vito1704 | 0:817b136c4489 | 169 | BSP_LCD_DisplayStringAt(0, LINE(6), (uint8_t *)"WELCOME", CENTER_MODE); |
Vito1704 | 0:817b136c4489 | 170 | wait(2); |
Vito1704 | 0:817b136c4489 | 171 | BSP_LCD_SetFont(&Font16); |
Vito1704 | 0:817b136c4489 | 172 | BSP_LCD_Clear(LCD_COLOR_RED); |
Vito1704 | 0:817b136c4489 | 173 | BSP_LCD_SetBackColor(LCD_COLOR_RED); |
Vito1704 | 0:817b136c4489 | 174 | BSP_LCD_SetTextColor(LCD_COLOR_YELLOW); |
Vito1704 | 0:817b136c4489 | 175 | BSP_LCD_DisplayStringAt(0, LINE(4), (uint8_t *)"DISCO_L496AG", CENTER_MODE); |
Vito1704 | 0:817b136c4489 | 176 | BSP_LCD_DisplayStringAt(0, LINE(5), (uint8_t *)"VIOLATER", CENTER_MODE); |
Vito1704 | 0:817b136c4489 | 177 | BSP_LCD_DisplayStringAt(0, LINE(6), (uint8_t *)"WELCOME", CENTER_MODE); |
Vito1704 | 0:817b136c4489 | 178 | wait(2); |
Vito1704 | 0:817b136c4489 | 179 | BSP_LCD_SetFont(&Font20); |
Vito1704 | 0:817b136c4489 | 180 | BSP_LCD_Clear(LCD_COLOR_GREEN); |
Vito1704 | 0:817b136c4489 | 181 | BSP_LCD_SetBackColor(LCD_COLOR_GREEN); |
Vito1704 | 0:817b136c4489 | 182 | BSP_LCD_SetTextColor(LCD_COLOR_BLACK); |
Vito1704 | 0:817b136c4489 | 183 | BSP_LCD_DisplayStringAt(0, LINE(4), (uint8_t *)"DISCO_L496AG", CENTER_MODE); |
Vito1704 | 0:817b136c4489 | 184 | BSP_LCD_DisplayStringAt(0, LINE(5), (uint8_t *)"VIOLATER", CENTER_MODE); |
Vito1704 | 0:817b136c4489 | 185 | BSP_LCD_DisplayStringAt(0, LINE(6), (uint8_t *)"WELCOME", CENTER_MODE); |
Vito1704 | 0:817b136c4489 | 186 | wait(2); |
Vito1704 | 0:817b136c4489 | 187 | BSP_LCD_Clear(LCD_COLOR_BLUE); |
Vito1704 | 0:817b136c4489 | 188 | BSP_LCD_SetBackColor(LCD_COLOR_BLUE); |
Vito1704 | 0:817b136c4489 | 189 | BSP_LCD_SetTextColor(LCD_COLOR_WHITE); |
Vito1704 | 0:817b136c4489 | 190 | BSP_LCD_SetFont(&Font24); |
Vito1704 | 0:817b136c4489 | 191 | BSP_LCD_DisplayStringAt(0, LINE(4), (uint8_t *)"DISCO_L496AG", CENTER_MODE); |
Vito1704 | 0:817b136c4489 | 192 | BSP_LCD_DisplayStringAt(0, LINE(5), (uint8_t *)"VIOLATER", CENTER_MODE); |
Vito1704 | 0:817b136c4489 | 193 | BSP_LCD_DisplayStringAt(0, LINE(6), (uint8_t *)"WHEN THE SAINT GO MARCHING IN", CENTER_MODE); |
Vito1704 | 0:817b136c4489 | 194 | wait(2); |
Vito1704 | 0:817b136c4489 | 195 | cSoundWave = 0; |
pinofal | 1:4baec1317561 | 196 | /* --- Inizio OLD ONE ---- |
Vito1704 | 0:817b136c4489 | 197 | BSP_AUDIO_OUT_SetVolume(50.0); |
Vito1704 | 0:817b136c4489 | 198 | for(nIndex=0; nIndex<WHENDURATION; nIndex++) |
Vito1704 | 0:817b136c4489 | 199 | { |
pinofal | 1:4baec1317561 | 200 | BSP_AUDIO_OUT_Init( OUTPUT_DEVICE_HEADPHONE, 50.0, fNoteWhen[nIndex]); |
pinofal | 1:4baec1317561 | 201 | BSP_AUDIO_OUT_Init(OUTPUT_DEVICE_HEADPHONE, 100, BSP_AUDIO_FREQUENCY_22K); |
Vito1704 | 0:817b136c4489 | 202 | if (BSP_AUDIO_OUT_Stop != 0) |
Vito1704 | 0:817b136c4489 | 203 | { |
Vito1704 | 0:817b136c4489 | 204 | BSP_AUDIO_OUT_Play((uint16_t *)OUTPUT_DEVICE_HEADPHONE, AUDIODATA_SIZE); // oppure fNoteWhen[nIndex] |
Vito1704 | 0:817b136c4489 | 205 | } |
Vito1704 | 0:817b136c4489 | 206 | wait_us(fLengthWhen[nIndex]); |
Vito1704 | 0:817b136c4489 | 207 | } |
Vito1704 | 0:817b136c4489 | 208 | wait(2); |
pinofal | 1:4baec1317561 | 209 | --- Fine OLD ONE --- */ |
pinofal | 1:4baec1317561 | 210 | //BSP_AUDIO_OUT_DeInit(); |
pinofal | 1:4baec1317561 | 211 | BSP_AUDIO_OUT_Init(OUTPUT_DEVICE_HEADPHONE, 100, SAI_AUDIO_FREQUENCY_16K); |
pinofal | 1:4baec1317561 | 212 | BSP_AUDIO_OUT_ChangeAudioConfig(BSP_AUDIO_OUT_MONOMODE | BSP_AUDIO_OUT_NORMALMODE); |
pinofal | 1:4baec1317561 | 213 | BSP_AUDIO_OUT_Play((uint16_t*)&naInputSoundWaveDontTouch[0], 15403); // l'array generato è nel file SampledSoundDontTouch.h |
pinofal | 1:4baec1317561 | 214 | |
pinofal | 1:4baec1317561 | 215 | |
Vito1704 | 0:817b136c4489 | 216 | } //p.s. La funzione per il vecchio amplificatore SampleOut per la lettura della sinusoide non funziona |
Vito1704 | 0:817b136c4489 | 217 | |
Vito1704 | 0:817b136c4489 | 218 | } |