1 // BlinkenSisters - Hunt for the Lost Pixels
2 //     Bringing back the fun of the 80s
3 //
4 // (C) 2005-07 Rene Schickbauer, Wolfgang Dautermann
5 //
6 // See License.txt for licensing information
7 //
8 
9 
10 #include <stdlib.h>
11 #include "../globals.h"
12 #include "blinkensound.h"
13 #include "../errorhandler.h"
14 #include <string.h>
15 
16 
17 int audio_rate = 22050;
18 Uint16 audio_format = AUDIO_S16SYS; /* 16-bit stereo */
19 int audio_channels = 2;
20 Uint32 audio_buffers = 2048;
21 bool soundInitOK = false;
22 bool isPlaying = false;
23 bool isPaused = false;
24 
25 char captureSound[8192];
26 
initSound()27 void initSound()
28 {
29 	/* This is where we open up our audio device.  Mix_OpenAudio takes
30 	as its parameters the audio format we'd /like/ to have. */
31 	if(Mix_OpenAudio(audio_rate, audio_format, audio_channels, audio_buffers)) {
32 		printf("Unable to open audio!\n");
33 		soundInitOK = false;
34 		return;
35 	} else {
36 		soundInitOK = true;
37 		Mix_HookMusicFinished(soundMusicFinished);
38 		if(!Mix_RegisterEffect(MIX_CHANNEL_POST, soundCaptureOutput, NULL, NULL)) {
39             printf("Unable to register display callback\n");
40         }
41 	}
42 
43 	/* If we actually care about what we got, we can ask here.  In this
44 	program we don't... yet */
45 	Mix_QuerySpec(&audio_rate, &audio_format, &audio_channels);
46 	memset(captureSound, 0, sizeof(captureSound));
47 }
48 
soundLoad(Uint32 idx,char * fname)49 void soundLoad(Uint32 idx, char* fname) {
50 	char fullfname[MAX_FNAME_LENGTH];
51 	sprintf(fullfname, "%s", configGetPath(fname));
52 	titles[idx].sdlmusic = Mix_LoadMUS(fullfname);
53 	if(!titles[idx].sdlmusic) {
54 		DIE(ERROR_SOUND_READ, fullfname);
55 	}
56 }
57 
deInitSound()58 void deInitSound()
59 {
60 	if(!soundInitOK) {
61 		return;
62 	}
63 	soundStopMusic();
64 
65 	for(Uint32 i = 0; i < 100; i++) {
66        if(titles[i].sdlmusic) {
67            Mix_FreeMusic(titles[i].sdlmusic);
68            titles[i].sdlmusic = 0;
69        }
70     }
71 
72 	Mix_CloseAudio();
73 	soundInitOK = false;
74 }
75 
soundStartMusic()76 void soundStartMusic()
77 {
78 	if(!soundInitOK) {
79 		return;
80 	}
81 	if(isPaused) {
82         soundPauseMusic();
83         return;
84     }
85 	if(isPlaying) {
86         soundStopMusic();
87     }
88 
89 	Mix_PlayMusic(titles[curSnd].sdlmusic, 1);
90 	Mix_VolumeMusic(MIX_MAX_VOLUME / 2);
91 	isPlaying = true;
92 }
93 
soundStopMusic()94 void soundStopMusic()
95 {
96 	if(!soundInitOK) {
97 		return;
98 	}
99 	if(isPaused) {
100         soundPauseMusic();
101     }
102 
103 	Mix_HaltMusic();
104 	isPlaying = false;
105 }
106 
soundStepNext()107 void soundStepNext() {
108      curSnd++;
109      if(curSnd == maxSnd) {
110          curSnd = 0;
111      }
112      if(isPaused) {
113          soundStopMusic();
114      } else if(isPlaying) {
115          soundStartMusic();
116      }
117 }
118 
soundStepPrev()119 void soundStepPrev() {
120      if(curSnd == 0) {
121          curSnd = maxSnd;
122      }
123      curSnd--;
124      if(isPaused) {
125          soundStopMusic();
126      } else if(isPlaying) {
127          soundStartMusic();
128      }
129 }
130 
soundPauseMusic()131 void soundPauseMusic() {
132      if(!isPlaying) {
133          return;
134      }
135      if(!isPaused) {
136          Mix_PauseMusic();
137      } else {
138          Mix_ResumeMusic();
139      }
140      isPaused = !isPaused;
141 }
142 
soundMusicFinished()143 void soundMusicFinished() {
144 	soundStepNext();
145 }
146 
soundCaptureOutput(int chan,void * stream,int len,void * udata)147 void soundCaptureOutput(int chan, void* stream, int len, void* udata) {
148      memcpy(captureSound, stream, len);
149      if(chan || udata) {
150              // Supress "unused variables" warning
151      }
152 }
153