Loopp/src/looperclip.cxx

192 lines
3.0 KiB
C++
Raw Normal View History

2013-08-15 18:17:16 +02:00
#include "looperclip.hxx"
#include <stdio.h>
#include "config.hxx"
#include "event.hxx"
#include "eventhandler.hxx"
#include "audiobuffer.hxx"
LooperClip::LooperClip()
{
_loaded = false;
_playing = false;
_recording = false;
_buffer = 0; //new AudioBuffer(44100);
_newBufferInTransit = false;
_playhead = 0;
_recordhead = 0;
}
/// loads a sample: eg from disk, unloading current sample if necessary
void LooperClip::load( AudioBuffer* ab )
{
_loaded = true;
if ( _buffer )
{
EventDeallocateBuffer e( _buffer );
writeToGuiRingbuffer( &e );
}
_buffer = ab;
_playhead = 0;
// set the endpoint to the buffer's size
_recordhead = _buffer->getData().size();
}
void LooperClip::setRequestedBuffer( AudioBuffer* ab )
{
if ( _buffer )
{
size_t size = _buffer->getData().size();
for(size_t i = 0; i < size; i++)
{
ab->getData().at(i) = _buffer->getData().at( i );
}
EventDeallocateBuffer e( _buffer );
writeToGuiRingbuffer( &e );
}
_buffer = ab;
_newBufferInTransit = false;
}
void LooperClip::record(int count, float* L, float* R)
{
// write "count" samples into current buffer.
if ( _buffer )
{
for(int i = 0; i < count; i++)
{
_buffer->getData().at( _recordhead ) = *L++;
_recordhead++;
}
}
}
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-08-22 01:26:01 +02:00
void LooperClip::queuePlay()
{
_playing = true;
_playhead = 0;
}
2013-08-22 01:26:01 +02:00
void LooperClip::queueStop()
{
_playing = false;
_playhead = 0;
}
2013-08-22 01:26:01 +02:00
void LooperClip::queueRecord()
{
_recording = true;
_playing = false;
}
bool LooperClip::playing()
{
return _playing;
}
bool LooperClip::recording()
{
return _recording;
}
2013-08-22 01:26:01 +02:00
bool LooperClip::loaded()
{
2013-08-22 01:26:01 +02:00
return _loaded;
}
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)
{
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;
EventGuiPrint e( "LooperClip resetting _playhead" );
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;
}