Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7778037
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T18:17:47+00:00 2026-06-01T18:17:47+00:00

i am having problems understanding how the audio part of the sdl library works

  • 0

i am having problems understanding how the audio part of the sdl library works
now, i know that when you initialize it, you have to specify the frequency and a >>callback<< function, which i think is then called automatically at the given frequency.
can anyone who worked with the sdl library write a simple example that would use sdl_audio to generate a 440 hz square wave (since it is the simplest waveform) at a sampling frequency of 44000 hz?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T18:17:48+00:00Added an answer on June 1, 2026 at 6:17 pm

    The Introduction to SDL (2011 cached version: 2) has got a neat example of using SDL Sound library that should get you started: http://www.libsdl.org/intro.en/usingsound.html

    EDIT: Here is a working program that does what you asked for. I modified a bit the code found here: http://www.dgames.org/beep-sound-with-sdl/

    #include <SDL/SDL.h>
    #include <SDL/SDL_audio.h>
    #include <queue>
    #include <cmath>
    
    const int AMPLITUDE = 28000;
    const int FREQUENCY = 44100;
    
    struct BeepObject
    {
        double freq;
        int samplesLeft;
    };
    
    class Beeper
    {
    private:
        double v;
        std::queue<BeepObject> beeps;
    public:
        Beeper();
        ~Beeper();
        void beep(double freq, int duration);
        void generateSamples(Sint16 *stream, int length);
        void wait();
    };
    
    void audio_callback(void*, Uint8*, int);
    
    Beeper::Beeper()
    {
        SDL_AudioSpec desiredSpec;
    
        desiredSpec.freq = FREQUENCY;
        desiredSpec.format = AUDIO_S16SYS;
        desiredSpec.channels = 1;
        desiredSpec.samples = 2048;
        desiredSpec.callback = audio_callback;
        desiredSpec.userdata = this;
    
        SDL_AudioSpec obtainedSpec;
    
        // you might want to look for errors here
        SDL_OpenAudio(&desiredSpec, &obtainedSpec);
    
        // start play audio
        SDL_PauseAudio(0);
    }
    
    Beeper::~Beeper()
    {
        SDL_CloseAudio();
    }
    
    void Beeper::generateSamples(Sint16 *stream, int length)
    {
        int i = 0;
        while (i < length) {
    
            if (beeps.empty()) {
                while (i < length) {
                    stream[i] = 0;
                    i++;
                }
                return;
            }
            BeepObject& bo = beeps.front();
    
            int samplesToDo = std::min(i + bo.samplesLeft, length);
            bo.samplesLeft -= samplesToDo - i;
    
            while (i < samplesToDo) {
                stream[i] = AMPLITUDE * std::sin(v * 2 * M_PI / FREQUENCY);
                i++;
                v += bo.freq;
            }
    
            if (bo.samplesLeft == 0) {
                beeps.pop();
            }
        }
    }
    
    void Beeper::beep(double freq, int duration)
    {
        BeepObject bo;
        bo.freq = freq;
        bo.samplesLeft = duration * FREQUENCY / 1000;
    
        SDL_LockAudio();
        beeps.push(bo);
        SDL_UnlockAudio();
    }
    
    void Beeper::wait()
    {
        int size;
        do {
            SDL_Delay(20);
            SDL_LockAudio();
            size = beeps.size();
            SDL_UnlockAudio();
        } while (size > 0);
    
    }
    
    void audio_callback(void *_beeper, Uint8 *_stream, int _length)
    {
        Sint16 *stream = (Sint16*) _stream;
        int length = _length / 2;
        Beeper* beeper = (Beeper*) _beeper;
    
        beeper->generateSamples(stream, length);
    }
    
    int main(int argc, char* argv[])
    {
        SDL_Init(SDL_INIT_AUDIO);
    
        int duration = 1000;
        double Hz = 440;
    
        Beeper b;
        b.beep(Hz, duration);
        b.wait();
    
        return 0;
    }
    

    Good luck.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having problems understanding how latest.integration works. I have an example that is not
I'm having problems understanding how preventDefault function works. I created a simple jsfiddle, but
I am relatively new to C++ and am having problems understanding struct. I have
I'm having problems with understanding how jQuery create headers for ajax post. I have
Im having some problems understanding some code in a programme I have inherited. CGPoint
I'm having problems understanding how WPF app.xaml works. Is it like Main method in
I'm having problems understanding how I should install xhtml2pdf. I have followed the instructions
I'm having some problems using / understanding is_dir. (Yes, I have read the PHP
I'm having problems understanding xslt. In my source document I have to find the
I am having problems understanding what/how this works. it seems odd assigning self to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.