List of SDL2 articles

Introduction to SDL2

SDL2 event processing

SDL2 texture rendering

Originally planned to write FFmpeg+SDL2 video playback, but found that the content is a little too much to say, so I will start with simple audio data playback, step by step.

Open audio Device

int SDLCALL SDL_OpenAudio(SDL_AudioSpec * desired, SDL_AudioSpec * obtained);
// Desired: The desired parameter.
Obtained: Actual audio device parameters, normally set to NULL.
Copy the code

SDL_AudioSpec

// This structure contains various parameters of the audio
typedef struct SDL_AudioSpec
{
    int freq;                   /**< audio sampling rate */
    SDL_AudioFormat format;     /**< audio data format */
    Uint8 channels;             /**< channel number: 1 mono, 2 stereo */
    Uint8 silence;              /**< Sets the mute value */
    Uint16 samples;             /**< The number of samples in the audio buffer, which must be 2 times n */
    Uint16 padding;             /**< a parameter for compatibility */
    Uint32 size;                /**< Size of the audio buffer, in bytes */
    SDL_AudioCallback callback; /**< callback function to fill the audio buffer */
    void *userdata;             /**< user-defined data */
} SDL_AudioSpec;
Copy the code

SDL_AudioCallback

This callback function is called when the audio device needs more data.

// userData: user - defined data in the SDL_AudioSpec structure. // stream: This pointer points to the audio buffer to fill. Len: Size of the audio buffer, in bytes. void (SDLCALL * SDL_AudioCallback) (void *userdata, Uint8 *stream, int len);Copy the code

Play Audio data

// Start playing audio data when pause_on is set to 0. When set to 1, the mute value is played.
void SDLCALL SDL_PauseAudio(int pause_on)
Copy the code

Play PCM audio Demo

#include <stdio.h>
#include <tchar.h>
#include <SDL_types.h>
#include "SDL.h"

static Uint8 *audio_chunk;
static Uint32 audio_len;
static Uint8 *audio_pos;
int pcm_buffer_size = 4096;

// The audio device calls the callback function when it needs more data
void read_audio_data(void *udata, Uint8 *stream, int len) {
    SDL_memset(stream, 0, len);
    if (audio_len == 0)
        return;
    len = (len > audio_len ? audio_len : len);

    SDL_MixAudio(stream, audio_pos, len, SDL_MIX_MAXVOLUME);
    audio_pos += len;
    audio_len -= len;
}

int WinMain(int argc, char *argv[]) {
    
    if (SDL_Init(SDL_INIT_AUDIO | SDL_INIT_TIMER)) {
        printf("Could not initialize SDL - %s\n", SDL_GetError());
        return - 1;
    }
  
    SDL_AudioSpec spec;
    spec.freq = 44100;// Depending on the PCM sample rate you record
    spec.format = AUDIO_S16SYS;
    spec.channels = 1; / / mono
    spec.silence = 0;
    spec.samples = 1024;
    spec.callback = read_audio_data;
    spec.userdata = NULL;

    if (SDL_OpenAudio(&spec, NULL) < 0) {
        printf("can't open audio.\n");
        return - 1;
    }

    FILE *fp = fopen("C:\\Users\\lenovo\\Desktop\\1111111.pcm"."rb+");
    if (fp == NULL) {
        printf("cannot open this file\n");
        return - 1;
    }
    char *pcm_buffer = (char *) malloc(pcm_buffer_size);

    / / play
    SDL_PauseAudio(0);

    while (1) {
        if (fread(pcm_buffer, 1, pcm_buffer_size, fp) ! = pcm_buffer_size) {// Read the data from the file. The audio device does the rest. After playing a piece of data, it performs a callback function to fetch more data
            break;
        }

        audio_chunk = (Uint8 *) pcm_buffer;
        audio_len = pcm_buffer_size; // The length is the length of read data, subtracted in read_audio_data
        audio_pos = audio_chunk;

        while (audio_len > 0) // Check whether the playback is complete
            SDL_Delay(1);
    }
    free(pcm_buffer);
    SDL_Quit();

    return 0;
}
Copy the code

OK! Step one is complete. You can now play sound normally.

Want to record PCM and try it yourself? OpenSL ES records and plays audio (with source code) to record a segment.

Source making – SimplePlayer