bool ofxTLAudioTrack::togglePlay(){
	if(getIsPlaying()){
		stop();
	}
	else {
		play();
	}
	return getIsPlaying();
}
bool ofxTLAudioTrack::togglePlay(){
	cout << "ofxTLAudioTrack::togglePlay" << endl;
	if(getIsPlaying()){
		stop();
	}
	else {
		play();
	}
	return getIsPlaying();
}
bool ofxTLVideoTrack::togglePlay(){
	
    if(!isLoaded()) return false;
    
    if(getIsPlaying()){
        stop();
    }
    else{
        play();
    }
	return getIsPlaying();
}
void AudioPlaybackBlock::togglePlayPause() {
    if (getIsPlaying()) {
        pause();
    } else {
        play();
    }
}
void AudioPlaybackBlock::onIsPlayingChanged() {
    if (getIsPlaying()) {
        m_activeNode->setValue(1.0);
    } else {
        m_activeNode->setValue(0.0);
    }
}
// ----------------------------------------------------------------------------
void ofxSoundPlayerFMOD::setVolume(float vol)
{
	if (getIsPlaying() == true){
		FMOD_Channel_SetVolume(channel, vol);
	}
	volume = vol;
}
// ----------------------------------------------------------------------------
void ofxSoundPlayerFMOD::setPan(float p)
{
	if (getIsPlaying() == true){
		FMOD_Channel_SetPan(channel,p);
	}
	pan = p;
}
// ----------------------------------------------------------------------------
void ofxSoundPlayerFMOD::setSpeed(float spd)
{
	if (getIsPlaying() == true){
		FMOD_Channel_SetFrequency(channel, internalFreq * spd);
	}
	speed = spd;
}
// ----------------------------------------------------------------------------
void ofxSoundPlayerFMOD::setPaused(bool bP)
{
	if (getIsPlaying() == true){
		FMOD_Channel_SetPaused(channel,bP);
		bPaused = bP;
	}
}
// ----------------------------------------------------------------------------
void ofxSoundPlayerFMOD::setLoop(bool bLp)
{
	if (getIsPlaying() == true){
		FMOD_Channel_SetMode(channel,  (bLp == true) ? FMOD_LOOP_NORMAL : FMOD_LOOP_OFF);
	}
	bLoop = bLp;
}
void ofxTLAudioTrack::update(){
	if(this == timeline->getTimecontrolTrack()){
		if(getIsPlaying()){
			if(player.getPosition() < lastPercent){
				ofxTLPlaybackEventArgs args = timeline->createPlaybackEvent();
				ofNotifyEvent(events().playbackLooped, args);
			}
			lastPercent = player.getPosition();
			
			//currently only supports timelines with duration == duration of player
			if(lastPercent < timeline->getInOutRange().min){
				//player.setPosition( timeline->getInOutRange().min );
				player.setPosition( positionForSecond(timeline->getInTimeInSeconds()) );
			}
			else if(lastPercent > timeline->getInOutRange().max){
				if(timeline->getLoopType() == OF_LOOP_NONE){
//					player.setPosition(timeline->getInOutRange().max);
					player.setPosition( positionForSecond(timeline->getInTimeInSeconds()) );
					stop();
				}
				else{
//					player.setPosition(timeline->getInOutRange().min);
					player.setPosition( positionForSecond(timeline->getOutTimeInSeconds()) );
				}
			}
			
			timeline->setCurrentTimeSeconds(player.getPosition() * player.getDuration());
		}
	}
	//cout << "audio track " << player.getPosition() << " timeline " << timeline->getPercentComplete() << endl;
}
// ----------------------------------------------------------------------------
void ofxSoundPlayerFMOD::setPosition(float pct)
{
	if (getIsPlaying() == true){
		int sampleToBeAt = (int)(length * pct);
		FMOD_Channel_SetPosition(channel, sampleToBeAt, FMOD_TIMEUNIT_PCM);
	}
}
void AudioPlaybackBlock::play() {
    if (m_alwaysStartAtBegin) {
        m_player.resetPosition();
    }
    if (!getIsPlaying()) {
        m_player.play();
    }
}
//------------------------------------------------------------
void ofQuicktimeSoundPlayer::setSpeed(float spd){
	if (getIsPlaying() == true){
		if (fabs(spd - 0) < FLT_EPSILON){
			bIWasPlayingAndMySpeedWasSetToZero = true;	
		} else {
			bIWasPlayingAndMySpeedWasSetToZero = false;
		}
		SetMovieRate(soundToPlay, X2Fix(spd));
	}
	speed = spd;
}
//------------------------------------------------------------
void ofQuicktimeSoundPlayer::setPaused(bool bP){
	if (getIsPlaying() == true){
		bPaused = bP;
		if (bP == true){
			StopMovie(soundToPlay);
		} else {
			StartMovie(soundToPlay);
		}
		
	}
}
void ofxTLAudioTrack::draw(){
	
	if(!soundLoaded || player.getBuffer().size() == 0){
		ofPushStyle();
		ofSetColor(timeline->getColors().disabledColor);
		ofRectangle(bounds);
		ofPopStyle();
		return;
	}
		
	if(shouldRecomputePreview || viewIsDirty){
//		cout << "recomputing waveform for audio file " << getSoundfilePath() << endl;
		recomputePreview();
	}


    ofPushStyle();
    ofSetColor(timeline->getColors().keyColor);
    ofNoFill();
    
    for(int i = 0; i < previews.size(); i++){
        ofPushMatrix();
        ofTranslate( normalizedXtoScreenX(computedZoomBounds.min, zoomBounds)  - normalizedXtoScreenX(zoomBounds.min, zoomBounds), 0, 0);
        ofScale(computedZoomBounds.span()/zoomBounds.span(), 1, 1);
        previews[i].draw();
        ofPopMatrix();
    }
    ofPopStyle();
	
	if(getIsPlaying() || timeline->getIsPlaying()){
		ofPushStyle();
		
		//will refresh fft bins for other calls too
		vector<float>& bins = getFFTSpectrum(defaultFFTBins);
		float binWidth = bounds.width / bins.size();
		//find max
		float averagebin = 0 ;
		for(int i = 0; i < bins.size(); i++){
			maxBinReceived = MAX(maxBinReceived, bins[i]);
			averagebin += bins[i];
		}
		averagebin /= bins.size();
		
		ofFill();
		ofSetColor(timeline->getColors().disabledColor, 120);
		for(int i = 0; i < bins.size(); i++){
			float height = bounds.height * bins[i]/maxBinReceived;
			float y = bounds.y + bounds.height - height;
			ofRect(i*binWidth, y, binWidth, height);
		}
		
		ofPopStyle();
	}
}
//------------------------------------------------------------
int ofFmodSoundPlayer::getPositionMS(){
	if (getIsPlaying() == true){
		unsigned int sampleImAt;

		FMOD_Channel_GetPosition(channel, &sampleImAt, FMOD_TIMEUNIT_MS);

		return sampleImAt;
	} else {
		return 0;
	}
}
// ----------------------------------------------------------------------------
bool ofxSoundPlayerFMOD::getLoop()
{
    FMOD_MODE mode;
    if (getIsPlaying() == true)
    {
        FMOD_Channel_GetMode(channel,&mode);
        if(mode == FMOD_LOOP_NORMAL)
        {
            return true;
        }
    }
    return false;
}
//------------------------------------------------------------
float ofFmodSoundPlayer::getPosition(){
	if (getIsPlaying() == true){
		unsigned int sampleImAt;

		FMOD_Channel_GetPosition(channel, &sampleImAt, FMOD_TIMEUNIT_PCM);

		float pct = 0.0f;
		if (length > 0){
			pct = sampleImAt / (float)length;
		}
		return pct;
	} else {
		return 0;
	}
}
//------------------------------------------------------------
float ofQuicktimeSoundPlayer::getPosition(){
	if (getIsPlaying() == true){
		unsigned int sampleImAt;

		
		// TODO: get position
		
		float pct = 0.0f;
		if (length > 0){
			pct = sampleImAt / (float)length;
		}
		return pct;
	} else {
		return 0;
	}
}
Exemple #21
0
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 ofxTLVideoTrack::play(){
    if(isLoaded() && !getIsPlaying()){
        if(player->getIsMovieDone()){
			player->setFrame(inFrame);
		}
		if(getCurrentFrame() >= timeline->getOutFrame()){
			player->setFrame(timeline->getInFrame());
		}
//		#ifdef OF_VIDEO_PLAYER_QUICKTIME
		player->setSpeed(1.0);
//		#endif
		player->play();
		ofxTLPlaybackEventArgs args = timeline->createPlaybackEvent();
		ofNotifyEvent(events().playbackStarted, args);
		
        currentlyPlaying = true;
    }
}
void ofxTLVideoTrack::stop(){
	if(!isLoaded()){
		return;
	}

	player->setSpeed(0);
    if(isLoaded() && getIsPlaying()){
//		cout << "stopping playback" << endl;
		#ifdef OF_VIDEO_PLAYER_QUICKTIME
        player->setSpeed(0.0);
		#else
		player->stop();
		#endif
        ofxTLPlaybackEventArgs args = timeline->createPlaybackEvent();
        ofNotifyEvent(events().playbackEnded, args);
        currentlyPlaying = false;
//		cout << "player is playing? " << player->isPlaying() << endl;
		if(timeline->getTimecontrolTrack() == this){
			timeline->setPercentComplete(player->getPosition());
		}
    }
}
//------------------------------------------------------------
void ofQuicktimeSoundPlayer::setPan(float p){
	if (getIsPlaying() == true){
		SetMovieAudioBalance(soundToPlay, pan, 0);
	}
	pan = p;
}
//------------------------------------------------------------
void ofQuicktimeSoundPlayer::setLoop(bool bLp){
	if (getIsPlaying() == true){
		// TODO: set looping
	}
	bLoop = bLp;
}
//void ofxTLVideoTrack::update(ofEventArgs& args){
void ofxTLVideoTrack::update(){
    
	if(!isLoaded()){
		return;
	}
	
   	if(timeline->getTimecontrolTrack() == this){
		
		if(timeline->getIsFrameBased()){
			timeline->setCurrentFrame(player->getCurrentFrame());
		}
		else {
			timeline->setCurrentTimeSeconds(player->getPosition()*player->getDuration());
		}

   		if(getIsPlaying()){
			//no longer used
			if(player->getCurrentFrame() < inFrame || player->getCurrentFrame() > outFrame){
	//            cout << "reset in frame from " << player->getCurrentFrame() << endl;
				player->setFrame(inFrame);
	//            cout << "	to: " << player->getCurrentFrame() << endl;
			}
			
			if(selectedFrame > player->getCurrentFrame()){
				currentLoop++;
				//cout << "LOOPED! with last frame " << lastFrame << " " << player->getCurrentFrame() << " current loop " << currentLoop << endl;
			}

			//if(timeline->getInOutRange().max < player->getPosition()){
			if(timeline->getOutFrame() <= player->getCurrentFrame() || player->getIsMovieDone()){
	//			cout << " OVER " << endl;
				if(timeline->getLoopType() == OF_LOOP_NONE){
	//				cout << " no loop g" << endl;
					stop();                
				}
				else {
					int loopFrame = timeline->getInFrame();
					selectFrame(loopFrame);
					if(playAlongToTimeline){
						player->play();
					}
					ofxTLPlaybackEventArgs args = timeline->createPlaybackEvent();
					ofNotifyEvent(events().playbackLooped, args);                
				}
			}
			//else if(timeline->getInOutRange().min > player->getPosition()){
			if(timeline->getInFrame() > player->getCurrentFrame()){
				//cout << "looping frame " << timeline->getInFrame()  << endl;
				int loopFrame = timeline->getInFrame();
				selectFrame(loopFrame);
			}
			//timeline->setPercentComplete(player->getPosition());
		}
		else{
//	        if(player->getSpeed() != 0){
//		        player->setSpeed(0);
//	        }
			if(player->isPlaying()){
				stop();
			}
		}
	}
	else {
		if(timeline->getCurrentTime() > player->getDuration()){
			player->stop();
		}
	}
	
	player->update();
//	cout << "timeline frame vs video frame " << player->getCurrentFrame() << " vs " << timeline->getCurrentFrame() << endl;
	selectedFrame = player->getCurrentFrame();
}
void ofxTLAudioTrack::draw(){
	
	if(!soundLoaded || player.getBuffer().size() == 0){
		ofPushStyle();
		ofSetColor(timeline->getColors().disabledColor);
		ofRectangle(bounds);
		ofPopStyle();
		return;
	}
		
	if(shouldRecomputePreview || viewIsDirty){
		recomputePreview();
	}

	//cout << "ofxTLAudioTrack::draw" << endl;

    ofSetColor(255, 255, 255, 205);
    ofFill();
    ofRect(bounds);
    ofPushStyle();
    //ofSetColor(timeline->getColors().keyColor);
    ofSetColor(0, 0 , 0, 255);
    ofNoFill();
    
    for(int i = 0; i < previews.size(); i++){
        ofPushMatrix();
        ofTranslate( normalizedXtoScreenX(computedZoomBounds.min, zoomBounds)  - normalizedXtoScreenX(zoomBounds.min, zoomBounds), 0, 0);
        ofScale(computedZoomBounds.span()/zoomBounds.span(), 1, 1);
        previews[i].draw();
        ofPopMatrix();
    }
    ofPopStyle();
	
	if(getIsPlaying() || timeline->getIsPlaying()){
		ofPushStyle();
		
		//will refresh fft bins for other calls too
		vector<float>& bins = getFFTSpectrum(defaultFFTBins);
		float binWidth = bounds.width / bins.size();
		//find max
		float averagebin = 0 ;
		for(int i = 0; i < bins.size(); i++){
			maxBinReceived = MAX(maxBinReceived, bins[i]);
			averagebin += bins[i];
		}
		averagebin /= bins.size();
		
		ofFill();
		ofSetColor(timeline->getColors().disabledColor, 120);
		for(int i = 0; i < bins.size(); i++){
			float height = bounds.height * bins[i]/maxBinReceived;
			float y = bounds.y + bounds.height - height;
			ofRect(bounds.x + i*binWidth, y, binWidth, height);
		}
		
		ofPopStyle();
	}

	// playhead 
	ofPushStyle();
	ofSetColor(0, 0, 0, 255);
	float x = normalizedXtoScreenX( oldpos, zoomBounds);
	ofLine(x, bounds.y, x, bounds.y + bounds.height);
	ofSetLineWidth(3);
	ofPopStyle();

	/*
	float pos = player.getPosition();
	if (pos) {
		ofxTLZoomer2D *zoom = (ofxTLZoomer2D*)timeline->getZoomer();
		ofRange z = zoom->getViewRange();
		ofRange oldz = z;
		float c = z.center(); 
		float d = pos - c;

		z.min = ofClamp(z.min + d, 0, 1); z.max = ofClamp(z.max + d, 0, 1);
		if (z.min == .0 && z.span() < oldz.span())
			z.max = oldz.max - oldz.min;
		if (z.max == 1. && z.span() < oldz.span())
			z.min = z.max - oldz.max + oldz.min;
	}*/




	// draw markers:
	for (vector<AlignMarker>::iterator m = markers.begin(); m != markers.end(); m++) {
		float xn = screenXtoNormalizedX(millisToScreenX(m->ms));
		if (zoomBounds.contains(xn)) {
			float x = timeline->normalizedXtoScreenX(xn, zoomBounds);
			if (m->selected)
				ofSetColor(255, 0, 0, 255);
			else 
				ofSetColor(0, 0, 0, 255);
			ofLine(x, bounds.y, x, bounds.y+bounds.height);
			m->rect = ofRectangle(x - 5, bounds.y + bounds.height - 12, 10, 10);
			ofSetColor(10, 0, 200, 100);
			ofFill();
			ofRect(m->rect);

			//cout << "m:" << m->ms << endl;
			ofSetColor(0, 0, 0, 255);
			timeline->getFont().drawString(ofToString(m->ms), x+1, bounds.y + 30);
		}
	}
	
}
void ofFmodSoundPlayer::setPositionMS(int ms) {
	if (getIsPlaying() == true){
		FMOD_Channel_SetPosition(channel, ms, FMOD_TIMEUNIT_MS);
	}
}
void AudioPlaybackBlock::pause() {
    if (getIsPlaying()) {
        m_player.pause();
    }
}
//------------------------------------------------------------
void ofQuicktimeSoundPlayer::setVolume(float vol){
	if (getIsPlaying() == true){
		SetMovieVolume(soundToPlay, vol*255);
	}
	volume = vol;
}