99 lines
		
	
	
		
			2.4 KiB
		
	
	
	
		
			Plaintext
		
	
	
	
	
	
			
		
		
	
	
			99 lines
		
	
	
		
			2.4 KiB
		
	
	
	
		
			Plaintext
		
	
	
	
	
	
#include <SDL2/SDL.h>
 | 
						|
#include <stdio.h>
 | 
						|
#include <stdlib.h>
 | 
						|
 | 
						|
typedef struct {
 | 
						|
	Uint8* data;
 | 
						|
	int current_pos;
 | 
						|
	int max_length;
 | 
						|
} BufferType;
 | 
						|
 | 
						|
SDL_AudioDeviceID dev;
 | 
						|
 | 
						|
void audio_callback(void* userdata, Uint8* stream, int len)
 | 
						|
{
 | 
						|
	BufferType* buffer = (BufferType*)userdata;
 | 
						|
 | 
						|
	printf("callback: %u %u\n", *stream, len);
 | 
						|
	int space_left = buffer->max_length - buffer->current_pos;
 | 
						|
	if (space_left <= 0) {
 | 
						|
		// Buffer is full; stop the device
 | 
						|
		SDL_PauseAudioDevice(dev, 1);
 | 
						|
		printf("Buffer full, stopping recording.\n");
 | 
						|
		return;
 | 
						|
	}
 | 
						|
 | 
						|
	if (len > space_left) {
 | 
						|
		len = space_left;
 | 
						|
	}
 | 
						|
 | 
						|
	memcpy(buffer->data + buffer->current_pos, stream, len);
 | 
						|
	buffer->current_pos += len;
 | 
						|
}
 | 
						|
 | 
						|
int main(int argc, char* argv[])
 | 
						|
{
 | 
						|
	if (SDL_Init(SDL_INIT_AUDIO) < 0) {
 | 
						|
		fprintf(stderr, "Failed to initialize SDL: %s\n", SDL_GetError());
 | 
						|
		return 1;
 | 
						|
	}
 | 
						|
 | 
						|
	BufferType audio_buffer;
 | 
						|
	audio_buffer.max_length = 44100 * 5 * sizeof(float) * 2; // 5 seconds of stereo float audio
 | 
						|
	audio_buffer.data = (Uint8*)malloc(audio_buffer.max_length);
 | 
						|
	if (audio_buffer.data == NULL) {
 | 
						|
		fprintf(stderr, "Failed to allocate audio buffer\n");
 | 
						|
		SDL_Quit();
 | 
						|
		return 1;
 | 
						|
	}
 | 
						|
	audio_buffer.current_pos = 0;
 | 
						|
 | 
						|
	SDL_AudioSpec desired_spec, obtained_spec;
 | 
						|
	SDL_zero(desired_spec);
 | 
						|
	desired_spec.freq = 44100;
 | 
						|
	desired_spec.format = AUDIO_F32SYS;
 | 
						|
	desired_spec.channels = 2;
 | 
						|
	desired_spec.samples = 512;
 | 
						|
	desired_spec.callback = audio_callback;
 | 
						|
	desired_spec.userdata = &audio_buffer;
 | 
						|
 | 
						|
	dev = SDL_OpenAudioDevice(NULL, 1, &desired_spec, &obtained_spec, 0);
 | 
						|
	if (dev == 0) {
 | 
						|
		fprintf(stderr, "Failed to open audio device: %s\n", SDL_GetError());
 | 
						|
		free(audio_buffer.data);
 | 
						|
		SDL_Quit();
 | 
						|
		return 1;
 | 
						|
	}
 | 
						|
 | 
						|
	printf("Audio device opened successfully.\n");
 | 
						|
	printf("Obtained Spec:\n");
 | 
						|
	printf(" Frequency: %d\n", obtained_spec.freq);
 | 
						|
	printf(" Format: 0x%X\n", obtained_spec.format);
 | 
						|
	printf(" Channels: %d\n", obtained_spec.channels);
 | 
						|
	printf(" Samples: %d\n", obtained_spec.samples);
 | 
						|
 | 
						|
	SDL_PauseAudioDevice(dev, 0); // Start recording
 | 
						|
	printf("Recording started.\n");
 | 
						|
 | 
						|
	SDL_Event event;
 | 
						|
	while (audio_buffer.current_pos < audio_buffer.max_length) {
 | 
						|
		while (SDL_PollEvent(&event)) {
 | 
						|
			if (event.type == SDL_QUIT) {
 | 
						|
				printf("Quit event received.\n");
 | 
						|
				goto cleanup;
 | 
						|
			}
 | 
						|
		}
 | 
						|
		SDL_Delay(100);
 | 
						|
	}
 | 
						|
 | 
						|
	printf("Recording complete. Total bytes recorded: %d\n", audio_buffer.current_pos);
 | 
						|
 | 
						|
cleanup:
 | 
						|
	SDL_PauseAudioDevice(dev, 1); // Stop recording
 | 
						|
	SDL_CloseAudioDevice(dev);
 | 
						|
	free(audio_buffer.data);
 | 
						|
	SDL_Quit();
 | 
						|
 | 
						|
	return 0;
 | 
						|
}
 |