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 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345
|
/* -*- C++ -*-
*
* ONScripterLabel_sound.cpp - Methods for playing sound
*
* Copyright (c) 2001-2010 Ogapee. All rights reserved.
*
* ogapee@aqua.dti2.ne.jp
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "ONScripterLabel.h"
#if defined(LINUX)
#include <signal.h>
#endif
#if defined(USE_AVIFILE)
#include "AVIWrapper.h"
#endif
#ifndef MP3_MAD
#include <smpeg.h>
extern "C" void mp3callback( void *userdata, Uint8 *stream, int len )
{
SMPEG_playAudio( (SMPEG*)userdata, stream, len );
}
#endif
extern bool ext_music_play_once_flag;
extern "C"{
extern void musicFinishCallback();
extern Uint32 SDLCALL cdaudioCallback( Uint32 interval, void *param );
}
extern void midiCallback( int sig );
extern SDL_TimerID timer_cdaudio_id;
#define TMP_MUSIC_FILE "tmp.mus"
int ONScripterLabel::playSound(const char *filename, int format, bool loop_flag, int channel)
{
if ( !audio_open_flag ) return SOUND_NONE;
long length = script_h.cBR->getFileLength( filename );
if (length == 0) return SOUND_NONE;
unsigned char *buffer;
if (format & SOUND_MUSIC &&
length == music_buffer_length &&
music_buffer ){
buffer = music_buffer;
}
else{
buffer = new unsigned char[length];
script_h.cBR->getFile( filename, buffer );
}
if (format & SOUND_MUSIC){
music_info = Mix_LoadMUS_RW( SDL_RWFromMem( buffer, length ) );
Mix_VolumeMusic( music_volume );
Mix_HookMusicFinished( musicFinishCallback );
if ( Mix_PlayMusic( music_info, music_play_loop_flag?-1:0 ) == 0 ){
music_buffer = buffer;
music_buffer_length = length;
return SOUND_MUSIC;
}
}
if (format & SOUND_CHUNK){
Mix_Chunk *chunk = Mix_LoadWAV_RW(SDL_RWFromMem(buffer, length), 1);
if (playWave(chunk, format, loop_flag, channel) == 0){
delete[] buffer;
return SOUND_CHUNK;
}
}
/* check WMA */
if ( buffer[0] == 0x30 && buffer[1] == 0x26 &&
buffer[2] == 0xb2 && buffer[3] == 0x75 ){
delete[] buffer;
return SOUND_OTHER;
}
if (format & SOUND_MIDI){
FILE *fp;
if ( (fp = fopen(TMP_MUSIC_FILE, "wb")) == NULL){
fprintf(stderr, "can't open temporaly MIDI file %s\n", TMP_MUSIC_FILE);
}
else{
fwrite(buffer, 1, length, fp);
fclose( fp );
ext_music_play_once_flag = !loop_flag;
if (playMIDI(loop_flag) == 0){
delete[] buffer;
return SOUND_MIDI;
}
}
}
delete[] buffer;
return SOUND_OTHER;
}
void ONScripterLabel::playCDAudio()
{
if ( cdaudio_flag ){
if ( cdrom_info ){
int length = cdrom_info->track[current_cd_track - 1].length / 75;
SDL_CDPlayTracks( cdrom_info, current_cd_track - 1, 0, 1, 0 );
timer_cdaudio_id = SDL_AddTimer( length * 1000, cdaudioCallback, NULL );
}
}
else{
char filename[256];
sprintf( filename, "cd\\track%2.2d.mp3", current_cd_track );
int ret = playSound( filename, SOUND_MUSIC, cd_play_loop_flag );
if (ret == SOUND_MUSIC) return;
sprintf( filename, "cd\\track%2.2d.ogg", current_cd_track );
ret = playSound( filename, SOUND_MUSIC, cd_play_loop_flag );
if (ret == SOUND_MUSIC) return;
sprintf( filename, "cd\\track%2.2d.wav", current_cd_track );
ret = playSound( filename, SOUND_MUSIC, cd_play_loop_flag, MIX_BGM_CHANNEL );
}
}
int ONScripterLabel::playWave(Mix_Chunk *chunk, int format, bool loop_flag, int channel)
{
if (!chunk) return -1;
Mix_Pause( channel );
if ( wave_sample[channel] ) Mix_FreeChunk( wave_sample[channel] );
wave_sample[channel] = chunk;
if (channel == 0) Mix_Volume( channel, voice_volume * MIX_MAX_VOLUME / 100 );
else if (channel == MIX_BGM_CHANNEL) Mix_Volume( channel, music_volume * MIX_MAX_VOLUME / 100 );
else Mix_Volume( channel, se_volume * MIX_MAX_VOLUME / 100 );
if ( !(format & SOUND_PRELOAD) )
Mix_PlayChannel( channel, wave_sample[channel], loop_flag?-1:0 );
return 0;
}
int ONScripterLabel::playMIDI(bool loop_flag)
{
Mix_SetMusicCMD(midi_cmd);
char midi_filename[256];
sprintf(midi_filename, "%s%s", archive_path, TMP_MUSIC_FILE);
if ((midi_info = Mix_LoadMUS(midi_filename)) == NULL) return -1;
int midi_looping = loop_flag ? -1 : 0;
#if defined(LINUX)
signal(SIGCHLD, midiCallback);
if (midi_cmd) midi_looping = 0;
#endif
Mix_VolumeMusic(music_volume);
Mix_PlayMusic(midi_info, midi_looping);
current_cd_track = -2;
return 0;
}
int ONScripterLabel::playMPEG(const char *filename, bool click_flag, bool loop_flag)
{
int ret = 0;
#ifndef MP3_MAD
unsigned long length = script_h.cBR->getFileLength( filename );
unsigned char *mpeg_buffer = new unsigned char[length];
script_h.cBR->getFile( filename, mpeg_buffer );
SMPEG *mpeg_sample = SMPEG_new_rwops( SDL_RWFromMem( mpeg_buffer, length ), NULL, 0 );
if ( !SMPEG_error( mpeg_sample ) ){
SMPEG_enableaudio( mpeg_sample, 0 );
if ( audio_open_flag ){
SMPEG_actualSpec( mpeg_sample, &audio_format );
SMPEG_enableaudio( mpeg_sample, 1 );
}
SMPEG_enablevideo( mpeg_sample, 1 );
SMPEG_setdisplay( mpeg_sample, screen_surface, NULL, NULL );
SMPEG_setvolume( mpeg_sample, music_volume );
SMPEG_loop(mpeg_sample, loop_flag);
Mix_HookMusic( mp3callback, mpeg_sample );
SMPEG_play( mpeg_sample );
bool done_flag = false;
while( !(done_flag & click_flag) && SMPEG_status(mpeg_sample) == SMPEG_PLAYING ){
SDL_Event event;
while( SDL_PollEvent( &event ) ){
switch (event.type){
case SDL_KEYUP:
if ( ((SDL_KeyboardEvent *)&event)->keysym.sym == SDLK_RETURN ||
((SDL_KeyboardEvent *)&event)->keysym.sym == SDLK_SPACE ||
((SDL_KeyboardEvent *)&event)->keysym.sym == SDLK_ESCAPE )
done_flag = true;
break;
case SDL_QUIT:
ret = 1;
case SDL_MOUSEBUTTONUP:
done_flag = true;
break;
default:
break;
}
}
SDL_Delay( 10 );
}
SMPEG_stop( mpeg_sample );
Mix_HookMusic( NULL, NULL );
SMPEG_delete( mpeg_sample );
}
delete[] mpeg_buffer;
#else
fprintf( stderr, "mpegplay command is disabled.\n" );
#endif
return ret;
}
int ONScripterLabel::playAVI( const char *filename, bool click_flag )
{
#if defined(USE_AVIFILE)
char *absolute_filename = new char[ strlen(archive_path) + strlen(filename) + 1 ];
sprintf( absolute_filename, "%s%s", archive_path, filename );
for ( unsigned int i=0 ; i<strlen( absolute_filename ) ; i++ )
if ( absolute_filename[i] == '/' ||
absolute_filename[i] == '\\' )
absolute_filename[i] = DELIMITER;
if ( audio_open_flag ) Mix_CloseAudio();
AVIWrapper *avi = new AVIWrapper();
if ( avi->init( absolute_filename, false ) == 0 &&
avi->initAV( screen_surface, audio_open_flag ) == 0 ){
if (avi->play( click_flag )) return 1;
}
delete avi;
delete[] absolute_filename;
if ( audio_open_flag ){
Mix_CloseAudio();
openAudio();
}
#else
fprintf( stderr, "avi command is disabled.\n" );
#endif
return 0;
}
void ONScripterLabel::stopBGM( bool continue_flag )
{
if ( cdaudio_flag && cdrom_info ){
extern SDL_TimerID timer_cdaudio_id;
if ( timer_cdaudio_id ){
SDL_RemoveTimer( timer_cdaudio_id );
timer_cdaudio_id = NULL;
}
if (SDL_CDStatus( cdrom_info ) >= CD_PLAYING )
SDL_CDStop( cdrom_info );
}
if ( wave_sample[MIX_BGM_CHANNEL] ){
Mix_Pause( MIX_BGM_CHANNEL );
Mix_FreeChunk( wave_sample[MIX_BGM_CHANNEL] );
wave_sample[MIX_BGM_CHANNEL] = NULL;
}
if ( !continue_flag ){
setStr( &music_file_name, NULL );
music_play_loop_flag = false;
if (music_buffer){
delete[] music_buffer;
music_buffer = NULL;
}
}
if ( midi_info ){
ext_music_play_once_flag = true;
Mix_HaltMusic();
Mix_FreeMusic( midi_info );
midi_info = NULL;
}
if ( !continue_flag ){
setStr( &midi_file_name, NULL );
midi_play_loop_flag = false;
}
if ( music_info ){
ext_music_play_once_flag = true;
Mix_HaltMusic();
Mix_FreeMusic( music_info );
music_info = NULL;
}
if ( !continue_flag ) current_cd_track = -1;
}
void ONScripterLabel::stopAllDWAVE()
{
for (int ch=0; ch<ONS_MIX_CHANNELS ; ch++)
if ( wave_sample[ch] ){
Mix_Pause( ch );
Mix_FreeChunk( wave_sample[ch] );
wave_sample[ch] = NULL;
}
}
void ONScripterLabel::playClickVoice()
{
if ( clickstr_state == CLICK_NEWPAGE ){
if ( clickvoice_file_name[CLICKVOICE_NEWPAGE] )
playSound(clickvoice_file_name[CLICKVOICE_NEWPAGE],
SOUND_CHUNK, false, MIX_WAVE_CHANNEL);
}
else if ( clickstr_state == CLICK_WAIT ){
if ( clickvoice_file_name[CLICKVOICE_NORMAL] )
playSound(clickvoice_file_name[CLICKVOICE_NORMAL],
SOUND_CHUNK, false, MIX_WAVE_CHANNEL);
}
}
|