SDL2でマイクを使う
code:microphone.c
void
callback(void* userdata, Uint8* stream, int len)
{
printf("len: %d, stream0: %d\n", len, stream0); }
int
main(void)
{
if (SDL_Init(SDL_INIT_AUDIO) < -1) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s", SDL_GetError());
return 1;
}
SDL_SetHintWithPriority(SDL_HINT_AUDIO_RESAMPLING_MODE, "medium", SDL_HINT_OVERRIDE);
SDL_AudioSpec want, have;
SDL_zero(want);
SDL_zero(have);
want.freq = 16000;
want.format = AUDIO_F32;
want.channels = 1;
want.samples = 1024;
want.callback = callback;
want.userdata = NULL;
SDL_AudioDeviceID device_id = SDL_OpenAudioDevice(NULL, SDL_TRUE, &want, &have, 0);
if (device_id == 0) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't open audio: %s", SDL_GetError());
return 1;
}
SDL_PauseAudioDevice(device_id, 0);
SDL_Event event;
while (1) {
while (SDL_PollEvent(&event)) {
if (event.type == SDL_QUIT) {
SDL_CloseAudioDevice(device_id);
SDL_Quit();
}
}
SDL_Delay(1000);
printf("%d: %d\n", __LINE__, have.samples);
}
return 0;
}
少なくともmacOS 15.1.1, Apple M2チップでは動いた。