double Sample::update() { if(!(soundStatus & PLAYING)) return 0; if(soundStatus & PAUSED) return 0; if(!getIsLoaded()) return 0; long length=getLength(); double remainder; short* buffer = (short *)myData; position=(position+speed); remainder = position - (long) position; // check if reached EOF if ((long) position>length) { if(getIsLooping()) { position=0; } else { soundStatus &= ~PLAYING; return 0; } } //check if position less than zero (reverse) if ((long) position < 0) { if(getIsLooping()) { position = length; } else { soundStatus &= ~PLAYING; return 0; } } output = (double) ((1.0-remainder) * buffer[1+ (long) position] + remainder * buffer[2+(long) position])/32767.0;//linear interpolation return(output); }
void Sample::generateWaveForm(vector<MiniMaxima> * _waveForm) { _waveForm->clear(); bool loopState = getIsLooping(); setLooping(false); bool playState = getIsPlaying(); double tmpSpeed = getSpeed(); setSpeed(1.0f); play(); // waveform display method based on this discussion http://answers.google.com/answers/threadview/id/66003.html double sampleL, sampleR; while ((long)position<getLength()) { MiniMaxima mm; mm.minL = mm.minR = mm.maxL = mm.maxR = 0; for (int i = 0; i < 256; i++){ if(myChannels == 1) { sampleL = update(); sampleR = sampleL; } else { sampleL = update()*0.5; sampleR = update()*0.5; } mm.minL = MIN(mm.minL, sampleL); mm.minR = MIN(mm.minR, sampleR); mm.maxL = MAX(mm.maxL, sampleL); mm.maxR = MAX(mm.maxR, sampleR); } _waveForm->push_back(mm); //cout << mm.minR << " :: " << mm.maxR << " :: " << mm.minL << " :: " << mm.maxL << endl; } position = 0; setLooping(loopState); setSpeed(tmpSpeed); if(playState) play(); }
void SoundInstance::update() { if (mMotionProvider) { mMotionProvider->update(*mSource); } if (mBinding) { mBinding->update(); } if (!getIsLooping()) { if (mPreviousState == AL_PLAYING) { ALint alNewState; alGetSourcei(mSource->getALSource(), AL_SOURCE_STATE, &alNewState); SoundGeneral::checkAlError("Checking source state."); if (alNewState == AL_STOPPED) { EventPlayComplete.emit(); mPreviousState = alNewState; } } } }