Click here to Skip to main content
15,887,365 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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[]) {
  // Initialize SDL.
  if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) < 0) {
    printf("SDL could not be initialized: %s\n", SDL_GetError());
    return 1;
  }

  // Create a window and renderer.
  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);

  // Create an audio device.
  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;
  }

  // Allocate memory for the audio buffer.
  short *buffer = malloc(BUFFER_SIZE * sizeof(short));

  // Create a texture to render the audio spectrum.
  SDL_Texture *texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGB24, SDL_TEXTUREACCESS_TARGET, 640, 480);

  // Main loop.
  while (1) {
    // Get the audio samples from the device.
    SDL_AudioStreamGet(device, buffer, BUFFER_SIZE);

    // Calculate the audio spectrum.
    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) {
          // Set the pixel at (i, j) to red.
          SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255);
          SDL_RenderDrawPoint(renderer, i, j);
        }
      }
    }

    // Render the audio spectrum.
    SDL_RenderClear(renderer);
    SDL_RenderCopy(renderer, texture, NULL, NULL);
    SDL_RenderPresent(renderer);

    // Sleep for a millisecond.
    SDL_Delay(10);
  }

  // Free resources.
  SDL_DestroyTexture(texture);
  SDL_Free(buffer);
  SDL_CloseAudioDevice(device);
  SDL_DestroyRenderer(renderer);
  SDL_DestroyWindow(window);
  SDL_Quit();

  return 0;
}
Posted

At the very least, you should identify at WHICH line your program segfaults.
Step through it with a debugger and if you get at which line it does that, you more than likely will be able to tell why.

Also you really should understand why you get those warnings, and take appropriate action to make sure there are no warning.
 
Share this answer
 
You are using invalid parameters as input to SDL2/SDL_OpenAudioDevice - SDL Wiki[^]. So as soon as the library module tries to access via what should be a pointer, it will cause the seg fault.
 
Share this answer
 
v2
Comments
CPallini 18-Dec-23 8:06am    
5.
Richard MacCutchan 18-Dec-23 8:11am    
Thanks Carlo. I sometimes think that "RTFM" could be the answer to many questions. :)
CPallini 18-Dec-23 8:41am    
You are not alone.
Richard C. Andrews 18-Dec-23 10:45am    
Oh, be nice. I was having a hard time and looked for help.
Richard MacCutchan 18-Dec-23 10:51am    
And you got it. But Bruno's advice is also relevant. If you get errors or warnings in your compilations then you need to investigate them at the very least. Don't just assume you can ignore them.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900