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 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468
|
/*
Copyright (C) 2001-2004 Stephane Magnenat & Luc-Olivier de Charrière
for any question or comment contact us at <stephane at magnenat dot net> or <NuageBleu at gmail dot com>
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 3 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 "SoundMixer.h"
#include "Order.h"
#include <Toolkit.h>
#include <FileManager.h>
using namespace GAGCore;
#include <iostream>
#include <assert.h>
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <speex/speex.h>
#ifndef DX9_BACKEND // TODO:Die!
#include <SDL_endian.h>
#else
#include <Types.h>
#endif
#ifdef WIN32
#include <malloc.h>
#endif
#define SAMPLE_COUNT_PER_SLICE 4096*8
#define INTERPOLATION_RANGE 65535
#define INTERPOLATION_BITS 16
#define SPEEX_FRAME_SIZE 160
#if SDL_BYTEORDER == SDL_LIL_ENDIAN
#define OGG_BYTEORDER 0
#else
#define OGG_BYTEORDER 1
#endif
static int interpolationTable[SAMPLE_COUNT_PER_SLICE];
static void initInterpolationTable(void)
{
double l = static_cast<double>(SAMPLE_COUNT_PER_SLICE-1);
double m = INTERPOLATION_RANGE;
double a = - (2) / (l * l * l);
double b = (3) / (l * l);
for (unsigned i=0; i<SAMPLE_COUNT_PER_SLICE; i++)
{
double x = static_cast<double>(i);
double v = m * (a * (x * x * x) + b * (x* x));
interpolationTable[i] = static_cast<int>(v);
}
}
void SoundMixer::handleVoiceInsertion(int *outputSample, int voicevol)
{
// if no more voice
if (voices.empty())
return;
float value = 0;
for (std::map<int, PlayerVoice>::iterator i = voices.begin(); i != voices.end();)
{
struct PlayerVoice &pv = i->second;
value += (1-pv.voiceSubIndex) * pv.voiceVal0 + pv.voiceSubIndex * pv.voiceVal1;
// increment index, keep track of stereo
pv.voiceSubIndex += (8000.0f/44100.0f)*0.5f;
if (pv.voiceSubIndex > 1)
{
pv.voiceSubIndex -= 1;
pv.voiceVal0 = pv.voiceVal1;
pv.voiceDatas.pop();
pv.voiceVal1 = pv.voiceDatas.front();
// if there is no more data in this voice, remove it
if (pv.voiceDatas.empty())
{
// go to next voice
std::map<int, PlayerVoice>::iterator j = i;
++i;
voices.erase(j);
continue;
}
}
// go to next voice
++i;
}
// saturate
value = std::min(value, 32767.0f);
value = std::max(value, -32767.0f);
value = (value * voicevol)/256;
// write sample
*outputSample = (static_cast<int>(3.0f * (value)) + (*outputSample)) / 4;
}
void mixaudio(void *voidMixer, Uint8 *stream, int len)
{
SoundMixer *mixer = static_cast<SoundMixer *>(voidMixer);
unsigned nsamples = static_cast<unsigned>(len) >> 1;
Sint16 *mix = reinterpret_cast<Sint16 *>(stream);
int musicvol = static_cast<int>(mixer->musicVolume);
int voicevol = static_cast<int>(mixer->voiceVolume);
assert(mixer->actTrack >= 0);
assert(mixer->mode != SoundMixer::MODE_STOPPED);
// Dejan: this is supposed to fix reported problem on Gentoo
// assert(nsamples == SAMPLE_COUNT_PER_SLICE);
assert(nsamples);
if (mixer->mode == SoundMixer::MODE_EARLY_CHANGE)
{
Sint16 *track0 = reinterpret_cast<Sint16 *>(alloca(len));
Sint16 *track1 = reinterpret_cast<Sint16 *>(alloca(len));
long rest;
char *p;
// read first ogg
rest = len;
p = reinterpret_cast<char *>(track0);
ogg_int64_t firstPos = ov_pcm_tell(mixer->tracks[mixer->actTrack]);
while(rest > 0)
{
int bs;
long ret = ov_read(mixer->tracks[mixer->actTrack], p, rest, OGG_BYTEORDER, 2, 1, &bs);
if (ret == 0) // EOF
{
ov_pcm_seek(mixer->tracks[mixer->actTrack], 0);
}
else if (ret < 0) // stream error
{
}
else
{
rest -= ret;
p += ret;
}
}
// read second ogg
ov_pcm_seek(mixer->tracks[mixer->nextTrack], firstPos);
rest = len;
p = reinterpret_cast<char *>(track1);
while(rest > 0)
{
int bs;
long ret = ov_read(mixer->tracks[mixer->nextTrack], p, rest, OGG_BYTEORDER, 2, 1, &bs);
if (ret == 0) // EOF
{
ov_pcm_seek(mixer->tracks[mixer->nextTrack], 0);
}
else if (ret < 0) // stream error
{
}
else
{
rest -= ret;
p += ret;
}
}
// mix
for (unsigned i=0; i<nsamples; i++)
{
int t0 = track0[i];
int t1 = track1[i];
int intI = interpolationTable[i];
int val = (intI*t1+((INTERPOLATION_RANGE-intI)*t0))>>INTERPOLATION_BITS;
val = (val * musicvol)>>8;
mixer->handleVoiceInsertion(&val, voicevol);
mix[i] = val;
}
// clear change
mixer->actTrack = mixer->nextTrack;
mixer->mode = SoundMixer::MODE_NORMAL;
}
else
{
// read ogg
long rest = len;
char *p = reinterpret_cast<char *>(mix);
while(rest > 0)
{
int bs;
long ret = ov_read(mixer->tracks[mixer->actTrack], p, rest, OGG_BYTEORDER, 2, 1, &bs);
if (ret == 0) // EOF
{
mixer->actTrack = mixer->nextTrack;
ov_pcm_seek(mixer->tracks[mixer->actTrack], 0);
}
else if (ret < 0) // stream error
{
}
else
{
rest -= ret;
p += ret;
}
}
// volume & fading
if (mixer->mode == SoundMixer::MODE_NORMAL)
{
if (musicvol != 255)
for (unsigned i=0; i<nsamples; i++)
{
int t = mix[i];
t = (t * musicvol) >> 8;
mixer->handleVoiceInsertion(&t, voicevol);
mix[i] = t;
}
}
else if (mixer->mode == SoundMixer::MODE_START)
{
for (unsigned i=0; i<nsamples; i++)
{
int t = mix[i];
t = (interpolationTable[i]*t) >> INTERPOLATION_BITS;
t = (t * musicvol) >> 8;
mixer->handleVoiceInsertion(&t, voicevol);
mix[i] = t;
}
mixer->mode = SoundMixer::MODE_NORMAL;
}
else if (mixer->mode == SoundMixer::MODE_STOP)
{
for (unsigned i=0; i<nsamples; i++)
{
int t = mix[i];
int intI = interpolationTable[i];
t = ((INTERPOLATION_RANGE-intI)*t) >> INTERPOLATION_BITS;
t = (t * musicvol) >> 8;
mixer->handleVoiceInsertion(&t, voicevol);
mix[i] = t;
}
mixer->mode = SoundMixer::MODE_STOPPED;
SDL_PauseAudio(1);
}
}
}
void SoundMixer::openAudio(void)
{
SDL_AudioSpec as;
// Set 16-bit stereo audio at 44Khz
as.freq = 44100;
as.format = AUDIO_S16SYS;
as.channels = 2;
as.samples = SAMPLE_COUNT_PER_SLICE>>1;
as.callback = mixaudio;
as.userdata = this;
// Open the audio device and start playing sound!
if (SDL_OpenAudio(&as, NULL) < 0)
{
soundEnabled = false;
std::cerr << "SoundMixer : Unable to open audio: " << SDL_GetError() << std::endl;
return;
}
else
{
soundEnabled = true;
mode = MODE_STOPPED;
}
// Open Speex decoder
speexDecoderState = speex_decoder_init(&speex_nb_mode);
int tmp = 1;
speex_decoder_ctl(speexDecoderState, SPEEX_SET_ENH, &tmp);
}
SoundMixer::SoundMixer(unsigned musicvol, unsigned voicevol, bool mute)
{
actTrack = -1;
nextTrack = -1;
this->musicVolume = musicvol;
this->voiceVolume = voicevol;
mode = MODE_STOPPED;
speexDecoderState = NULL;
initInterpolationTable();
if (mute)
{
this->musicVolume = 0;
this->voiceVolume = 0;
}
openAudio();
/*
if (mute)
{
soundEnabled = false;
std::cout << "SoundMixer : No volume, audio has been disabled !" << std::endl;
return;
}
else
{
openAudio();
}
*/
}
SoundMixer::~SoundMixer()
{
if (soundEnabled)
{
SDL_PauseAudio(1);
SDL_CloseAudio();
speex_decoder_destroy(speexDecoderState);
}
for (size_t i=0; i<tracks.size(); i++)
{
ov_clear(tracks[i]);
delete tracks[i];
}
}
int SoundMixer::loadTrack(const char *name)
{
FILE* fp = Toolkit::getFileManager()->openFP(name);
if (!fp)
{
std::cerr << "SoundMixer : File " << name << " can't be opened for reading." << std::endl;
return -1;
}
OggVorbis_File *oggFile = new OggVorbis_File;
if (ov_open(fp, oggFile, NULL, 0) < 0)
{
std::cerr << "SoundMixer : File " << name << " does not appear to be an Ogg bitstream." << std::endl;
fclose(fp);
return -2;
}
SDL_LockAudio();
tracks.push_back(oggFile);
SDL_UnlockAudio();
return (int)tracks.size()-1;
}
void SoundMixer::setNextTrack(unsigned i, bool earlyChange)
{
if ((soundEnabled) && (i<tracks.size()))
{
SDL_LockAudio();
// Select next tracks
if (actTrack >= 0)
nextTrack = i;
else
nextTrack = actTrack = i;
// Select mode
if (mode == MODE_STOPPED)
{
SDL_PauseAudio(0);
mode = MODE_START;
}
else if (earlyChange)
{
mode = MODE_EARLY_CHANGE;
}
SDL_UnlockAudio();
}
}
void SoundMixer::setVolume(unsigned musicVolume, unsigned voiceVolume, bool mute)
{
if (!soundEnabled)
{
if (!mute)
{
openAudio();
this->musicVolume = musicVolume;
this->voiceVolume = voiceVolume;
}
else
{
return;
}
}
else if (mute)
{
this->musicVolume = 0;
this->voiceVolume = 0;
}
else
{
this->musicVolume = musicVolume;
this->voiceVolume = voiceVolume;
}
}
void SoundMixer::stopMusic(void)
{
mode = MODE_STOP;
}
bool SoundMixer::isPlayerTransmittingVoice(int player)
{
SDL_LockAudio();
if(voices.find(player) != voices.end())
{
SDL_UnlockAudio();
return true;
}
SDL_UnlockAudio();
return false;
}
void SoundMixer::addVoiceData(boost::shared_ptr<OrderVoiceData> order)
{
if (soundEnabled)
{
SDL_LockAudio();
// get or create the voice
PlayerVoice &pv = voices[order->sender];
// insert 200 ms silence to let packets come if we aer the first
if (pv.voiceDatas.empty())
{
for (size_t j=0; j<2000; j++)
pv.voiceDatas.push(0);
pv.voiceVal0 = pv.voiceVal1 = 0;
pv.voiceSubIndex = 0;
}
SpeexBits bits;
speex_bits_init(&bits);
speex_bits_read_from(&bits, (char *)order->getFramesData(), order->framesDatasLength);
// read each frame
for (size_t i=0; i<order->frameCount; i++)
{
float floatBuffer[SPEEX_FRAME_SIZE];
speex_decode(speexDecoderState, &bits, floatBuffer);
for (size_t j=0; j<SPEEX_FRAME_SIZE; j++)
pv.voiceDatas.push(floatBuffer[j]);
}
speex_bits_destroy(&bits);
SDL_UnlockAudio();
}
}
|