File: sound.cpp

package info (click to toggle)
openssn 1.4-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 7,340 kB
  • sloc: cpp: 10,973; makefile: 80
file content (76 lines) | stat: -rw-r--r-- 1,272 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include <stdio.h>
#include <SDL.h>
#include <SDL/SDL_mixer.h>
#include "sound.h"

Mix_Music *my_music = NULL;
int play_sounds = FALSE;

int Init_Audio(int allow_sounds)
{
   my_music = NULL;
   int audio_rate = 22050;
   Uint16 audio_format = AUDIO_S16; /* 16-bit stereo */
   int audio_channels = 2;
   int audio_buffers = 4096;

   if (allow_sounds)
     Mix_OpenAudio(audio_rate, audio_format, audio_channels, audio_buffers);
     
   play_sounds = allow_sounds;
   
   return TRUE;
}


void Sound_Done()
{
    if (my_music)
    {
      Mix_HaltMusic();
      Mix_FreeMusic(my_music);
      my_music = NULL;
    }
}



int Play_Sound(char *sound_file)
{
   // maybe sound if disabled?
   if (! play_sounds)
      return FALSE;

   // something else is playing
   if (my_music)
      return FALSE;

   // bad pointer?
   if (! sound_file)
      return FALSE;

   my_music = Mix_LoadMUS(sound_file);
   if (my_music)
   {
       Mix_PlayMusic(my_music, 0);
       Mix_HookMusicFinished(Sound_Done);
       return TRUE;
   }
   return FALSE;
}



void Clean_Up_Audio()
{
    // sound was disabled, nothing to clean
    if (! play_sounds)
       return;

    Mix_HaltMusic();
    if (my_music)
       Mix_FreeMusic(my_music);
    my_music = NULL;
    Mix_CloseAudio();
}