-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
115 lines (84 loc) · 2.16 KB
/
main.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
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <unistd.h>
#include <SDL2/SDL.h>
#define PI_2 6.283185307f
float sin_step = 0;
float sin_pos = 0;
float frequency;
float volume = 25000;
float duration;
void print_help()
{
printf( "tone_generator [-f frequency] [-d duration]\n");
printf( "frequency : in Hz0\n");
printf( "duration : in seconds\n ");
}
void sin_generator( void* data, Uint8* stream, int bytes_count )
{
sin_step = 1.f / 48000.f;
int16_t* samples = (int16_t*) stream;
int samples_count = bytes_count / 2;
for( int i = 0; i < samples_count; ++i )
{
// https://en.wikipedia.org/wiki/Radian_per_second
samples[i] = volume * sin( sin_pos * PI_2 * frequency );
sin_pos += sin_step;
}
}
int main( int argc, char** argv )
{
int r;
frequency = 0;
duration = 0;
while( (r = getopt(argc, argv, "d:f:h")) != -1 )
{
switch( r )
{
case 'd':
duration = atoi( optarg );
break;
case 'f':
frequency = atof( optarg );
break;
case 'v':
volume = atoi( optarg );
break;
case 'h':
default:
print_help();
return 0;
}
}
if( (0 == frequency) || (0 == duration) )
{
print_help();
return 0;
}
r = SDL_Init( SDL_INIT_AUDIO );
if( r != 0 )
{
fprintf( stderr, "Unable to initialize SDL\n");
return -1;
}
SDL_AudioSpec specs[2];
memset( specs, 0, sizeof(SDL_AudioSpec) * 2 );
specs[0].freq = 48000 ;
specs[0].format = AUDIO_S16;
specs[0].channels = 1;
specs[0].samples = 4096;
specs[0].callback = sin_generator;
SDL_AudioDeviceID dev;
dev = SDL_OpenAudioDevice( NULL, 0, &specs[0], &specs[1], 0 );
if( dev == 0 )
{
fprintf( stderr, "Unable to open audio device\n" );
return -1;
}
SDL_PauseAudioDevice( dev, 0 );
SDL_Delay( duration * 1000. );
SDL_CloseAudioDevice( dev );
SDL_Quit();
return 0;
}