1 // Copyright 2013 Olivier Gillet.
2 //
3 // Author: Olivier Gillet (ol.gillet@gmail.com)
4 //
5 // This program is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 // GNU General Public License for more details.
13 // You should have received a copy of the GNU General Public License
14 // along with this program.  If not, see <http://www.gnu.org/licenses/>.
15 
16 #include <cmath>
17 #include <cstdio>
18 #include <cstring>
19 #include <cstdlib>
20 
21 #include "peaks/processors.h"
22 
23 using namespace peaks;
24 using namespace stmlib;
25 
26 const uint32_t kSampleRate = 48000;
27 
write_wav_header(FILE * fp,int num_samples,int num_channels)28 void write_wav_header(FILE* fp, int num_samples, int num_channels) {
29   uint32_t l;
30   uint16_t s;
31 
32   fwrite("RIFF", 4, 1, fp);
33   l = 36 + num_samples * 2 * num_channels;
34   fwrite(&l, 4, 1, fp);
35   fwrite("WAVE", 4, 1, fp);
36 
37   fwrite("fmt ", 4, 1, fp);
38   l = 16;
39   fwrite(&l, 4, 1, fp);
40   s = 1;
41   fwrite(&s, 2, 1, fp);
42   s = num_channels;
43   fwrite(&s, 2, 1, fp);
44   l = kSampleRate;
45   fwrite(&l, 4, 1, fp);
46   l = static_cast<uint32_t>(kSampleRate) * 2 * num_channels;
47   fwrite(&l, 4, 1, fp);
48   s = 2 * num_channels;
49   fwrite(&s, 2, 1, fp);
50   s = 16;
51   fwrite(&s, 2, 1, fp);
52 
53   fwrite("data", 4, 1, fp);
54   l = num_samples * 2 * num_channels;
55   fwrite(&l, 4, 1, fp);
56 }
57 
main(void)58 int main(void) {
59   FILE* fp = fopen("peaks.wav", "wb");
60   write_wav_header(fp, kSampleRate * 10, 1);
61   processors[0].Init(1);
62   processors[0].set_control_mode(CONTROL_MODE_HALF);
63   processors[0].set_function(PROCESSOR_FUNCTION_BASS_DRUM);
64   processors[0].set_parameter(0, 60000);
65   processors[0].set_parameter(1, 40000);
66 
67   uint32_t period = kSampleRate / 2;
68   for (uint32_t i = 0; i < kSampleRate * 10 ; ++i) {
69     uint16_t tri = i;
70     tri = tri > 32767 ? 65535 - tri : tri;
71     uint8_t control = 0;
72     if (i % period < (period / 4)) {
73       control |= CONTROL_GATE;
74     }
75     if (i % period == 0) {
76       control |= CONTROL_GATE_RISING;
77     }
78     int16_t s = processors[0].Process(control);
79     fwrite(&s, sizeof(s), 1, fp);
80     processors[0].Buffer();
81   }
82 }
83