S32 PCMAudioManager::loadSound(const char *assetName) { Property *prop = btGetContext()->appProperties->get("debug_sounds"); BOOL32 debugSounds = FALSE; if (prop) { debugSounds = prop->getBoolValue(); } if (isSoundLoaded(assetName)) { return getSoundId(assetName); } int assetSize = 0; unsigned char *fileData = _platform_load_asset(assetName, &assetSize); S32 sndId = -1; if (fileData) { signed short *decoded; int channels, len; unsigned int sampleRate = 0; len = stb_vorbis_decode_memory(fileData, assetSize, &channels, &sampleRate, &decoded); if (debugSounds) { char buf[1024]; sprintf(buf, "Loaded %s: (%i enc bytes) length=%i channels=%i rate=%i", assetName, assetSize, len, channels, sampleRate); logmsg(buf); } sndId = loadSound(decoded, len * channels, sampleRate, channels, assetName); } _platform_free_asset(fileData); return sndId; }
void ofxTLAudioTrack::playbackLooped(ofxTLPlaybackEventArgs& args){ if(isSoundLoaded() && this != timeline->getTimecontrolTrack()){ if(!player.getIsPlaying()){ player.play(); } player.setPosition( positionForSecond(timeline->getCurrentTime()) ); } }
void Sound::playSoundSegment(uint32 start, uint32 end) { if (!isSoundLoaded()) return; stopSound(); Audio::AudioStream *subStream = new Audio::SubSeekableAudioStream(_stream, Audio::Timestamp(0, start, 600), Audio::Timestamp(0, end, 600), DisposeAfterUse::NO); g_system->getMixer()->playStream(Audio::Mixer::kPlainSoundType, &_handle, subStream, -1, _volume, 0, DisposeAfterUse::YES); }
void ofxTLAudioTrack::playbackStarted(ofxTLPlaybackEventArgs& args){ ofxTLTrack::playbackStarted(args); if(isSoundLoaded() && this != timeline->getTimecontrolTrack()){ //player.setPosition(timeline->getPercentComplete()); float position = positionForSecond(timeline->getCurrentTime()); if(position < 1.0){ player.play(); } player.setPosition( position ); } }
vector<float>& ofxTLAudioTrack::getFFTSpectrum(int numBins){ float fftPosition = player.getPosition(); if(isSoundLoaded() && lastFFTPosition != fftPosition){ if(defaultFFTBins != numBins){ maxBinReceived = 0; defaultFFTBins = numBins; } lastFFTPosition = fftPosition; fftBins = player.getSpectrum(defaultFFTBins); } return fftBins; }
void Sound::playSound() { if (!isSoundLoaded()) return; stopSound(); // Make sure the sound is back at the beginning before we play it _stream->rewind(); if (_fader) setVolume(_fader->getFaderValue()); g_system->getMixer()->playStream(Audio::Mixer::kPlainSoundType, &_handle, _stream, -1, _volume, 0, DisposeAfterUse::NO); }
void Sound::loopSound() { if (!isSoundLoaded()) return; stopSound(); // Create a looping stream Audio::AudioStream *loopStream = new Audio::LoopingAudioStream(_stream, 0, DisposeAfterUse::NO); // Assume that if there is a fader, we're going to fade the sound in. if (_fader) setVolume(0); g_system->getMixer()->playStream(Audio::Mixer::kPlainSoundType, &_handle, loopStream, -1, _volume, 0, DisposeAfterUse::YES); }
//envelope and dampening approach from Marius Watz //http://workshop.evolutionzone.com/2012/08/30/workshops-sept-89-sound-responsive-visuals-3d-printing-and-parametric-modeling/ vector<float>& ofxTLAudioTrack::getFFT(){ float fftPosition = player.getPosition(); if(isSoundLoaded() && lastFFTPosition != fftPosition){ vector<float>& fftAverages = player.getAverages(); averageSize = fftAverages.size(); if(envelope.size() != averageSize){ generateEnvelope(averageSize); } if(dampened.size() != averageSize){ dampened.clear(); dampened.resize(averageSize); } if(getUseFFTEnvelope()){ for(int i = 0; i < fftAverages.size(); i++){ fftAverages[i] *= envelope[i]; } } float max = 0; for(int i = 0; i < fftAverages.size(); i++){ max = MAX(max, fftAverages[i]); } if(max != 0){ for(int i = 0; i < fftAverages.size(); i++){ fftAverages[i] = ofMap(fftAverages[i],0, max, 0, 1.0); } } for(int i = 0; i < averageSize; i++) { dampened[i] = (fftAverages[i] * dampening) + dampened[i]*(1-dampening); } //normalizer hack lastFFTPosition = fftPosition; } return dampened; }
bool Sound::isPlaying() { return isSoundLoaded() && g_system->getMixer()->isSoundHandleActive(_handle); }
void ofxTLAudioTrack::playbackEnded(ofxTLPlaybackEventArgs& args){ if(isSoundLoaded() && this != timeline->getTimecontrolTrack()){ player.stop(); } }
float ofxTLAudioTrack::positionForSecond(float second){ if(isSoundLoaded()){ return ofMap(second, 0, player.getDuration(), 0, 1.0, true); } return 0; }
void ofxTLAudioTrack::setFFTLogAverages(int minBandwidth, int bandsPerOctave){ if(isSoundLoaded()){ player.setLogAverages(minBandwidth, bandsPerOctave); } }