test/loopwavequeue.c
author Sam Lantinga <slouken@libsdl.org>
Sat, 27 Jan 2018 12:05:26 -0800
changeset 11836 a42b155124a3
parent 11811 5d94cb6b24d3
child 12503 806492103856
permissions -rw-r--r--
Fixed bug 4065 - SDL_wave.c fail to compile in armeabi with ndk r16b

Sylvain

armeabi is almost deprecated for android-ndk higher that r13b.
either it doesn't compile (ICE), or it executes wrongly (using long long for instance).

android people advices to use armeabi-v7a (or use r13b).
icculus@9012
     1
/*
slouken@11811
     2
  Copyright (C) 1997-2018 Sam Lantinga <slouken@libsdl.org>
icculus@9012
     3
icculus@9012
     4
  This software is provided 'as-is', without any express or implied
icculus@9012
     5
  warranty.  In no event will the authors be held liable for any damages
icculus@9012
     6
  arising from the use of this software.
icculus@9012
     7
icculus@9012
     8
  Permission is granted to anyone to use this software for any purpose,
icculus@9012
     9
  including commercial applications, and to alter it and redistribute it
icculus@9012
    10
  freely.
icculus@9012
    11
*/
icculus@9012
    12
icculus@9012
    13
/* Program to load a wave file and loop playing it using SDL sound queueing */
icculus@9012
    14
icculus@9012
    15
#include <stdio.h>
icculus@9012
    16
#include <stdlib.h>
icculus@9012
    17
philipp@9420
    18
#ifdef __EMSCRIPTEN__
philipp@9420
    19
#include <emscripten/emscripten.h>
philipp@9420
    20
#endif
philipp@9420
    21
slouken@9079
    22
#include "SDL.h"
slouken@9079
    23
icculus@9012
    24
#if HAVE_SIGNAL_H
icculus@9012
    25
#include <signal.h>
icculus@9012
    26
#endif
icculus@9012
    27
philipp@10902
    28
static struct
icculus@9012
    29
{
icculus@9012
    30
    SDL_AudioSpec spec;
icculus@9012
    31
    Uint8 *sound;               /* Pointer to wave data */
icculus@9012
    32
    Uint32 soundlen;            /* Length of wave data */
icculus@9012
    33
} wave;
icculus@9012
    34
icculus@9012
    35
icculus@9012
    36
/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
icculus@9012
    37
static void
icculus@9012
    38
quit(int rc)
icculus@9012
    39
{
icculus@9012
    40
    SDL_Quit();
icculus@9012
    41
    exit(rc);
icculus@9012
    42
}
icculus@9012
    43
icculus@9012
    44
static int done = 0;
icculus@9012
    45
void
icculus@9012
    46
poked(int sig)
icculus@9012
    47
{
icculus@9012
    48
    done = 1;
icculus@9012
    49
}
icculus@9012
    50
philipp@9420
    51
void
philipp@9420
    52
loop()
philipp@9420
    53
{
philipp@9420
    54
#ifdef __EMSCRIPTEN__
philipp@9420
    55
    if (done || (SDL_GetAudioStatus() != SDL_AUDIO_PLAYING)) {
philipp@9420
    56
        emscripten_cancel_main_loop();
philipp@9420
    57
    }
philipp@9420
    58
    else
philipp@9420
    59
#endif
philipp@9420
    60
    {
philipp@9420
    61
        /* The device from SDL_OpenAudio() is always device #1. */
philipp@9420
    62
        const Uint32 queued = SDL_GetQueuedAudioSize(1);
philipp@9420
    63
        SDL_Log("Device has %u bytes queued.\n", (unsigned int) queued);
philipp@9420
    64
        if (queued <= 8192) {  /* time to requeue the whole thing? */
philipp@9420
    65
            if (SDL_QueueAudio(1, wave.sound, wave.soundlen) == 0) {
philipp@9420
    66
                SDL_Log("Device queued %u more bytes.\n", (unsigned int) wave.soundlen);
philipp@9420
    67
            } else {
philipp@9420
    68
                SDL_Log("Device FAILED to queue %u more bytes: %s\n", (unsigned int) wave.soundlen, SDL_GetError());
philipp@9420
    69
            }
philipp@9420
    70
        }
philipp@9420
    71
    }
philipp@9420
    72
}
philipp@9420
    73
