-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathaudio_test.c
89 lines (70 loc) · 2.14 KB
/
audio_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
#include <SDL/SDL_audio.h>
#include "sampler.h"
long last_count = 0;
float gfreq = 5000;
//long gsample_freq = 44100 / 2; //22050;
long gamp = 32767;
FiniteSequence gsequence;
Filter gfilter;
/* The audio function callback takes the following parameters:
stream: A pointer to the audio buffer to be filled
len: The length (in bytes) of the audio buffer
*/
void fill_audio(void *udata, Uint8 *stream, int len)
{
int nwords = len / 2;
int ii;
Sint16 *words = (Sint16*)stream;
for(ii = 0; ii < nwords; ++ii) {
words[ii] = SAMPLE(gfilter, ii + last_count);
}
last_count += nwords;
}
void sample_to_file(Sampler sampler, float sample_freq, float max, FILE* out) {
int ii = 0;
long samples = max * SAMPLE_FREQ;
Filter filter = lowpass_make(sampler, 2000, sample_freq);
for (ii = 0; ii < samples; ++ii) {
float t = (float)ii / SAMPLE_FREQ;
fprintf(out, "%f, %d, %d\n", t,
SAMPLE(sampler, ii),
SAMPLE(filter, ii));
}
}
int main(int argc, char ** argv) {
float cutoff = 2000;
if(argc > 1) {
cutoff = atoi(argv[1]);
}
if(argc > 2) {
gamp = atoi(argv[2]);
}
float freqs[] = {C_(1), D_(1), E_(1), E_(1),
C_(1), D_(1), E_(1),
C_(1), D_(1), E_(1), D_(1), C_(1), D_(1), C_(1)};
gsequence = make_sequence(freqs, array_size(freqs), gamp, 0.13);
gfilter = lowpass_make((Sampler)gsequence, cutoff, SAMPLE_FREQ);
FILE * seqfile = fopen("seq.csv", "w");
sample_to_file((Sampler)gsequence, SAMPLE_FREQ, 1.0, seqfile);
fclose(seqfile);
SDL_AudioSpec wanted;
// Set the audio format
wanted.freq = SAMPLE_FREQ;
wanted.format = AUDIO_S16SYS;
wanted.channels = 1; // 1 = mono, 2 = stereo
wanted.samples = 1024; // Good low-latency value for callback
//wanted.size = 4096;
wanted.padding = 0;
wanted.callback = fill_audio;
wanted.userdata = NULL;
// Open the audio device, forcing the desired format
if ( SDL_OpenAudio(&wanted, NULL) < 0 ) {
fprintf(stderr, "Couldn't open audio: %s\n", SDL_GetError());
return(-1);
}
SDL_PauseAudio(0);
SDL_Delay(2500);
RELEASE_SAMPLER(gfilter);
SDL_CloseAudio();
return 0;
}