test/loopwave.c
author Sam Lantinga <slouken@libsdl.org>
Sun, 07 Jul 2019 09:10:56 -0700
changeset 12928 3c4a4b1077cd
parent 12503 806492103856
permissions -rw-r--r--
Fixed bug 4710 - audio/alsa: avoid configuring hardware parameters with only a single period

Anthony Pesch

The previous code first configured the period size using snd_pcm_hw_par-
ams_set_period_size_near. Then, it further narrowed the configuration
space by calling snd_pcm_hw_params_set_buffer_size_near using a buffer
size of 2 times the _requested_ period size in order to try and get a
configuration with only 2 periods. If the configured period size was
larger than the requested size, the second call could inadvertently
narrow the configuration space to contain only a single period.

Rather than fixing the call to snd_pcm_hw_params_set_buffer_size_near
to use a size of 2 times the configured period size, the code has been
changed to use snd_pcm_hw_params_set_periods_min in order to more
clearly explain the intent.
slouken@8582
     1
/*
slouken@12503
     2
  Copyright (C) 1997-2019 Sam Lantinga <slouken@libsdl.org>
slouken@8582
     3
slouken@8582
     4
  This software is provided 'as-is', without any express or implied
slouken@8582
     5
  warranty.  In no event will the authors be held liable for any damages
slouken@8582
     6
  arising from the use of this software.
slouken@8582
     7
slouken@8582
     8
  Permission is granted to anyone to use this software for any purpose,
slouken@8582
     9
  including commercial applications, and to alter it and redistribute it
slouken@8582
    10
  freely.
slouken@8582
    11
*/
slouken@8582
    12
philipp@9945
    13
/* Program to load a wave file and loop playing it using SDL audio */
slouken@8582
    14
slouken@8582
    15
/* loopwaves.c is much more robust in handling WAVE files --
slouken@8582
    16
    This is only for simple WAVEs
slouken@8582
    17
*/
slouken@8582
    18
#include "SDL_config.h"
slouken@8582
    19
slouken@8582
    20
#include <stdio.h>
slouken@8582
    21
#include <stdlib.h>
slouken@8582
    22
icculus@9278
    23
#ifdef __EMSCRIPTEN__
icculus@9278
    24
#include <emscripten/emscripten.h>
icculus@9278
    25
#endif
icculus@9278
    26
slouken@8582
    27
#include "SDL.h"
slouken@8582
    28
philipp@10902
    29
static struct
slouken@8582
    30
{
slouken@8582
    31
    SDL_AudioSpec spec;
slouken@8582
    32
    Uint8 *sound;               /* Pointer to wave data */
slouken@8582
    33
    Uint32 soundlen;            /* Length of wave data */
slouken@8582
    34
    int soundpos;               /* Current play position */
slouken@8582
    35
} wave;
slouken@8582
    36
slouken@10932
    37
static SDL_AudioDeviceID device;
slouken@8582
    38
slouken@8582
    39
/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
slouken@8582
    40
static void
slouken@8582
    41
quit(int rc)
slouken@8582
    42
{
slouken@8582
    43
    SDL_Quit();
slouken@8582
    44
    exit(rc);
slouken@8582
    45
}
slouken@8582
    46
slouken@10932
    47
static void
slouken@10932
    48
close_audio()
slouken@10932
    49
{
slouken@10933
    50
    if (device != 0) {
slouken@10933
    51
        SDL_CloseAudioDevice(device);
slouken@10933
    52
        device = 0;
slouken@10933
    53
    }
slouken@10932
    54
}
slouken@10932
    55
slouken@10932
    56
static void
slouken@10932
    57
open_audio()
slouken@10932
    58
{
slouken@10933
    59
    /* Initialize fillerup() variables */
slouken@10933
    60
    device = SDL_OpenAudioDevice(NULL, SDL_FALSE, &wave.spec, NULL, 0);
slouken@10933
    61
    if (!device) {
slouken@10933
    62
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't open audio: %s\n", SDL_GetError());
slouken@10933
    63
        SDL_FreeWAV(wave.sound);
slouken@10933
    64
        quit(2);
slouken@10933
    65
    }
slouken@10932
    66
slouken@10932
    67
slouken@10933
    68
    /* Let the audio run */
slouken@10933
    69
    SDL_PauseAudioDevice(device, SDL_FALSE);
slouken@10932
    70
}
slouken@10932
    71
slouken@10932
    72
static void reopen_audio()
slouken@10932
    73
{
slouken@10933
    74
    close_audio();
slouken@10933
    75
    open_audio();
slouken@10932
    76
}
slouken@10932
    77
slouken@8582
    78
slouken@8582
    79
void SDLCALL
slouken@8582
    80
fillerup(void *unused, Uint8 * stream, int len)
slouken@8582
    81
{
slouken@8582
    82
    Uint8 *waveptr;
slouken@8582
    83
    int waveleft;
slouken@8582
    84
slouken@8582
    85
    /* Set up the pointers */
slouken@8582
    86
    waveptr = wave.sound + wave.soundpos;
slouken@8582
    87
    waveleft = wave.soundlen - wave.soundpos;
slouken@8582
    88
slouken@8582
    89
    /* Go! */
slouken@8582
    90
    while (waveleft <= len) {
slouken@8582
    91
        SDL_memcpy(stream, waveptr, waveleft);
slouken@8582
    92
        stream += waveleft;
slouken@8582
    93
        len -= waveleft;
slouken@8582
    94
        waveptr = wave.sound;
slouken@8582
    95
        waveleft = wave.soundlen;
slouken@8582
    96
        wave.soundpos = 0;
slouken@8582
    97
    }
slouken@8582
    98
    SDL_memcpy(stream, waveptr, len);
slouken@8582
    99
    wave.soundpos += len;
slouken@8582
   100
}
slouken@8582
   101
slouken@8582
   102
static int done = 0;
slouken@8582
   103
icculus@9280
   104
#ifdef __EMSCRIPTEN__
icculus@9278
   105
void
icculus@9278
   106
loop()
icculus@9278
   107
{
slouken@10932
   108
    if(done || (SDL_GetAudioDeviceStatus(device) != SDL_AUDIO_PLAYING))
icculus@9278
   109
        emscripten_cancel_main_loop();
icculus@9278
   110
}
icculus@9280
   111
#endif
icculus@9278
   112
slouken@8582
   113
int
slouken@8582
   114
main(int argc, char *argv[])
slouken@8582
   115
{
slouken@8582
   116
    int i;
slouken@8582
   117
    char filename[4096];
slouken@8582
   118
philipp@9922
   119
    /* Enable standard application logging */
philipp@9922
   120
    SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
slouken@8582
   121
slouken@8582
   122
    /* Load the SDL library */
slouken@10932
   123
    if (SDL_Init(SDL_INIT_AUDIO|SDL_INIT_EVENTS) < 0) {
slouken@8582
   124
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
slouken@8582
   125
        return (1);
slouken@8582
   126
    }
slouken@8582
   127
slouken@8836
   128
    if (argc > 1) {
slouken@8582
   129
        SDL_strlcpy(filename, argv[1], sizeof(filename));
slouken@8582
   130
    } else {
slouken@8582
   131
        SDL_strlcpy(filename, "sample.wav", sizeof(filename));
slouken@8582
   132
    }
slouken@8582
   133
    /* Load the wave file into memory */
slouken@8582
   134
    if (SDL_LoadWAV(filename, &wave.spec, &wave.sound, &wave.soundlen) == NULL) {
slouken@8837
   135
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't load %s: %s\n", filename, SDL_GetError());
slouken@8582
   136
        quit(1);
slouken@8582
   137
    }
slouken@8582
   138
slouken@8582
   139
    wave.spec.callback = fillerup;
slouken@8582
   140
slouken@8582
   141
    /* Show the list of available drivers */
slouken@8582
   142
    SDL_Log("Available audio drivers:");
slouken@8582
   143
    for (i = 0; i < SDL_GetNumAudioDrivers(); ++i) {
philipp@9922
   144
        SDL_Log("%i: %s", i, SDL_GetAudioDriver(i));
slouken@10933
   145
    }
slouken@8582
   146
slouken@10933
   147
    SDL_Log("Using audio driver: %s\n", SDL_GetCurrentAudioDriver());
slouken@8582
   148
slouken@10933
   149
    open_audio();
slouken@8582
   150
slouken@10933
   151
    SDL_FlushEvents(SDL_AUDIODEVICEADDED, SDL_AUDIODEVICEREMOVED);
icculus@9278
   152
icculus@9278
   153
#ifdef __EMSCRIPTEN__
icculus@9278
   154
    emscripten_set_main_loop(loop, 0, 1);
icculus@9278
   155
#else
slouken@10933
   156
    while (!done) {
slouken@10933
   157
        SDL_Event event;
slouken@10932
   158
slouken@10933
   159
        while (SDL_PollEvent(&event) > 0) {
slouken@10933
   160
            if (event.type == SDL_QUIT) {
slouken@10933
   161
                done = 1;
slouken@10933
   162
            }
slouken@10933
   163
            if ((event.type == SDL_AUDIODEVICEADDED && !event.adevice.iscapture) ||
slouken@10933
   164
                (event.type == SDL_AUDIODEVICEREMOVED && !event.adevice.iscapture && event.adevice.which == device)) {
slouken@10933
   165
                reopen_audio();
slouken@10933
   166
            }
slouken@10933
   167
        }
slouken@10933
   168
        SDL_Delay(100);
slouken@10933
   169
    }
icculus@9278
   170
#endif
slouken@8582
   171
slouken@8582
   172
    /* Clean up on signal */
slouken@10933
   173
    close_audio();
slouken@8582
   174
    SDL_FreeWAV(wave.sound);
slouken@8582
   175
    SDL_Quit();
slouken@8582
   176
    return (0);
slouken@8582
   177
}
slouken@8582
   178
slouken@8582
   179
/* vi: set ts=4 sw=4 expandtab: */