Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/tcc -run -I/usr/include/SDL2 -lSDL2
- #include <math.h>
- #include <stdlib.h>
- #include <unistd.h>
- #define SDL_DISABLE_IMMINTRIN_H
- #include <SDL.h>
- #include <SDL_audio.h>
- int AMPLITUDE = 28000;
- int SAMPLE_RATE = 44100;
- void audio_callback(void *user_data, Uint8 *raw_buffer, int bytes)
- {
- Sint16 *buffer = (Sint16*)raw_buffer;
- int length = bytes / 2;
- int *sample_nr = (int*)user_data;
- if (sample_nr == NULL)
- return;
- for (int i = 0; i < length; ++i, ++*sample_nr)
- {
- double time = (double)*sample_nr / (double)SAMPLE_RATE;
- buffer[i] = (Sint16)(AMPLITUDE * sin(2.0f * M_PI * 441.0f * time));
- }
- }
- int main(int argc, char *argv[])
- {
- int c;
- while ((c = getopt(argc, argv, "A:S:")) != -1)
- {
- switch (c)
- {
- case 'A': AMPLITUDE = atoi(optarg); break;
- case 'S': SAMPLE_RATE = atoi(optarg); break;
- default: break;
- }
- }
- if (SDL_Init(SDL_INIT_AUDIO) != 0)
- SDL_Log("Failed to initialize SDL: %s", SDL_GetError());
- int sample_nr = 0;
- SDL_AudioSpec want;
- want.freq = SAMPLE_RATE;
- want.format = AUDIO_S16SYS;
- want.channels = 1;
- want.samples = 2048;
- want.callback = audio_callback;
- want.userdata = &sample_nr;
- SDL_AudioSpec have;
- if (SDL_OpenAudio(&want, &have) != 0)
- SDL_LogError(SDL_LOG_CATEGORY_AUDIO, "Failed to open audio: %s", SDL_GetError());
- if (want.format != have.format)
- SDL_LogError(SDL_LOG_CATEGORY_AUDIO, "Failed to get the desired AudioSpec");
- SDL_PauseAudio(0);
- SDL_Delay(1000);
- SDL_PauseAudio(1);
- SDL_CloseAudio();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment