'' SDL2_mixer Example program:
'' A test application for the SDL mixer library.
'' translated/modified by TJF 2013
'/*
  'PLAYMUS:  A test application for the SDL mixer library.
  'Copyright (C) 1997-2013 Sam Lantinga <slouken@libsdl.org>

  'This software is provided 'as-is', without any express or implied
  'warranty.  In no event will the authors be held liable for any damages
  'arising from the use of this software.

  'Permission is granted to anyone to use this software for any purpose,
  'including commercial applications, and to alter it and redistribute it
  'freely, subject to the following restrictions:

  '1. The origin of this software must not be misrepresented; you must not
     'claim that you wrote the original software. If you use this software
     'in a product, an acknowledgment in the product documentation would be
     'appreciated but is not required.
  '2. Altered source versions must be plainly marked as such, and must not be
     'misrepresented as being the original software.
  '3. This notice may not be removed or altered from any source distribution.
'*/

'/* Quiet windows compiler warnings */
#DEFINE _CRT_SECURE_NO_WARNINGS

#INCLUDE ONCE "crt/stdio.bi"

#IFDEF __FB_UNIX__
#INCLUDE ONCE "crt/unistd.bi"
#ENDIF

#INCLUDE ONCE "SDL2/SDL2.bi"
#INCLUDE ONCE "SDL2/SDL2_mixer.bi"

'#ifdef HAVE_SIGNAL_H
'#include <signal.h>
'#endif

STATIC SHARED AS INTEGER audio_open = 0
STATIC SHARED AS Mix_Music PTR music = NULL
STATIC SHARED AS INTEGER next_track = 0

SUB CleanUp(BYVAL exitcode AS INTEGER)
  IF Mix_PlayingMusic() THEN
    Mix_FadeOutMusic(1500)
    SDL_Delay(1500)
  END IF
  IF music THEN
    Mix_FreeMusic(music)
    music = NULL
  END IF
  IF audio_open THEN
    Mix_CloseAudio()
    audio_open = 0
  END IF
  SDL_Quit()
  END exitcode
END SUB

SUB Usage(BYVAL Argv0 AS ZSTRING PTR)
  fprintf(stderr, !"Usage: %s [-i] [-l] [-8] [-r rate] [-c channels] [-b buffers] [-v N] [-rwops] <musicfile>\n", Argv0)
END SUB

SUB Menu()
    DIM AS ZSTRING*10 buf

    printf(!"Available commands: (p)ause (r)esume (h)alt volume(v#) > ")
    fflush(stdin)
    IF scanf("%s", buf) = 1 THEN
      SELECT CASE AS CONST buf[0]
      CASE ASC("P"), ASC("p") : Mix_PauseMusic()
      CASE ASC("R"), ASC("r") : Mix_ResumeMusic()
      CASE ASC("H"), ASC("h") : Mix_HaltMusic()
      CASE ASC("V"), ASC("v") : Mix_VolumeMusic(VALINT(MID(buf, 2)))
      END SELECT
    END IF
    printf(!"Music playing: %s Paused: %s\n", IIF(Mix_PlayingMusic(), @"yes", @"no"), _
           IIF(Mix_PausedMusic(), @"yes", @"no"))
END SUB

'#ifdef HAVE_SIGNAL_H

'void IntHandler(int sig)
'{
    'switch (sig) {
            'case SIGINT:
            'next_track++;
            'break;
    '}
'}

'#endif

'/* Initialize variables */
VAR audio_rate = 22050
DIM AS Uint16 audio_format = AUDIO_S16
VAR audio_channels = 2
VAR audio_buffers = 4096
VAR audio_volume = MIX_MAX_VOLUME
VAR looping = 0
VAR interactive = 0
VAR rwops = 0