icculus@9012
    74
int
icculus@9012
    75
main(int argc, char *argv[])
icculus@9012
    76
{
icculus@9012
    77
    char filename[4096];
icculus@9012
    78
philipp@9922
    79
    /* Enable standard application logging */
philipp@9922
    80
    SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
icculus@9012
    81
icculus@9012
    82
    /* Load the SDL library */
icculus@9012
    83
    if (SDL_Init(SDL_INIT_AUDIO) < 0) {
icculus@9012
    84
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
icculus@9012
    85
        return (1);
icculus@9012
    86
    }
icculus@9012
    87
icculus@9012
    88
    if (argc > 1) {
icculus@9012
    89
        SDL_strlcpy(filename, argv[1], sizeof(filename));
icculus@9012
    90
    } else {
icculus@9012
    91
        SDL_strlcpy(filename, "sample.wav", sizeof(filename));
icculus@9012
    92
    }
icculus@9012
    93
    /* Load the wave file into memory */
icculus@9012
    94
    if (SDL_LoadWAV(filename, &wave.spec, &wave.sound, &wave.soundlen) == NULL) {
icculus@9012
    95
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't load %s: %s\n", filename, SDL_GetError());
icculus@9012
    96
        quit(1);
icculus@9012
    97
    }
icculus@9012
    98
icculus@9012
    99
    wave.spec.callback = NULL;  /* we'll push audio. */
icculus@9012
   100
icculus@9012
   101
#if HAVE_SIGNAL_H
icculus@9012
   102
    /* Set the signals */
icculus@9012
   103
#ifdef SIGHUP
icculus@9012
   104
    signal(SIGHUP, poked);
icculus@9012
   105
#endif
icculus@9012
   106
    signal(SIGINT, poked);
icculus@9012
   107
#ifdef SIGQUIT
icculus@9012
   108
    signal(SIGQUIT, poked);
icculus@9012
   109
#endif
icculus@9012
   110
    signal(SIGTERM, poked);
icculus@9012
   111
#endif /* HAVE_SIGNAL_H */
icculus@9012
   112
icculus@9012
   113
    /* Initialize fillerup() variables */
icculus@9012
   114
    if (SDL_OpenAudio(&wave.spec, NULL) < 0) {
icculus@9012
   115
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't open audio: %s\n", SDL_GetError());
icculus@9012
   116
        SDL_FreeWAV(wave.sound);
icculus@9012
   117
        quit(2);
icculus@9012
   118
    }
icculus@9012
   119
icculus@9012
   120
    /*static x[99999]; SDL_QueueAudio(1, x, sizeof (x));*/
icculus@9012
   121
icculus@9012
   122
    /* Let the audio run */
icculus@9012
   123
    SDL_PauseAudio(0);
icculus@9012
   124
philipp@9420
   125
    done = 0;
philipp@9420
   126
icculus@9012
   127
    /* Note that we stuff the entire audio buffer into the queue in one
icculus@9012
   128
       shot. Most apps would want to feed it a little at a time, as it
icculus@9012
   129
       plays, but we're going for simplicity here. */
icculus@9012
   130
    
philipp@9420
   131
#ifdef __EMSCRIPTEN__
philipp@9420
   132
    emscripten_set_main_loop(loop, 0, 1);
philipp@9420
   133
#else
icculus@9012
   134
    while (!done && (SDL_GetAudioStatus() == SDL_AUDIO_PLAYING))
icculus@9012
   135
    {
philipp@9420
   136
        loop();
icculus@9012
   137
icculus@9012
   138
        SDL_Delay(100);  /* let it play for awhile. */
icculus@9012
   139
    }
philipp@9420
   140
#endif
icculus@9012
   141
icculus@9012
   142
    /* Clean up on signal */
icculus@9012
   143
    SDL_CloseAudio();
icculus@9012
   144
    SDL_FreeWAV(wave.sound);
icculus@9012
   145
    SDL_Quit();
icculus@9012
   146
    return 0;
icculus@9012
   147
}
icculus@9012
   148
icculus@9012
   149
/* vi: set ts=4 sw=4 expandtab: */