forked from sacchy777/mid2wav
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwtssynth_test.c
179 lines (152 loc) · 5.48 KB
/
wtssynth_test.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/*
* Test of
* Wable Table Synthesizer / Noise Rhythm synthesizer
*
* wtssynth_test.c
*
*
* Copyright (c) 2013 sada.gussy (sada dot gussy at gmail dot com)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
*/
#include "wtssynth.h"
#include "nrsynth.h"
#include <assert.h>
#include "freq_table.h"
#include "audiobuf.h"
#include <stdio.h>
#include <stdlib.h>
#include "sdelay.h"
#include "midievent.h"
/*
*
*
* gcc wtssynth_test.c wtssynth.c freq_table.c audiobuf.c xor128.c nrsynth.c
*
*
*/
#define BUFSIZE 44100 * 8 /* 8sec buffer */
#define TEMPFILE "test.wav"
#define VELOCITY 1.0
int g_current_time = 0;
/*---------------------------------------------------
* plays seventh chord.
*---------------------------------------------------*/
void seventh(wtssynth_t *w, audiobuf_t *a, int key1, int key2, int key3, int key4, int len){
/* play a chord */
wtssynth_keyon(w, key1, VELOCITY);
wtssynth_keyon(w, key2, VELOCITY);
wtssynth_keyon(w, key3, VELOCITY);
wtssynth_keyon(w, key4, VELOCITY);
/* render audio data in a specified length */
wtssynth_render(w, a, g_current_time, len);
g_current_time += len;
/* release a chord */
wtssynth_keyoff(w, key1);
wtssynth_keyoff(w, key2);
wtssynth_keyoff(w, key3);
wtssynth_keyoff(w, key4);
}
void seventh_midi(wtssynth_t *w, audiobuf_t *a, int key1, int key2, int key3, int key4, int len){
midievent_t *e1, *e2, *e3, *e4, *e5, *e6, *e7, *e8;
e1 = midievent_create_short(0, 0, MIDIEVENT_TYPE_NOTEON, key1, 100, 0, 0);
e2 = midievent_create_short(0, 0, MIDIEVENT_TYPE_NOTEON, key2, 100, 0, 0);
e3 = midievent_create_short(0, 0, MIDIEVENT_TYPE_NOTEON, key3, 100, 0, 0);
e4 = midievent_create_short(0, 0, MIDIEVENT_TYPE_NOTEON, key4, 100, 0, 0);
e5 = midievent_create_short(0, 0, MIDIEVENT_TYPE_NOTEOFF, key1, 0, 0, 0);
e6 = midievent_create_short(0, 0, MIDIEVENT_TYPE_NOTEOFF, key2, 0, 0, 0);
e7 = midievent_create_short(0, 0, MIDIEVENT_TYPE_NOTEOFF, key3, 0, 0, 0);
e8 = midievent_create_short(0, 0, MIDIEVENT_TYPE_NOTEOFF, key4, 0, 0, 0);
/* play a chord */
wtssynth_midi(w, e1);
wtssynth_midi(w, e2);
wtssynth_midi(w, e3);
wtssynth_midi(w, e4);
/* render audio data in a specified length */
wtssynth_render(w, a, g_current_time, len);
g_current_time += len;
/* release a chord */
wtssynth_midi(w, e5);
wtssynth_midi(w, e6);
wtssynth_midi(w, e7);
wtssynth_midi(w, e8);
midievent_destroy(e1);
midievent_destroy(e2);
midievent_destroy(e3);
midievent_destroy(e4);
midievent_destroy(e5);
midievent_destroy(e6);
midievent_destroy(e7);
midievent_destroy(e8);
}
/*---------------------------------------------------
* program main
*---------------------------------------------------*/
int main(int argc, char *argv[]){
int i;
wtssynth_t *w;
audiobuf_t *a;
nrsynth_t *n;
sdelay_t *s;
/* 1sec length, equal to 2 quater notes length in BPM 120 at sampling rate 44100 */
int delta = 44100;
w = wtssynth_create();
assert(w != NULL);
a = audiobuf_create_default(BUFSIZE);
assert(a != NULL);
n = nrsynth_create();
assert(n != NULL);
s = sdelay_create();
assert(s != NULL);
wtsconfig_programchange(w->config, 8);
/*** chord rendering ***/
g_current_time = 0; /* global variable */
seventh_midi(w, a, key_C4, key_E5, key_G5, key_B5, delta); /* CMaj7 */
seventh_midi(w, a, key_D4, key_F5, key_A5, key_C6, delta); /* Dm7 */
seventh_midi(w, a, key_E4, key_G5, key_B5, key_D6, delta); /* Em7 */
seventh_midi(w, a, key_Dp4, key_G5, key_Ap5, key_D6, delta); /* EbMaj7 */
seventh_midi(w, a, key_D4, key_F5, key_A5, key_C6, delta*2); /* Dm7 */
seventh_midi(w, a, key_Cp4, key_F5, key_Gp5, key_C6, delta*2); /* DbMaj7 */
/*** rythm rendering ***/
/* renders kick and snare */
g_current_time = 0;
for(i = 0; i < 16; i++){
nrsynth_play(n, i%2 == 0 ? 35 : 38, 1.0); /* 35=kick, 38=snare. see nrsynth_init() */
nrsynth_render(n, a, g_current_time, BUFSIZE/16);
g_current_time += BUFSIZE/16;
}
/* renders hihats */
g_current_time = 0;
for(i = 0; i < 32; i++){
nrsynth_play(n, i%4 == 2 ? 46 : 42, 1.0); /* 42=closed HH, 46=open HH. see nrsynth_init() */
nrsynth_render(n, a, g_current_time, BUFSIZE/32);
g_current_time += BUFSIZE/32;
}
/*render reverb for all buffer */
sdelay_set_drywet(s, 0.8, 0.5);
sdelay_render(s, a, 0, BUFSIZE);
/* write rendered audio data to a file in PCM format */
audiobuf_save(a, TEMPFILE);
sdelay_destroy(s);
nrsynth_destroy(n);
audiobuf_destroy(a);
wtssynth_destroy(w);
return EXIT_SUCCESS;
}