I am using SDL 2.23.6 and gcc 12.3. When I compile and link I only get a few warnings.
Quote:
warning: passing argument 3 of ‘SDL_OpenAudioDevice’ makes pointer from integer without a cast
for instance.
What I have tried:
#include <stdio.h>
#include <stdlib.h>
#include <SDL2/SDL.h>
#include <SDL2/SDL_audio.h>
#define SAMPLE_RATE 44100
#define CHANNELS 1
#define BUFFER_SIZE 512
int main(int argc, char *argv[]) {
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) < 0) {
printf("SDL could not be initialized: %s\n", SDL_GetError());
return 1;
}
SDL_Window *window = SDL_CreateWindow("Audio Spectrum", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 480, SDL_WINDOW_SHOWN);
SDL_Renderer *renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
SDL_AudioDeviceID device = SDL_OpenAudioDevice(NULL, 0, AUDIO_S16SYS, SAMPLE_RATE, BUFFER_SIZE);
if (device == 0) {
printf("Failed to open audio device: %s\n", SDL_GetError());
return 1;
}
short *buffer = malloc(BUFFER_SIZE * sizeof(short));
SDL_Texture *texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGB24, SDL_TEXTUREACCESS_TARGET, 640, 480);
while (1) {
SDL_AudioStreamGet(device, buffer, BUFFER_SIZE);
int i, j;
for (i = 0; i < BUFFER_SIZE; i++) {
int sample = buffer[i];
for (j = 0; j < SAMPLE_RATE/256; j++) {
if (sample >= j * 256 / SAMPLE_RATE) {
SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255);
SDL_RenderDrawPoint(renderer, i, j);
}
}
}
SDL_RenderClear(renderer);
SDL_RenderCopy(renderer, texture, NULL, NULL);
SDL_RenderPresent(renderer);
SDL_Delay(10);
}
SDL_DestroyTexture(texture);
SDL_Free(buffer);
SDL_CloseAudioDevice(device);
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}