'/* Check command line usage */
VAR i = 1
WHILE __FB_ARGV__[i] ANDALSO *__FB_ARGV__[i] = ASC("-")
  SELECT CASE *__FB_ARGV__[i]
  CASE "-r"
    IF __FB_ARGV__[ i + 1 ] THEN
      i += 1
      audio_rate = VALINT(*__FB_ARGV__[i])
    END IF
  CASE "-c"
    IF __FB_ARGV__[ i + 1 ] THEN
      i += 1
      audio_channels = VALINT(*__FB_ARGV__[i])
    END IF
  CASE "-b"
    IF __FB_ARGV__[ i + 1 ] THEN
      i += 1
      audio_buffers = VALINT(*__FB_ARGV__[i])
    END IF
  CASE "-v"
    IF __FB_ARGV__[ i + 1 ] THEN
      i += 1
      audio_volume = VALINT(*__FB_ARGV__[i])
    END IF
  CASE "-m" : audio_channels = 1
  CASE "-l" : looping = -1
  CASE "-i" : interactive = 1
  CASE "-8" : audio_format = AUDIO_U8
  CASE "-rwops" : rwops = 1
  CASE ELSE : Usage(__FB_ARGV__[0]) : END 1
  END SELECT : i += 1
WEND

IF 0 = __FB_ARGV__[i] THEN
  Usage(__FB_ARGV__[0])
  END 1
END IF

'/* Initialize the SDL library */
IF SDL_Init(SDL_INIT_AUDIO) < 0 THEN
  fprintf(stderr, !"Couldn't initialize SDL: %s\n", SDL_GetError())
  END 255
END IF

'#ifdef HAVE_SIGNAL_H
'signal(SIGINT, IntHandler);
'signal(SIGTERM, CleanUp);
'#endif

'/* Open the audio device */
IF Mix_OpenAudio(audio_rate, audio_format, audio_channels, audio_buffers) < 0 THEN
  fprintf(stderr, !"Couldn't open audio: %s\n", SDL_GetError())
  END 2
ELSE
  Mix_QuerySpec(@audio_rate, @audio_format, @audio_channels)
  printf(!"Opened audio at %d Hz %d bit %s (%s), %d bytes audio buffer\n", audio_rate, _
         (audio_format AND &hFF), _
         IIF(audio_channels > 2, @"surround", IIF(audio_channels > 1, @"stereo", @"mono")), _
         IIF(audio_format AND &h1000, @"BE", @"LE"), _
         audio_buffers)
END IF
audio_open = 1



' By uncommenting check for several decoders support
'var flags = MIX_INIT_OGG OR MIX_INIT_MP3 OR MIX_INIT_MOD OR MIX_INIT_FLAC
'var initted = Mix_Init(flags)
'if (initted and flags) <> flags then
  'printf(!"Mix_Init: Failed to init required decoder support!\n")
  'printf(!"Mix_Init: %s\n", Mix_GetError())
  'CleanUp(3)
'end if



'/* Set the music volume */
Mix_VolumeMusic(audio_volume)

'/* Set the external music player, if any */
Mix_SetMusicCMD(SDL_getenv("MUSIC_CMD"))

WHILE __FB_ARGV__[i]
  next_track = 0

  '/* Load the requested music file */
  IF rwops THEN
    music = Mix_LoadMUS_RW(SDL_RWFromFile(__FB_ARGV__[i], "rb"), SDL_TRUE)
  ELSE
    music = Mix_LoadMUS(__FB_ARGV__[i])
  END IF
  IF music = NULL THEN
    fprintf(stderr, !"Couldn't load %s: %s\n", __FB_ARGV__[i], SDL_GetError())
    CleanUp(2)
  END IF

  '/* Play and then exit */
  printf(!"Playing %s\n", __FB_ARGV__[i])
  Mix_FadeInMusic(music, looping, 2000)
  WHILE 0 = next_track ANDALSO (Mix_PlayingMusic() ORELSE Mix_PausedMusic())
    IF interactive THEN
      Menu()
    ELSE
      SDL_Delay(100)
    END IF
  WEND
  Mix_FreeMusic(music)
  music = NULL

  '/* If the user presses Ctrl-C more than once, exit. */
  SDL_Delay(500)
  IF next_track > 1 THEN EXIT WHILE

  i += 1
WEND
CleanUp(0)
