sdl-audio/example-test-code/main.c.bak1

65 lines
1.5 KiB
Plaintext
Raw Normal View History

2024-10-10 20:11:57 +08:00
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <SDL2/SDL_ttf.h>
#include <stdio.h>
#include "log.h"
SDL_AudioSpec* desiredSpec;
void audio_callback(void* userdata, Uint8* stream, int len)
{
// Fill the audio buffer with data
char string[64];
sprintf(string, "stream: %u len: %u\n", stream, len);
printf(string, "stream: %u len: %u\n", stream, len);
log_message(LOG_INFO, string);
}
int main(int argc, char* argv[])
{
if (SDL_Init(SDL_INIT_AUDIO) < 0) {
// Handle error
}
desiredSpec = malloc(sizeof(SDL_AudioSpec));
memset(desiredSpec, 0, sizeof(SDL_AudioSpec));
desiredSpec->freq = 44100; // Sample rate
desiredSpec->format = AUDIO_F32; // Audio format
desiredSpec->channels = 2;
desiredSpec->samples = 4096; // Buffer size
desiredSpec->callback = audio_callback; // Set the callback
SDL_AudioDeviceID audioDevice = SDL_OpenAudioDevice(SDL_GetAudioDeviceName(2, 1), 1, desiredSpec, NULL, SDL_AUDIO_ALLOW_FORMAT_CHANGE);
if (SDL_GetError()[0] != '\0') {
printf("SDL Error : %s\n", SDL_GetError());
}
printf("Name: %s\n", SDL_GetAudioDeviceName(2, 1));
if (audioDevice == 0) {
// Handle error
}
// Start audio playback
SDL_PauseAudioDevice(audioDevice, 0);
if (SDL_GetError()[0] != '\0') {
printf("SDL Error : %s\n", SDL_GetError());
}
SDL_Event event;
int running = 1;
while (running) {
while (SDL_PollEvent(&event)) {
SDL_Keycode keysym = event.key.keysym.sym;
switch (event.type) {
case SDL_QUIT:
running = 0;
break;
}
}
}
SDL_CloseAudioDevice(audioDevice);
SDL_Quit();
return 0;
}