1 // -*- Mode: C++; tab-width:2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 // vi:tw=80:et:ts=2:sts=2
3 //
4 // -----------------------------------------------------------------------
5 //
6 // This file is part of RLVM, a RealLive virtual machine clone.
7 //
8 // -----------------------------------------------------------------------
9 //
10 // Copyright (C) 2007 Elliot Glaysher
11 //
12 // This program is free software; you can redistribute it and/or modify
13 // it under the terms of the GNU General Public License as published by
14 // the Free Software Foundation; either version 3 of the License, or
15 // (at your option) any later version.
16 //
17 // This program is distributed in the hope that it will be useful,
18 // but WITHOUT ANY WARRANTY; without even the implied warranty of
19 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 // GNU General Public License for more details.
21 //
22 // You should have received a copy of the GNU General Public License
23 // along with this program; if not, write to the Free Software
24 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
25 //
26 // -----------------------------------------------------------------------
27
28 #include "systems/sdl/sdl_sound_chunk.h"
29
30 #include <SDL/SDL_mixer.h>
31 #include <boost/algorithm/string.hpp>
32 #include <string>
33
34 #include "systems/base/sound_system.h"
35 #include "systems/sdl/sdl_audio_locker.h"
36 #include "xclannad/wavfile.h"
37
38 SDLSoundChunk::PlayingTable SDLSoundChunk::s_playing_table;
39
SDLSoundChunk(const boost::filesystem::path & path)40 SDLSoundChunk::SDLSoundChunk(const boost::filesystem::path& path)
41 : sample_(LoadSample(path)) {}
42
SDLSoundChunk(char * data,int length)43 SDLSoundChunk::SDLSoundChunk(char* data, int length)
44 : sample_(Mix_LoadWAV_RW(SDL_RWFromMem(data, length + 0x2c), 1)),
45 data_(data) {}
46
~SDLSoundChunk()47 SDLSoundChunk::~SDLSoundChunk() {
48 Mix_FreeChunk(sample_);
49 data_.reset();
50 }
51
LoadSample(const boost::filesystem::path & path)52 Mix_Chunk* SDLSoundChunk::LoadSample(const boost::filesystem::path& path) {
53 if (boost::iequals(path.extension().string(), ".nwa")) {
54 // Hack to load NWA sounds into a MixChunk. I was resisted doing this
55 // because I assumed there was a better way, but this is essentially what
56 // jagarl does in xclannad too :(
57 FILE* f = fopen(path.native().c_str(), "r");
58 if (!f)
59 return NULL;
60 int size = 0;
61 char* data = NWAFILE::ReadAll(f, size);
62 fclose(f);
63
64 Mix_Chunk* chunk = Mix_LoadWAV_RW(SDL_RWFromMem(data, size), 1);
65 delete[] data;
66
67 return chunk;
68 } else {
69 return Mix_LoadWAV(path.native().c_str());
70 }
71 }
72
PlayChunkOn(int channel,int loops)73 void SDLSoundChunk::PlayChunkOn(int channel, int loops) {
74 {
75 SDLAudioLocker locker;
76 s_playing_table[channel] = shared_from_this();
77 }
78
79 if (Mix_PlayChannel(channel, sample_, loops) == -1) {
80 // TODO(erg): Throw something here.
81 }
82 }
83
FadeInChunkOn(int channel,int loops,int ms)84 void SDLSoundChunk::FadeInChunkOn(int channel, int loops, int ms) {
85 {
86 SDLAudioLocker locker;
87 s_playing_table[channel] = shared_from_this();
88 }
89
90 if (Mix_FadeInChannel(channel, sample_, loops, ms) == -1) {
91 // TODO(erg): Throw something here.
92 }
93 }
94
95 // static
SoundChunkFinishedPlayback(int channel)96 void SDLSoundChunk::SoundChunkFinishedPlayback(int channel) {
97 // Don't need an SDLAudioLocker because we're in the audio callback right
98 // now.
99 //
100 // Decrease the refcount of the SDLSoundChunk that just finished
101 // playing.
102 s_playing_table[channel].reset();
103 }
104
105 // static
FindNextFreeExtraChannel()106 int SDLSoundChunk::FindNextFreeExtraChannel() {
107 SDLAudioLocker locker;
108
109 for (int i = NUM_BASE_CHANNELS;
110 i < NUM_BASE_CHANNELS + NUM_EXTRA_WAVPLAY_CHANNELS;
111 ++i) {
112 if (s_playing_table[i].get() == 0)
113 return i;
114 }
115
116 return -1;
117 }
118
119 // static
StopChannel(int channel)120 void SDLSoundChunk::StopChannel(int channel) { Mix_HaltChannel(channel); }
121
StopAllChannels()122 void SDLSoundChunk::StopAllChannels() { Mix_HaltChannel(-1); }
123
FadeOut(const int channel,const int fadetime)124 void SDLSoundChunk::FadeOut(const int channel, const int fadetime) {
125 Mix_FadeOutChannel(channel, fadetime);
126 }
127