Test the Synth sound functions of Pokitto
Dependencies: PokittoLib
SynthTest - Pokitto synth demo
To test this program, simply press "Import into Compiler" button on the right!
SynthTest.cpp@6:cd9679164a70, 2018-05-01 (annotated)
- Committer:
- Pokitto
- Date:
- Tue May 01 18:44:03 2018 +0000
- Revision:
- 6:cd9679164a70
- Parent:
- 5:09131ded851b
New PokittoLib version;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
group-Pokitto-Community-Team | 0:e0bd5da25882 | 1 | #include "Pokitto.h" |
group-Pokitto-Community-Team | 0:e0bd5da25882 | 2 | #include "Synth.h" |
group-Pokitto-Community-Team | 0:e0bd5da25882 | 3 | |
group-Pokitto-Community-Team | 0:e0bd5da25882 | 4 | Pokitto::Core game; |
group-Pokitto-Community-Team | 0:e0bd5da25882 | 5 | Pokitto::Display disp; |
group-Pokitto-Community-Team | 0:e0bd5da25882 | 6 | Pokitto::Sound snd; |
group-Pokitto-Community-Team | 0:e0bd5da25882 | 7 | Pokitto::Buttons btn; |
Pokitto | 6:cd9679164a70 | 8 | #ifndef POK_SIM |
Pokitto | 6:cd9679164a70 | 9 | DigitalOut enable(EXT4); |
Pokitto | 6:cd9679164a70 | 10 | #endif // POK_SIM |
group-Pokitto-Community-Team | 0:e0bd5da25882 | 11 | |
Pokitto | 2:91c41852a646 | 12 | int tonefreq=46; |
group-Pokitto-Community-Team | 0:e0bd5da25882 | 13 | uint8_t amplitude = 255;//127; |
Pokitto | 2:91c41852a646 | 14 | uint8_t wavetype = 1, arpmode=1; |
group-Pokitto-Community-Team | 0:e0bd5da25882 | 15 | uint32_t changed = 1; |
group-Pokitto-Community-Team | 0:e0bd5da25882 | 16 | char notestr[6]; |
group-Pokitto-Community-Team | 0:e0bd5da25882 | 17 | uint8_t sbindx=0,sbx=0,prevy=0; |
group-Pokitto-Community-Team | 0:e0bd5da25882 | 18 | |
Pokitto | 6:cd9679164a70 | 19 | |
group-Pokitto-Community-Team | 0:e0bd5da25882 | 20 | int main() |
group-Pokitto-Community-Team | 0:e0bd5da25882 | 21 | { |
Pokitto | 6:cd9679164a70 | 22 | #ifndef POK_SIM |
Pokitto | 6:cd9679164a70 | 23 | enable=0; |
Pokitto | 6:cd9679164a70 | 24 | #endif // POK_SIM |
group-Pokitto-Community-Team | 0:e0bd5da25882 | 25 | game.begin(); |
Pokitto | 6:cd9679164a70 | 26 | //snd.setVolume(snd.getMaxVol()); // volume to max level depending on headset/speaker safety level |
group-Pokitto-Community-Team | 0:e0bd5da25882 | 27 | disp.persistence = true; |
group-Pokitto-Community-Team | 0:e0bd5da25882 | 28 | disp.color=wavetype+1; |
group-Pokitto-Community-Team | 0:e0bd5da25882 | 29 | disp.bgcolor=0; |
group-Pokitto-Community-Team | 0:e0bd5da25882 | 30 | uint8_t sbindx=0; |
group-Pokitto-Community-Team | 0:e0bd5da25882 | 31 | int oldvol=0; |
Pokitto | 2:91c41852a646 | 32 | int linecenter = (disp.height/4); // center oscilloscope line vertically |
Pokitto | 2:91c41852a646 | 33 | linecenter *=2; |
Pokitto | 2:91c41852a646 | 34 | linecenter += 0; |
group-Pokitto-Community-Team | 0:e0bd5da25882 | 35 | snd.ampEnable(1); |
group-Pokitto-Community-Team | 0:e0bd5da25882 | 36 | snd.playTone(1,tonefreq,amplitude,wavetype,arpmode); |
Pokitto | 2:91c41852a646 | 37 | //snd.playTone(1,100,255,0); |
group-Pokitto-Community-Team | 0:e0bd5da25882 | 38 | // want to have 2 oscillators 1 octave apart ? |
Pokitto | 2:91c41852a646 | 39 | snd.playTone(2,tonefreq+12,amplitude,wavetype,arpmode); |
group-Pokitto-Community-Team | 0:e0bd5da25882 | 40 | |
group-Pokitto-Community-Team | 0:e0bd5da25882 | 41 | disp.clear(); |
group-Pokitto-Community-Team | 0:e0bd5da25882 | 42 | disp.print("Frq:"); |
group-Pokitto-Community-Team | 0:e0bd5da25882 | 43 | getNoteString(notestr,tonefreq); |
group-Pokitto-Community-Team | 0:e0bd5da25882 | 44 | disp.println(notestr); |
group-Pokitto-Community-Team | 0:e0bd5da25882 | 45 | disp.print("Vol:"); |
group-Pokitto-Community-Team | 0:e0bd5da25882 | 46 | disp.println((int)amplitude); |
group-Pokitto-Community-Team | 0:e0bd5da25882 | 47 | disp.print("Wav:"); |
group-Pokitto-Community-Team | 0:e0bd5da25882 | 48 | disp.print((int)wavetype); |
group-Pokitto-Community-Team | 0:e0bd5da25882 | 49 | switch (wavetype) { |
group-Pokitto-Community-Team | 0:e0bd5da25882 | 50 | case 0: |
group-Pokitto-Community-Team | 0:e0bd5da25882 | 51 | disp.println(" Off");break; |
group-Pokitto-Community-Team | 0:e0bd5da25882 | 52 | case 1: |
group-Pokitto-Community-Team | 0:e0bd5da25882 | 53 | disp.println(" Square");break; |
group-Pokitto-Community-Team | 0:e0bd5da25882 | 54 | case 2: |
group-Pokitto-Community-Team | 0:e0bd5da25882 | 55 | disp.println(" Saw");break; |
group-Pokitto-Community-Team | 0:e0bd5da25882 | 56 | case 3: |
group-Pokitto-Community-Team | 0:e0bd5da25882 | 57 | disp.println(" Triang");break; |
group-Pokitto-Community-Team | 0:e0bd5da25882 | 58 | case 4: |
group-Pokitto-Community-Team | 0:e0bd5da25882 | 59 | disp.println(" Noise");break; |
group-Pokitto-Community-Team | 0:e0bd5da25882 | 60 | case 5: |
group-Pokitto-Community-Team | 0:e0bd5da25882 | 61 | disp.println(" SqNois");break; |
group-Pokitto-Community-Team | 0:e0bd5da25882 | 62 | } |
Pokitto | 6:cd9679164a70 | 63 | disp.print("Vol:"); |
Pokitto | 6:cd9679164a70 | 64 | #ifndef POK_SIM |
Pokitto | 6:cd9679164a70 | 65 | disp.print((int)Pokitto::getHWvolume()); |
Pokitto | 6:cd9679164a70 | 66 | disp.print(", "); |
Pokitto | 6:cd9679164a70 | 67 | #endif // POK_SIM |
group-Pokitto-Community-Team | 0:e0bd5da25882 | 68 | disp.println((int)snd.getVolume()); |
group-Pokitto-Community-Team | 0:e0bd5da25882 | 69 | disp.lcdRefresh(disp.screenbuffer); |
group-Pokitto-Community-Team | 0:e0bd5da25882 | 70 | |
Pokitto | 2:91c41852a646 | 71 | |
Pokitto | 2:91c41852a646 | 72 | |
group-Pokitto-Community-Team | 0:e0bd5da25882 | 73 | while (game.isRunning()) { |
group-Pokitto-Community-Team | 0:e0bd5da25882 | 74 | |
group-Pokitto-Community-Team | 0:e0bd5da25882 | 75 | #ifdef POK_SIM |
group-Pokitto-Community-Team | 0:e0bd5da25882 | 76 | #define DEBOU 2048*32 |
group-Pokitto-Community-Team | 0:e0bd5da25882 | 77 | #else |
group-Pokitto-Community-Team | 0:e0bd5da25882 | 78 | #define DEBOU 256 |
group-Pokitto-Community-Team | 0:e0bd5da25882 | 79 | #endif |
group-Pokitto-Community-Team | 0:e0bd5da25882 | 80 | |
Pokitto | 6:cd9679164a70 | 81 | |
group-Pokitto-Community-Team | 0:e0bd5da25882 | 82 | if (changed == 0) { |
group-Pokitto-Community-Team | 0:e0bd5da25882 | 83 | if(btn.upBtn()) { amplitude++; changed = DEBOU; } |
group-Pokitto-Community-Team | 0:e0bd5da25882 | 84 | if(btn.downBtn()) { amplitude--; changed = DEBOU; } |
group-Pokitto-Community-Team | 0:e0bd5da25882 | 85 | if(btn.leftBtn() && (tonefreq > 0)) { tonefreq --; changed = DEBOU; } |
group-Pokitto-Community-Team | 0:e0bd5da25882 | 86 | if(btn.rightBtn() && (tonefreq < 88)) { tonefreq ++; changed = DEBOU; } |
group-Pokitto-Community-Team | 0:e0bd5da25882 | 87 | if(btn.released(BTN_A) && wavetype > 0 ) { wavetype--; changed = DEBOU*10;} |
group-Pokitto-Community-Team | 0:e0bd5da25882 | 88 | if(btn.released(BTN_B) && wavetype < 5 ) { wavetype++; changed = DEBOU*10;} |
group-Pokitto-Community-Team | 0:e0bd5da25882 | 89 | if (snd.getVolume() != oldvol) changed=DEBOU; |
group-Pokitto-Community-Team | 0:e0bd5da25882 | 90 | |
group-Pokitto-Community-Team | 0:e0bd5da25882 | 91 | if (changed) { |
group-Pokitto-Community-Team | 0:e0bd5da25882 | 92 | disp.clear(); |
group-Pokitto-Community-Team | 0:e0bd5da25882 | 93 | disp.color=0;//owavetype+1; |
group-Pokitto-Community-Team | 0:e0bd5da25882 | 94 | disp.fillRectangle(0,0,109,34); |
group-Pokitto-Community-Team | 0:e0bd5da25882 | 95 | disp.color=wavetype+1; |
group-Pokitto-Community-Team | 0:e0bd5da25882 | 96 | disp.bgcolor=0; |
group-Pokitto-Community-Team | 0:e0bd5da25882 | 97 | disp.print("Frq:"); |
group-Pokitto-Community-Team | 0:e0bd5da25882 | 98 | getNoteString(notestr,tonefreq); |
group-Pokitto-Community-Team | 0:e0bd5da25882 | 99 | disp.println(notestr); |
group-Pokitto-Community-Team | 0:e0bd5da25882 | 100 | disp.print("Vol:"); |
group-Pokitto-Community-Team | 0:e0bd5da25882 | 101 | disp.println((int)amplitude); |
group-Pokitto-Community-Team | 0:e0bd5da25882 | 102 | disp.print("Wav:"); |
group-Pokitto-Community-Team | 0:e0bd5da25882 | 103 | disp.print((int)wavetype); |
group-Pokitto-Community-Team | 0:e0bd5da25882 | 104 | switch (wavetype) { |
group-Pokitto-Community-Team | 0:e0bd5da25882 | 105 | case 0: |
group-Pokitto-Community-Team | 0:e0bd5da25882 | 106 | disp.println(" Off");break; |
group-Pokitto-Community-Team | 0:e0bd5da25882 | 107 | case 1: |
group-Pokitto-Community-Team | 0:e0bd5da25882 | 108 | disp.println(" Square");break; |
group-Pokitto-Community-Team | 0:e0bd5da25882 | 109 | case 2: |
group-Pokitto-Community-Team | 0:e0bd5da25882 | 110 | disp.println(" Saw");break; |
group-Pokitto-Community-Team | 0:e0bd5da25882 | 111 | case 3: |
group-Pokitto-Community-Team | 0:e0bd5da25882 | 112 | disp.println(" Triang");break; |
group-Pokitto-Community-Team | 0:e0bd5da25882 | 113 | case 4: |
group-Pokitto-Community-Team | 0:e0bd5da25882 | 114 | disp.println(" Noise");break; |
group-Pokitto-Community-Team | 0:e0bd5da25882 | 115 | case 5: |
group-Pokitto-Community-Team | 0:e0bd5da25882 | 116 | disp.println(" SqNois");break; |
group-Pokitto-Community-Team | 0:e0bd5da25882 | 117 | } |
Pokitto | 6:cd9679164a70 | 118 | disp.print("Vol:"); |
Pokitto | 6:cd9679164a70 | 119 | #ifndef POK_SIM |
Pokitto | 6:cd9679164a70 | 120 | disp.print((int)Pokitto::getHWvolume()); |
Pokitto | 6:cd9679164a70 | 121 | disp.print(", "); |
Pokitto | 6:cd9679164a70 | 122 | #endif // POK_SIM |
group-Pokitto-Community-Team | 0:e0bd5da25882 | 123 | disp.println((int)snd.getVolume()); |
group-Pokitto-Community-Team | 0:e0bd5da25882 | 124 | snd.playTone(1,tonefreq,amplitude,wavetype,arpmode); |
group-Pokitto-Community-Team | 0:e0bd5da25882 | 125 | oldvol = (int) snd.getVolume(); |
group-Pokitto-Community-Team | 0:e0bd5da25882 | 126 | // want to have 2 oscillators 1 octave apart ? |
Pokitto | 2:91c41852a646 | 127 | snd.playTone(2,tonefreq+12,amplitude,wavetype,arpmode); |
group-Pokitto-Community-Team | 0:e0bd5da25882 | 128 | } |
group-Pokitto-Community-Team | 0:e0bd5da25882 | 129 | } else changed--; |
group-Pokitto-Community-Team | 0:e0bd5da25882 | 130 | disp.color=0; |
group-Pokitto-Community-Team | 0:e0bd5da25882 | 131 | disp.drawColumn(sbx,38,disp.height); |
group-Pokitto-Community-Team | 0:e0bd5da25882 | 132 | disp.color=wavetype+1; |
group-Pokitto-Community-Team | 0:e0bd5da25882 | 133 | uint8_t tindex = sbindx*2; |
Pokitto | 2:91c41852a646 | 134 | int16_t y = linecenter + (((int)128-(soundbuf[tindex]))>>2); |
group-Pokitto-Community-Team | 0:e0bd5da25882 | 135 | disp.drawLine(sbx-1,prevy,sbx,y); |
group-Pokitto-Community-Team | 0:e0bd5da25882 | 136 | game.update(); |
group-Pokitto-Community-Team | 0:e0bd5da25882 | 137 | disp.color=0; |
group-Pokitto-Community-Team | 0:e0bd5da25882 | 138 | prevy=y; |
group-Pokitto-Community-Team | 0:e0bd5da25882 | 139 | sbx++;sbindx++; |
group-Pokitto-Community-Team | 0:e0bd5da25882 | 140 | if (sbx>disp.width-1) {sbx=0;} |
group-Pokitto-Community-Team | 0:e0bd5da25882 | 141 | } |
group-Pokitto-Community-Team | 0:e0bd5da25882 | 142 | return 0; // good manners! |
group-Pokitto-Community-Team | 0:e0bd5da25882 | 143 | } |