2013-08-15 18:17:16 +02:00
|
|
|
|
|
|
|
#include "looperclip.hxx"
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include "config.hxx"
|
2013-08-22 01:46:58 +02:00
|
|
|
#include "jack.hxx"
|
2013-08-15 18:17:16 +02:00
|
|
|
#include "event.hxx"
|
|
|
|
#include "eventhandler.hxx"
|
|
|
|
#include "audiobuffer.hxx"
|
|
|
|
|
2013-10-12 14:19:37 +02:00
|
|
|
#include "controllerupdater.hxx"
|
|
|
|
#include "timemanager.hxx"
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-08-22 01:46:58 +02:00
|
|
|
extern Jack* jack;
|
|
|
|
|
|
|
|
LooperClip::LooperClip(int t, int s) :
|
2013-09-06 01:28:11 +02:00
|
|
|
Stately(),
|
2013-08-22 01:46:58 +02:00
|
|
|
track(t),
|
|
|
|
scene(s)
|
2013-09-06 02:28:14 +02:00
|
|
|
{
|
2013-09-11 14:07:21 +02:00
|
|
|
_buffer = new AudioBuffer();
|
|
|
|
_buffer->nonRtResize( 4410 );
|
2013-09-06 02:28:14 +02:00
|
|
|
init();
|
|
|
|
}
|
|
|
|
|
|
|
|
void LooperClip::init()
|
2013-08-15 18:17:16 +02:00
|
|
|
{
|
2013-08-22 01:46:58 +02:00
|
|
|
_loaded = false;
|
|
|
|
_playing = false;
|
|
|
|
_recording = false;
|
|
|
|
|
|
|
|
_queuePlay = false;
|
|
|
|
_queueStop = false;
|
|
|
|
_queueRecord= false;
|
2013-08-15 18:17:16 +02:00
|
|
|
|
2013-09-06 02:28:14 +02:00
|
|
|
if ( _buffer )
|
|
|
|
{
|
|
|
|
_buffer->init();
|
|
|
|
}
|
2013-08-15 18:17:16 +02:00
|
|
|
_newBufferInTransit = false;
|
|
|
|
|
|
|
|
_playhead = 0;
|
|
|
|
_recordhead = 0;
|
2013-11-02 15:10:26 +01:00
|
|
|
|
|
|
|
|
2013-08-15 18:17:16 +02:00
|
|
|
}
|
|
|
|
|
2013-09-03 19:05:57 +02:00
|
|
|
void LooperClip::save()
|
|
|
|
{
|
2013-11-14 15:03:23 +01:00
|
|
|
// ensure the buffer exists, and is saveable (not recording)
|
|
|
|
if ( _loaded && !_recording && !_queueRecord )
|
2013-09-03 20:35:02 +02:00
|
|
|
{
|
|
|
|
char buffer [50];
|
2013-09-06 00:07:19 +02:00
|
|
|
sprintf (buffer, "LC::save() track %i, scene %i", track,scene);
|
2013-09-03 20:35:02 +02:00
|
|
|
EventGuiPrint e( buffer );
|
|
|
|
writeToGuiRingbuffer( &e );
|
|
|
|
|
2013-11-14 15:03:23 +01:00
|
|
|
int frames = _buffer->getAudioFrames();
|
|
|
|
EventRequestSaveBuffer e2( track, scene, frames );
|
2013-09-03 20:35:02 +02:00
|
|
|
writeToGuiRingbuffer( &e2 );
|
|
|
|
}
|
2013-09-03 21:06:11 +02:00
|
|
|
else
|
|
|
|
{
|
2013-11-14 14:29:22 +01:00
|
|
|
// notify of "success" of save if there *is* no state to save
|
|
|
|
Stately::success();
|
2013-09-03 21:06:11 +02:00
|
|
|
}
|
2013-09-03 19:05:57 +02:00
|
|
|
}
|
|
|
|
|
2013-09-06 00:54:58 +02:00
|
|
|
void LooperClip::reset()
|
|
|
|
{
|
|
|
|
// TODO make the LooperClip reset to initial state
|
|
|
|
if ( _loaded )
|
|
|
|
{
|
|
|
|
char buffer [50];
|
2013-09-06 02:28:14 +02:00
|
|
|
sprintf (buffer, "LC::reset() track %i, scene %i", track,scene);
|
2013-09-06 00:54:58 +02:00
|
|
|
EventGuiPrint e( buffer );
|
|
|
|
writeToGuiRingbuffer( &e );
|
|
|
|
|
2013-09-17 13:07:25 +02:00
|
|
|
// set "progress" to zero as there's no clip anymore
|
|
|
|
jack->getControllerUpdater()->setTrackSceneProgress(track, scene, 0 );
|
2013-09-06 00:54:58 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-09-06 02:28:14 +02:00
|
|
|
//SaveAble::done();
|
2013-09-06 00:54:58 +02:00
|
|
|
}
|
2013-09-06 14:24:17 +02:00
|
|
|
|
|
|
|
init();
|
2013-09-06 00:54:58 +02:00
|
|
|
}
|
|
|
|
|
2013-08-15 18:17:16 +02:00
|
|
|
/// loads a sample: eg from disk, unloading current sample if necessary
|
|
|
|
void LooperClip::load( AudioBuffer* ab )
|
|
|
|
{
|
|
|
|
_loaded = true;
|
2013-09-06 14:24:17 +02:00
|
|
|
_recording = false;
|
2013-08-15 18:17:16 +02:00
|
|
|
|
|
|
|
if ( _buffer )
|
|
|
|
{
|
|
|
|
EventDeallocateBuffer e( _buffer );
|
|
|
|
writeToGuiRingbuffer( &e );
|
|
|
|
}
|
|
|
|
|
|
|
|
_buffer = ab;
|
|
|
|
|
|
|
|
_playhead = 0;
|
|
|
|
|
|
|
|
// set the endpoint to the buffer's size
|
|
|
|
_recordhead = _buffer->getData().size();
|
2013-08-22 01:46:58 +02:00
|
|
|
|
2013-12-04 15:46:44 +01:00
|
|
|
#ifdef DEBUG_BUFFER
|
2013-08-22 01:46:58 +02:00
|
|
|
char buffer [50];
|
2013-09-06 00:07:19 +02:00
|
|
|
sprintf (buffer, "LC::load() t %i, s %i, aF %i",track, scene, int(_buffer->getAudioFrames()) );
|
2013-08-22 01:46:58 +02:00
|
|
|
EventGuiPrint e( buffer );
|
|
|
|
writeToGuiRingbuffer( &e );
|
2013-12-04 15:46:44 +01:00
|
|
|
#endif
|
2013-08-15 18:17:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void LooperClip::setRequestedBuffer( AudioBuffer* ab )
|
|
|
|
{
|
|
|
|
if ( _buffer )
|
|
|
|
{
|
|
|
|
size_t size = _buffer->getData().size();
|
2013-08-22 02:35:43 +02:00
|
|
|
memcpy( &ab->getData().at(0), &_buffer->getData().at(0), sizeof(float)*size);
|
2013-08-15 18:17:16 +02:00
|
|
|
|
2013-08-22 02:35:43 +02:00
|
|
|
ab->setID ( _buffer->getID() );
|
2013-08-22 02:29:55 +02:00
|
|
|
ab->setBeats( _buffer->getBeats() );
|
|
|
|
|
2013-08-15 18:17:16 +02:00
|
|
|
EventDeallocateBuffer e( _buffer );
|
|
|
|
writeToGuiRingbuffer( &e );
|
|
|
|
}
|
|
|
|
|
|
|
|
_buffer = ab;
|
|
|
|
|
|
|
|
_newBufferInTransit = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-09-03 20:35:02 +02:00
|
|
|
|
|
|
|
void LooperClip::recieveSaveBuffer( AudioBuffer* saveBuffer )
|
|
|
|
{
|
2013-11-13 13:03:24 +01:00
|
|
|
if ( saveBuffer->getData().size() >= _buffer->getData().at(0) )
|
|
|
|
{
|
|
|
|
// copy current contents into save buffer,
|
|
|
|
size_t framesBySize = _buffer->getAudioFrames();
|
|
|
|
memcpy( &saveBuffer->getData().at(0), &_buffer->getData().at(0), sizeof(float)*framesBySize);
|
|
|
|
|
|
|
|
saveBuffer->setID ( _buffer->getID() );
|
|
|
|
saveBuffer->setBeats( _buffer->getBeats() );
|
|
|
|
saveBuffer->setAudioFrames( _buffer->getAudioFrames() );
|
|
|
|
|
|
|
|
EventStateSaveBuffer e ( track, scene, saveBuffer );
|
|
|
|
writeToGuiRingbuffer( &e );
|
|
|
|
|
2013-11-14 14:29:22 +01:00
|
|
|
Stately::success();
|
2013-11-13 13:03:24 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-12-04 15:46:44 +01:00
|
|
|
char buffer [50];
|
2013-12-05 20:57:17 +01:00
|
|
|
sprintf (buffer, "LC:: %i, %i: can't save, buf too small",track, scene );
|
2013-12-04 15:46:44 +01:00
|
|
|
EventGuiPrint e( buffer );
|
|
|
|
writeToGuiRingbuffer( &e );
|
2013-11-14 14:29:22 +01:00
|
|
|
Stately::error("");
|
2013-11-13 13:03:24 +01:00
|
|
|
}
|
2013-09-03 20:35:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-08-15 18:17:16 +02:00
|
|
|
void LooperClip::record(int count, float* L, float* R)
|
|
|
|
{
|
|
|
|
// write "count" samples into current buffer.
|
|
|
|
if ( _buffer )
|
|
|
|
{
|
2013-09-06 14:19:53 +02:00
|
|
|
size_t size = _buffer->getData().size();
|
|
|
|
|
2013-08-15 18:17:16 +02:00
|
|
|
for(int i = 0; i < count; i++)
|
|
|
|
{
|
2013-09-06 14:19:53 +02:00
|
|
|
if ( _recordhead < size )
|
|
|
|
{
|
|
|
|
_buffer->getData().at( _recordhead ) = *L++;
|
|
|
|
_recordhead++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// break: this is *BAD*, audio data is lost but the buffer isn't here
|
2013-12-08 00:27:33 +01:00
|
|
|
// yet to hold new audio data so there's no option. This has not been
|
|
|
|
// encountered in actual usage, only during the development process.
|
2013-09-06 14:19:53 +02:00
|
|
|
char buffer [50];
|
|
|
|
sprintf (buffer, "LooperClip t %i, s %i, Error: out of mem!",track, scene );
|
|
|
|
EventGuiPrint e( buffer );
|
|
|
|
writeToGuiRingbuffer( &e );
|
2013-09-17 13:24:58 +02:00
|
|
|
#ifdef BUILD_TESTS
|
|
|
|
LUPPP_WARN("%s","buffer has no space");
|
|
|
|
#endif
|
2013-09-06 14:24:17 +02:00
|
|
|
|
|
|
|
break;
|
2013-09-06 14:19:53 +02:00
|
|
|
}
|
2013-08-15 18:17:16 +02:00
|
|
|
}
|
|
|
|
}
|
2013-08-22 02:19:22 +02:00
|
|
|
|
|
|
|
_loaded = true;
|
2013-08-15 18:17:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
unsigned long LooperClip::recordSpaceAvailable()
|
|
|
|
{
|
|
|
|
if ( _buffer )
|
|
|
|
return _buffer->getData().size() - _recordhead;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t LooperClip::audioBufferSize()
|
|
|
|
{
|
|
|
|
if ( _buffer )
|
|
|
|
{
|
|
|
|
return _buffer->getData().size();
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void LooperClip::setBeats(int beats)
|
|
|
|
{
|
|
|
|
if ( _buffer )
|
|
|
|
{
|
|
|
|
_buffer->setBeats( beats );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int LooperClip::getBeats()
|
|
|
|
{
|
|
|
|
if ( _buffer )
|
|
|
|
return _buffer->getBeats();
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
long LooperClip::getBufferLenght()
|
|
|
|
{
|
|
|
|
return _recordhead;
|
|
|
|
}
|
|
|
|
|
2013-10-12 23:03:36 +02:00
|
|
|
void LooperClip::bar()
|
2013-08-22 01:46:58 +02:00
|
|
|
{
|
|
|
|
bool change = false;
|
|
|
|
GridLogic::State s = GridLogic::STATE_EMPTY;
|
|
|
|
|
2013-09-08 01:37:30 +02:00
|
|
|
// first update the buffer, as time has passed
|
|
|
|
if ( _recording )
|
|
|
|
{
|
|
|
|
// FIXME: assumes 4 beats in a bar
|
|
|
|
_buffer->setBeats( _buffer->getBeats() + 4 );
|
|
|
|
_buffer->setAudioFrames( jack->getTimeManager()->getFpb() * _buffer->getBeats() );
|
|
|
|
}
|
|
|
|
|
2013-11-02 05:07:27 +01:00
|
|
|
if ( _playhead > 0.9 * _recordhead )
|
|
|
|
{
|
|
|
|
_playhead = 0.f;
|
|
|
|
}
|
|
|
|
|
2013-08-22 01:46:58 +02:00
|
|
|
if ( _queuePlay && _loaded )
|
|
|
|
{
|
2013-11-16 03:44:37 +01:00
|
|
|
//LUPPP_NOTE("QPLay + loaded" );
|
2013-08-22 01:46:58 +02:00
|
|
|
_playing = true;
|
|
|
|
s = GridLogic::STATE_PLAYING;
|
|
|
|
_recording = false;
|
|
|
|
_queuePlay = false;
|
|
|
|
change = true;
|
|
|
|
|
|
|
|
_playhead = 0;
|
|
|
|
}
|
2013-08-22 16:00:57 +02:00
|
|
|
else if ( _queueStop && _loaded )
|
2013-08-22 01:46:58 +02:00
|
|
|
{
|
|
|
|
_playing = false;
|
|
|
|
s = GridLogic::STATE_STOPPED;
|
|
|
|
_recording = false;
|
|
|
|
_queueStop = false;
|
|
|
|
change = true;
|
2013-08-22 03:13:14 +02:00
|
|
|
// set "progress" to zero, as we're stopped!
|
|
|
|
jack->getControllerUpdater()->setTrackSceneProgress(track, scene, 0 );
|
2013-08-22 01:46:58 +02:00
|
|
|
}
|
|
|
|
else if ( _queueRecord )
|
|
|
|
{
|
|
|
|
_recording = true;
|
|
|
|
s = GridLogic::STATE_RECORDING;
|
|
|
|
_playing = false;
|
|
|
|
_queueRecord = false;
|
|
|
|
change = true;
|
|
|
|
|
2013-09-05 20:30:57 +02:00
|
|
|
if ( _buffer )
|
|
|
|
{
|
|
|
|
_buffer->setBeats( 0 );
|
|
|
|
}
|
|
|
|
|
2013-08-22 01:46:58 +02:00
|
|
|
_recordhead = 0;
|
|
|
|
}
|
2013-08-22 16:00:57 +02:00
|
|
|
else if ( _queuePlay )
|
|
|
|
{
|
|
|
|
// clip was queued, but there's nothing loaded
|
|
|
|
_queuePlay = false;
|
|
|
|
change = true;
|
|
|
|
}
|
2013-08-22 01:46:58 +02:00
|
|
|
|
|
|
|
if ( change )
|
|
|
|
{
|
|
|
|
jack->getControllerUpdater()->setSceneState(track, scene, s );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-06 13:13:15 +02:00
|
|
|
void LooperClip::neutralize()
|
|
|
|
{
|
|
|
|
_queuePlay = false;
|
|
|
|
_queueRecord = false;
|
2013-11-02 01:13:57 +01:00
|
|
|
_queueStop = false;
|
2013-09-06 13:13:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool LooperClip::somethingQueued()
|
|
|
|
{
|
|
|
|
if ( _queuePlay || _queueStop || _queueRecord )
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2013-08-22 00:50:52 +02:00
|
|
|
|
2013-09-06 00:43:42 +02:00
|
|
|
void LooperClip::queuePlay(bool qP)
|
2013-08-22 00:50:52 +02:00
|
|
|
{
|
2013-09-06 13:13:15 +02:00
|
|
|
_queuePlay = true;
|
2013-08-22 02:19:22 +02:00
|
|
|
_queueStop = false;
|
2013-09-15 23:59:35 +02:00
|
|
|
_queueRecord = false;
|
2013-08-22 00:50:52 +02:00
|
|
|
}
|
2013-08-22 06:48:59 +02:00
|
|
|
|
2013-08-22 01:26:01 +02:00
|
|
|
void LooperClip::queueStop()
|
2013-08-22 00:50:52 +02:00
|
|
|
{
|
2013-09-16 13:40:39 +02:00
|
|
|
// comment
|
2013-11-02 01:13:57 +01:00
|
|
|
if ( _loaded )
|
|
|
|
{
|
|
|
|
_queueStop = true;
|
|
|
|
_queuePlay = false;
|
|
|
|
}
|
2013-08-22 00:50:52 +02:00
|
|
|
}
|
|
|
|
|
2013-08-22 01:26:01 +02:00
|
|
|
void LooperClip::queueRecord()
|
|
|
|
{
|
2013-08-22 02:19:22 +02:00
|
|
|
_queueRecord = true;
|
|
|
|
_queuePlay = false;
|
|
|
|
_queueStop = false;
|
2013-08-22 01:26:01 +02:00
|
|
|
}
|
|
|
|
|
2013-08-22 01:54:40 +02:00
|
|
|
GridLogic::State LooperClip::getState()
|
2013-08-22 00:50:52 +02:00
|
|
|
{
|
2013-08-22 01:54:40 +02:00
|
|
|
GridLogic::State s = GridLogic::STATE_EMPTY;
|
|
|
|
|
2013-08-22 02:19:22 +02:00
|
|
|
if ( _loaded )
|
2013-08-22 01:54:40 +02:00
|
|
|
s = GridLogic::STATE_STOPPED;
|
2013-08-22 02:19:22 +02:00
|
|
|
if ( _playing )
|
2013-08-22 01:54:40 +02:00
|
|
|
s = GridLogic::STATE_PLAYING;
|
2013-08-22 02:19:22 +02:00
|
|
|
if ( _recording )
|
2013-08-22 01:54:40 +02:00
|
|
|
s = GridLogic::STATE_RECORDING;
|
2013-08-22 02:19:22 +02:00
|
|
|
if ( _queuePlay )
|
2013-08-22 01:54:40 +02:00
|
|
|
s = GridLogic::STATE_PLAY_QUEUED;
|
2013-08-22 02:19:22 +02:00
|
|
|
if ( _queueStop )
|
2013-08-22 01:54:40 +02:00
|
|
|
s = GridLogic::STATE_STOP_QUEUED;
|
2013-08-22 02:19:22 +02:00
|
|
|
if ( _queueRecord )
|
2013-08-22 01:54:40 +02:00
|
|
|
s = GridLogic::STATE_RECORD_QUEUED;
|
|
|
|
|
|
|
|
return s;
|
2013-08-22 00:50:52 +02:00
|
|
|
}
|
|
|
|
|
2013-08-22 02:40:25 +02:00
|
|
|
bool LooperClip::playing()
|
|
|
|
{
|
|
|
|
return _playing;
|
|
|
|
}
|
|
|
|
|
2013-09-06 00:43:42 +02:00
|
|
|
bool LooperClip::getQueueStop()
|
|
|
|
{
|
|
|
|
return _queueStop;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool LooperClip::getQueuePlay()
|
|
|
|
{
|
|
|
|
return _queuePlay;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool LooperClip::getLoaded()
|
|
|
|
{
|
|
|
|
return _loaded;
|
|
|
|
}
|
|
|
|
|
2013-08-22 02:40:25 +02:00
|
|
|
bool LooperClip::recording()
|
|
|
|
{
|
|
|
|
return _recording;
|
|
|
|
}
|
|
|
|
|
2013-08-22 01:26:01 +02:00
|
|
|
void LooperClip::newBufferInTransit(bool n)
|
|
|
|
{
|
|
|
|
_newBufferInTransit = n;
|
|
|
|
}
|
2013-08-15 18:17:16 +02:00
|
|
|
|
2013-08-22 01:26:01 +02:00
|
|
|
bool LooperClip::newBufferInTransit()
|
|
|
|
{
|
|
|
|
return _newBufferInTransit;
|
|
|
|
}
|
2013-08-15 18:17:16 +02:00
|
|
|
|
|
|
|
float LooperClip::getSample(float playSpeed)
|
|
|
|
{
|
2013-08-22 00:50:52 +02:00
|
|
|
if ( _buffer && _buffer->getData().size() > 0 )
|
2013-08-15 18:17:16 +02:00
|
|
|
{
|
|
|
|
if ( _playhead >= _recordhead ||
|
|
|
|
_playhead >= _buffer->getData().size() ||
|
|
|
|
_playhead < 0 )
|
|
|
|
{
|
|
|
|
_playhead = 0;
|
2013-08-16 14:14:47 +02:00
|
|
|
|
|
|
|
EventGuiPrint e( "LooperClip resetting _playhead" );
|
2013-11-19 23:15:26 +01:00
|
|
|
//writeToGuiRingbuffer( &e );
|
2013-08-15 18:17:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<float>& v = _buffer->getData();
|
|
|
|
float tmp = v.at(_playhead);
|
|
|
|
_playhead += playSpeed;
|
|
|
|
|
|
|
|
return tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0.f;
|
|
|
|
}
|
|
|
|
|
|
|
|
float LooperClip::getProgress()
|
|
|
|
{
|
|
|
|
if ( _buffer && _playing )
|
|
|
|
{
|
|
|
|
float p = float(_playhead) / _recordhead;
|
|
|
|
//printf("LooperClip progress %f\n", p );
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
return 0.f;
|
|
|
|
}
|
2013-09-17 13:51:11 +02:00
|
|
|
|
|
|
|
#ifdef BUILD_TESTS
|
|
|
|
void LooperClip::setState( bool load, bool play, bool rec, bool qPlay, bool qStop, bool qRec )
|
|
|
|
{
|
|
|
|
_loaded = load;
|
|
|
|
_playing = play;
|
|
|
|
_recording = rec;
|
|
|
|
|
|
|
|
_queuePlay = qPlay;
|
|
|
|
_queueStop = qStop;
|
|
|
|
_queueRecord = qRec;
|
|
|
|
}
|
|
|
|
#endif
|