sound library  v0.50.0
Create sound waves and control their volume, frequency, and shape, and mix up to 4 channels
sound.h
Go to the documentation of this file.
1 
17  //
18 #ifndef __SOUND_H
19 #define __SOUND_H
20 
21 #ifdef __cplusplus
22 extern "C"
23 {
24 #endif
25 
26 /*
27 #define PIEZO 3
28 #define AUDIO 0
29 */
30 
31 #ifndef DOXYGEN_SHOULD_SKIP_THIS
32 
33  #define SAMPLES 512
34  #define PERIOD 80_000_000 / 40_000
35  #define OSCILLATORS 4
36 
37  #define _SAMPLE 5
38  #define _ENV 0
39  #define _ATK 1
40  #define _DEC 2
41  #define _SUS 3
42  #define _REL 4
43  #define _WAV 5
44  #define _O 0
45  #define _A 1
46  #define _D 2
47  #define _S 3
48  #define _R 4
49 
50 #endif // DOXYGEN_SHOULD_SKIP_THIS
51 
52  //
53 #define SQUARE 0
54 #define SAW 1
55 #define TRIANGLE 2
56 #define SINE 3
57 #define NOISE 4
58 #define INC_HZ 52.4588
59 
60 #ifndef MUSIC_NOTES
61 #define MUSIC_NOTES
62 
63 #define REST (float) 255
64 #define END (float) 254
65 #define BEAT_VAL (float) 253
66 #define TEMPO (float) 252
67 #define HOLDS (float) 251
68 #define CH0 (float) 250
69 #define CH1 (float) 249
70 #define CH2 (float) 248
71 #define CH3 (float) 247
72 #define HOLD0 (float) 246
73 #define HOLD1 (float) 245
74 #define HOLD2 (float) 244
75 #define HOLD3 (float) 243
76 
77 
78 #define C0 (float) 0
79 #define D0b (float) 1
80 #define D0 (float) 2
81 #define E0b (float) 3
82 #define E0 (float) 4
83 #define F0 (float) 5
84 #define G0b (float) 6
85 #define G0 (float) 7
86 #define A0b (float) 8
87 #define A0 (float) 9
88 #define B0b (float) 10
89 #define B0 (float) 11
90 
91 
92 #define C1 (float) 12
93 #define D1b (float) 13
94 #define D1 (float) 14
95 #define E1b (float) 15
96 #define E1 (float) 16
97 #define F1 (float) 17
98 #define G1b (float) 18
99 #define G1 (float) 19
100 #define A1b (float) 20
101 #define A1 (float) 21
102 #define B1b (float) 22
103 #define B1 (float) 23
104 
105 
106 #define C2 (float) 24
107 #define D2b (float) 25
108 #define D2 (float) 26
109 #define E2b (float) 27
110 #define E2 (float) 28
111 #define F2 (float) 29
112 #define G2b (float) 30
113 #define G2 (float) 31
114 #define A2b (float) 32
115 #define A2 (float) 33
116 #define B2b (float) 34
117 #define B2 (float) 35
118 
119 
120 #define C3 (float) 36
121 #define D3b (float) 37
122 #define D3 (float) 38
123 #define E3b (float) 39
124 #define E3 (float) 40
125 #define F3 (float) 41
126 #define G3b (float) 42
127 #define G3 (float) 43
128 #define A3b (float) 44
129 #define A3 (float) 45
130 #define B3b (float) 46
131 #define B3 (float) 47
132 
133 
134 #define C4 (float) 48
135 #define D4b (float) 49
136 #define D4 (float) 50
137 #define E4b (float) 51
138 #define E4 (float) 52
139 #define F4 (float) 53
140 #define G4b (float) 54
141 #define G4 (float) 55
142 #define A4b (float) 56
143 #define A4 (float) 57
144 #define B4b (float) 58
145 #define B4 (float) 59
146 
147 
148 #define C5 (float) 60
149 #define D5b (float) 61
150 #define D5 (float) 62
151 #define E5b (float) 63
152 #define E5 (float) 64
153 #define F5 (float) 65
154 #define G5b (float) 66
155 #define G5 (float) 67
156 #define A5b (float) 68
157 #define A5 (float) 69
158 #define B5b (float) 70
159 #define B5 (float) 71
160 
161 
162 #define C6 (float) 72
163 #define D6b (float) 73
164 #define D6 (float) 74
165 #define E6b (float) 75
166 #define E6 (float) 76
167 #define F6 (float) 77
168 #define G6b (float) 78
169 #define G6 (float) 79
170 #define A6b (float) 80
171 #define A6 (float) 81
172 #define B6b (float) 82
173 #define B6 (float) 83
174 
175 
176 #define C7 (float) 84
177 #define D7b (float) 85
178 #define D7 (float) 86
179 #define E7b (float) 87
180 #define E7 (float) 88
181 #define F7 (float) 89
182 #define G7b (float) 90
183 #define G7 (float) 91
184 #define A7b (float) 92
185 #define A7 (float) 93
186 #define B7b (float) 94
187 #define B7 (float) 95
188 
189 
190 #define C8 (float) 96
191 #define D8b (float) 97
192 #define D8 (float) 98
193 #define E8b (float) 99
194 #define E8 (float) 100
195 #define F8 (float) 101
196 #define G8b (float) 102
197 #define G8 (float) 103
198 #define A8b (float) 104
199 #define A8 (float) 105
200 #define B8b (float) 106
201 #define B8 (float) 107
202 
203 #endif // MUSIC_NOTES
204 
205 
206 #ifndef DOXYGEN_SHOULD_SKIP_THIS
207 
208  typedef struct audiosynthpasm_struct
209  {
210  volatile int osc_sample;
211  volatile int osc_envelope;
212  volatile int osc_attack;
213  volatile int osc_decay;
214  volatile int osc_sustain;
215  volatile int osc_release;
216  volatile int osc_waveform;
217  volatile int osc_state;
218  volatile int osc_target[4]; // = {0, 0, 0, 0};
219  volatile int osc_vol[4]; // = {(127<<12),(127<<12),(127<<12),(127<<12)};
220  volatile int osc_inc[4]; // = {0, 0, 0, 0};
221  volatile int osc_acc[4]; // = {0, 0, 0, 0};
222  volatile int freqtable[12]; // ={ 439638, 465780, 493477, 522820, 553909, 586846,
223  // 621742, 658713, 697882, 739380, 783346, 829926};
224  volatile int ctraval;
225  volatile int pinmask;
226  volatile int divVal;
227  int cog;
228  } sound_t;
229 
230 #endif // DOXYGEN_SHOULD_SKIP_THIS
231 
232 typedef sound_t sound;
233 
234 
248 sound_t *sound_run(int pin, int npin);
249 
250 
257 void sound_end(sound_t *device);
258 
269 void sound_volume(sound_t *device, int channel, int volume);
270 
281 void sound_note(sound_t *device, int channel, int note);
282 
293 void sound_wave(sound_t *device, int channel, int wave);
294 
308 void sound_freq(sound_t *device, int channel, int freq);
309 
310 
311 #ifndef DOXYGEN_SHOULD_SKIP_THIS
312 
313  void sound_freqRaw(sound_t *device, int channel, int value);
314 
315  void sound_adsr(sound_t *device, int channel, int attack, int decay,
316  int sustain, int release);
317  void sound_envelopeSet(sound_t *device, int channel, int value);
318  void sound_envelopeStart(sound_t *device, int channel, int enable);
319 
320  void sound_playChords(sound_t *device, float *chords);
321 
322  void replace_byte(int *address, int intOffset, int byteOffset, int newVal);
323  void sound_param(sound_t *device, int type, int channel, int value);
324  void sound_loadPatch(sound_t *device, int *patchAddr);
325  void sound_sampleSet(sound_t *device, int value);
326 
327  void sound_playSound(sound_t *device, int channel, int value);
328  void sound_endSound(sound_t *device, int channel);
329  void sound_endAllSound(sound_t *device);
330 
331 #endif // DOXYGEN_SHOULD_SKIP_THIS
332 
333 #ifdef __cplusplus
334 }
335 #endif
336 /* __cplusplus */
337 #endif
338 /* __SOUND_H */
339 
340 
341 /*
342  TERMS OF USE: MIT License
343 
344  Permission is hereby granted, free of charge, to any person obtaining a
345  copy of this software and associated documentation files (the "Software"),
346  to deal in the Software without restriction, including without limitation
347  the rights to use, copy, modify, merge, publish, distribute, sublicense,
348  and/or sell copies of the Software, and to permit persons to whom the
349  Software is furnished to do so, subject to the following conditions:
350 
351  The above copyright notice and this permission notice shall be included in
352  all copies or substantial portions of the Software.
353 
354  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
355  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
356  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
357  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
358  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
359  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
360  DEALINGS IN THE SOFTWARE.
361 */
362