Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <SDL2/SDL.h>
- #include <math.h>
- #define PI 3.14159265358979323846
- #define BufferSizeInSamples 2048
- #define BytesPerSample 2 // 16 bit PCM
- #define BufferSizeInBytes (BufferSizeInSamples * BytesPerSample)
- #define SampleRate 44100 // Hz
- #define Frequence 1000 // Hz
- #define Volume 0.1 // 0..1
- #define x (Frequence * 2000 / SampleRate)
- int counter = 0;
- void audioCallback(void *userdata, Uint8 *stream, int len) {
- short buffer[BufferSizeInSamples];
- int i;
- for(i = 0; i < BufferSizeInSamples; i++)
- buffer[i] = (short)(sin((float)((counter + i) % x) / x * 2 * PI) * 32767);
- SDL_MixAudio(stream, (const Uint8 *)buffer, BufferSizeInBytes, SDL_MIX_MAXVOLUME * Volume);
- counter = counter + BufferSizeInSamples;
- }
- int main(int argc, char* argv[]) {
- if(SDL_Init(SDL_INIT_AUDIO) < 0) {
- printf("Init() failed");
- return -1;
- }
- SDL_AudioSpec audioSpec;
- audioSpec.freq = SampleRate;
- audioSpec.format = AUDIO_S16;
- audioSpec.channels = 1;
- audioSpec.samples = BufferSizeInBytes;
- audioSpec.callback = audioCallback;
- audioSpec.userdata = NULL;
- if(SDL_OpenAudio(&audioSpec, NULL) < 0 ) {
- printf("OpenAudio() failed");
- return -1;
- }
- SDL_PauseAudio(0);
- // run program for 2 seconds
- int t = 2000;
- while(t > 0) {
- SDL_Delay(100);
- t = t - 100;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment