Commit | Line | Data |
---|---|---|
21c4e167 H |
1 | #include <stdio.h> |
2 | #include <stdlib.h> | |
3 | #include <unistd.h> | |
4 | #include <math.h> | |
5 | #include "SDL.h" | |
6 | #include "SDL_mixer.h" | |
7 | ||
8 | #define PI 3.1415 | |
9 | #define deg2rad(a) (((a)/180)*PI) | |
10 | ||
11 | ||
12 | SDL_AudioSpec *desired, *obtained; | |
13 | SDL_AudioSpec *hardware_spec; | |
14 | ||
15 | ||
16 | void my_audio_callback(void *userdata, Uint8 *stream, int len) { | |
17 | int sampl_freq = desired->freq; | |
18 | sampl_freq = 44100; | |
19 | ||
20 | double sine_freq = 50; | |
21 | static double i,j; // = 0; | |
22 | ||
23 | short int *u; u = (short int *)stream; | |
24 | //for(;len>0;len--) *(u++) = random(); | |
25 | while( (void *)u<(void *)(stream+len) ) { | |
26 | // *(u++) = 0; | |
27 | *(u++) = sin( | |
28 | i += (2*PI*sine_freq)/sampl_freq | |
29 | )*128000; | |
30 | // *(u++) = sin(deg2rad(j+=8))*1280000; | |
31 | // *(u++) = 0; | |
32 | *(u++) = *(u-1); | |
33 | } | |
34 | //if(i>=2*PI) i=0; | |
35 | //if(j>=2*PI) j=0; | |
36 | } | |
37 | ||
38 | ||
39 | int main(int argc, char *argv[]) { | |
40 | ||
41 | /* Allocate a desired SDL_AudioSpec */ | |
42 | desired=(SDL_AudioSpec *)malloc(sizeof(SDL_AudioSpec)); | |
43 | ||
44 | /* Allocate space for the obtained SDL_AudioSpec */ | |
45 | obtained=(SDL_AudioSpec *)malloc(sizeof(SDL_AudioSpec)); | |
46 | ||
47 | /* 22050Hz - FM Radio quality */ | |
48 | desired->freq=22050; | |
49 | desired->freq=44100; | |
50 | ||
51 | /* 16-bit signed audio */ | |
52 | desired->format=AUDIO_S16LSB; | |
53 | //desired->format=AUDIO_S8; | |
54 | ||
55 | desired->channels=2; //0,1,2 = autodetect,mono,stereo | |
56 | ||
57 | /* Large audio buffer reduces risk of dropouts but increases response time */ | |
58 | desired->samples=65530; | |
59 | ||
60 | /* Our callback function */ | |
61 | desired->callback=my_audio_callback; | |
62 | ||
63 | desired->userdata=NULL; | |
64 | desired->userdata=&desired->freq; | |
65 | ||
66 | /* Open the audio device */ | |
67 | if ( SDL_OpenAudio(desired, obtained) < 0 ){ | |
68 | fprintf(stderr, "Couldn't open audio: %s", SDL_GetError()); | |
69 | exit(-1); | |
70 | } | |
71 | /* desired spec is no longer needed */ | |
72 | free(desired); | |
73 | hardware_spec=obtained; | |
74 | ||
75 | /* Prepare callback for playing */ | |
76 | ||
77 | /* Start playing */ | |
78 | SDL_PauseAudio(0); | |
79 | sleep(100); | |
80 | } |