2013-05-15 03:55:51 +02:00
|
|
|
|
|
|
|
|
|
|
|
#include "looper.hxx"
|
|
|
|
|
2013-07-27 19:24:04 +02:00
|
|
|
#include "config.hxx"
|
|
|
|
|
2013-05-15 03:55:51 +02:00
|
|
|
#include "jack.hxx"
|
2013-05-20 02:08:10 +02:00
|
|
|
#include "audiobuffer.hxx"
|
2013-05-16 14:38:46 +02:00
|
|
|
#include "eventhandler.hxx"
|
2013-05-19 03:01:22 +02:00
|
|
|
#include "controllerupdater.hxx"
|
2013-05-16 14:38:46 +02:00
|
|
|
|
2013-05-15 05:05:36 +02:00
|
|
|
extern Jack* jack;
|
2013-05-15 03:55:51 +02:00
|
|
|
|
2013-05-17 10:24:24 +02:00
|
|
|
Looper::Looper(int t) :
|
2013-07-31 02:05:14 +02:00
|
|
|
AudioProcessor(),
|
2013-07-31 11:55:48 +02:00
|
|
|
TimeObserver(),
|
2013-07-31 02:05:14 +02:00
|
|
|
track(t),
|
|
|
|
uiUpdateConstant(44100/30.f),
|
|
|
|
uiUpdateCounter(44100/30.f)
|
2013-05-17 10:24:24 +02:00
|
|
|
{
|
2013-05-19 23:12:31 +02:00
|
|
|
// pre-zero the internal sample
|
2013-07-31 11:55:48 +02:00
|
|
|
tmpRecordBuffer = (float*)malloc( sizeof(float) * MAX_BUFFER_SIZE );
|
|
|
|
memset( tmpRecordBuffer, 0, sizeof(float) * MAX_BUFFER_SIZE );
|
2013-07-31 02:05:14 +02:00
|
|
|
|
|
|
|
for(int i = 0; i < 10; i++ )
|
|
|
|
{
|
2013-07-31 12:46:45 +02:00
|
|
|
clips[i] = LooperClip();
|
2013-07-31 02:05:14 +02:00
|
|
|
}
|
2013-05-19 23:12:31 +02:00
|
|
|
|
2013-05-17 10:24:24 +02:00
|
|
|
// init faust pitch shift variables
|
|
|
|
fSamplingFreq = 44100;
|
|
|
|
IOTA = 0;
|
2013-07-31 02:28:18 +02:00
|
|
|
|
|
|
|
tmpBuffer.resize(MAX_BUFFER_SIZE);
|
|
|
|
|
2013-05-17 10:24:24 +02:00
|
|
|
for (int i=0; i<65536; i++)
|
|
|
|
fVec0[i] = 0;
|
2013-07-27 19:24:04 +02:00
|
|
|
for (int i=0; i<2; i++)
|
|
|
|
fRec0[i] = 0;
|
2013-05-17 10:24:24 +02:00
|
|
|
semitoneShift = 0.0f;
|
|
|
|
windowSize = 1000;
|
|
|
|
crossfadeSize = 1000;
|
|
|
|
}
|
|
|
|
|
2013-08-07 01:16:27 +02:00
|
|
|
void Looper::setRecord(int scene, bool r)
|
|
|
|
{
|
|
|
|
clips[scene].recording(r);
|
|
|
|
}
|
|
|
|
|
2013-08-07 01:33:38 +02:00
|
|
|
void Looper::play(int scene, bool r)
|
|
|
|
{
|
|
|
|
clips[scene].playing(r);
|
|
|
|
}
|
|
|
|
|
2013-07-27 20:06:28 +02:00
|
|
|
LooperClip* Looper::getClip(int scene)
|
|
|
|
{
|
2013-07-31 12:46:45 +02:00
|
|
|
return &clips[scene];
|
2013-07-27 20:06:28 +02:00
|
|
|
}
|
|
|
|
|
2013-05-18 20:52:12 +02:00
|
|
|
void Looper::midi(unsigned char* data)
|
2013-05-18 17:37:03 +02:00
|
|
|
{
|
2013-07-27 19:24:04 +02:00
|
|
|
/*
|
2013-05-18 20:52:12 +02:00
|
|
|
if ( data[0] - 144 == track )
|
|
|
|
{
|
|
|
|
switch ( data[1] )
|
|
|
|
{
|
|
|
|
case 48: setState( STATE_RECORD_QUEUED ); break;
|
2013-07-27 18:37:19 +02:00
|
|
|
case 53: case 54: case 55: case 56: case 57:
|
|
|
|
setState( STATE_PLAY_QUEUED );
|
|
|
|
setScene( data[1] - 53 );
|
|
|
|
break;
|
|
|
|
case 52: setState( STATE_STOP_QUEUED ); break;
|
2013-05-18 20:52:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if ( data[0] - 128 == track )
|
|
|
|
{
|
|
|
|
switch ( data[1] )
|
|
|
|
{
|
|
|
|
case 48: setState( STATE_STOP_QUEUED );
|
|
|
|
}
|
|
|
|
}
|
2013-05-18 21:33:13 +02:00
|
|
|
else if ( data[0] - 176 == track )
|
|
|
|
{
|
|
|
|
switch ( data[1] )
|
|
|
|
{
|
2013-07-27 22:59:20 +02:00
|
|
|
//case 7: gain = int(data[2])/127.f; break;
|
|
|
|
case 7:{
|
|
|
|
printf("volue\n");
|
|
|
|
// volume from APC
|
|
|
|
EventTrackVol e( track, data[2] / 127.f );
|
|
|
|
writeToGuiRingbuffer( &e ); }
|
|
|
|
break;
|
2013-05-18 21:33:13 +02:00
|
|
|
}
|
|
|
|
}
|
2013-07-27 19:24:04 +02:00
|
|
|
*/
|
2013-05-18 17:37:03 +02:00
|
|
|
}
|
|
|
|
|
2013-08-01 20:58:26 +02:00
|
|
|
void Looper::queuePlayScene( int sc )
|
2013-07-26 03:49:23 +02:00
|
|
|
{
|
2013-08-01 20:58:26 +02:00
|
|
|
//queuedScene = sc;
|
|
|
|
//jack->getControllerUpdater()->clipSelect(track, scene, Controller::CLIP_MODE_PLAY_QUEUED);
|
2013-07-26 03:49:23 +02:00
|
|
|
}
|
|
|
|
|
2013-07-27 19:24:04 +02:00
|
|
|
//void Looper::setState( State s) {
|
|
|
|
/*
|
2013-05-19 03:01:22 +02:00
|
|
|
// quantize recording to next bar event
|
2013-05-16 17:31:22 +02:00
|
|
|
if ( state == STATE_RECORDING )
|
|
|
|
{
|
|
|
|
stopRecordOnBar = true;
|
|
|
|
}
|
2013-05-18 20:52:12 +02:00
|
|
|
state = s;
|
2013-05-19 03:01:22 +02:00
|
|
|
updateControllers();
|
2013-07-27 19:24:04 +02:00
|
|
|
} */
|
|
|
|
|
2013-05-19 03:01:22 +02:00
|
|
|
|
|
|
|
void Looper::updateControllers()
|
|
|
|
{
|
2013-07-27 19:24:04 +02:00
|
|
|
/*
|
2013-05-18 20:52:12 +02:00
|
|
|
if (state == STATE_RECORD_QUEUED )
|
2013-05-15 03:55:51 +02:00
|
|
|
{
|
2013-05-18 20:52:12 +02:00
|
|
|
numBeats = 0;
|
2013-05-19 03:39:06 +02:00
|
|
|
jack->getControllerUpdater()->recordArm(track, true);
|
2013-07-26 03:05:35 +02:00
|
|
|
jack->getControllerUpdater()->clipSelect(track, scene, Controller::CLIP_MODE_RECORD_QUEUED);
|
2013-05-19 03:23:18 +02:00
|
|
|
}
|
|
|
|
else if (state == STATE_RECORDING )
|
|
|
|
{
|
2013-05-19 03:39:06 +02:00
|
|
|
jack->getControllerUpdater()->recordArm(track, true);
|
2013-07-26 03:05:35 +02:00
|
|
|
jack->getControllerUpdater()->clipSelect(track, scene, Controller::CLIP_MODE_RECORDING);
|
2013-05-15 03:55:51 +02:00
|
|
|
}
|
2013-05-19 02:26:18 +02:00
|
|
|
else
|
|
|
|
{
|
2013-05-19 03:39:06 +02:00
|
|
|
jack->getControllerUpdater()->recordArm(track, false);
|
2013-05-19 02:26:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (state == STATE_PLAY_QUEUED )
|
|
|
|
{
|
2013-07-26 03:05:35 +02:00
|
|
|
jack->getControllerUpdater()->clipSelect(track, scene, Controller::CLIP_MODE_PLAY_QUEUED);
|
2013-05-19 03:01:22 +02:00
|
|
|
}
|
2013-05-19 03:23:18 +02:00
|
|
|
|
|
|
|
if ( state == STATE_PLAYING )
|
2013-05-19 03:01:22 +02:00
|
|
|
{
|
2013-07-26 03:05:35 +02:00
|
|
|
jack->getControllerUpdater()->clipSelect(track, scene, Controller::CLIP_MODE_PLAYING);
|
2013-05-19 03:01:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (state == STATE_STOP_QUEUED )
|
|
|
|
{
|
2013-07-26 03:05:35 +02:00
|
|
|
jack->getControllerUpdater()->clipSelect(track, scene, Controller::CLIP_MODE_STOP_QUEUED);
|
2013-05-19 03:01:22 +02:00
|
|
|
}
|
|
|
|
else if ( state == STATE_STOPPED )
|
|
|
|
{
|
2013-07-26 03:05:35 +02:00
|
|
|
jack->getControllerUpdater()->clipSelect(track, scene, Controller::CLIP_MODE_LOADED);
|
2013-07-30 19:34:47 +02:00
|
|
|
EventLooperProgress e(track, 0 );
|
|
|
|
writeToGuiRingbuffer( &e );
|
2013-05-19 03:01:22 +02:00
|
|
|
}
|
2013-07-27 19:24:04 +02:00
|
|
|
*/
|
2013-05-15 03:55:51 +02:00
|
|
|
}
|
2013-05-16 02:38:11 +02:00
|
|
|
|
2013-08-06 23:20:37 +02:00
|
|
|
void Looper::setRequestedBuffer(int s, AudioBuffer* ab)
|
|
|
|
{
|
|
|
|
clips[s].setRequestedBuffer( ab );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-07-27 20:06:28 +02:00
|
|
|
void Looper::setSample(int scene, AudioBuffer* ab)
|
2013-05-20 00:57:12 +02:00
|
|
|
{
|
2013-07-31 12:46:45 +02:00
|
|
|
clips[scene].load( ab );
|
2013-08-12 20:06:25 +02:00
|
|
|
|
|
|
|
char buffer [50];
|
|
|
|
sprintf (buffer, "Looper setSample() writing to scene %i",scene);
|
|
|
|
EventGuiPrint e( buffer );
|
|
|
|
writeToGuiRingbuffer( &e );
|
2013-05-20 00:57:12 +02:00
|
|
|
}
|
|
|
|
|
2013-05-16 14:38:46 +02:00
|
|
|
void Looper::process(int nframes, Buffers* buffers)
|
|
|
|
{
|
|
|
|
float* in = buffers->audio[Buffers::MASTER_INPUT];
|
2013-08-12 20:06:25 +02:00
|
|
|
float* out = buffers->audio[Buffers::TRACK_0 + track];
|
2013-05-16 14:38:46 +02:00
|
|
|
|
2013-07-27 20:06:28 +02:00
|
|
|
// process each clip individually: this allows for playback of one clip,
|
|
|
|
// while another clip records.
|
2013-07-31 02:43:24 +02:00
|
|
|
for ( int clip = 0; clip < NSCENES; clip++ )
|
2013-07-27 20:06:28 +02:00
|
|
|
{
|
|
|
|
// handle state of clip, and do what needs doing:
|
|
|
|
// record into buffer, play from buffer, etc
|
2013-07-31 12:46:45 +02:00
|
|
|
if ( clips[clip].recording() )
|
2013-07-27 20:06:28 +02:00
|
|
|
{
|
2013-08-07 01:16:27 +02:00
|
|
|
if ( clips[clip].recordSpaceAvailable() < LOOPER_SAMPLES_BEFORE_REQUEST &&
|
|
|
|
!clips[clip].newBufferInTransit() )
|
2013-08-06 23:10:27 +02:00
|
|
|
{
|
2013-08-07 01:16:27 +02:00
|
|
|
EventLooperClipRequestBuffer e( track, clip, clips[clip].audioBufferSize() + 44100 * 4);
|
2013-08-06 23:10:27 +02:00
|
|
|
writeToGuiRingbuffer( &e );
|
2013-08-07 01:16:27 +02:00
|
|
|
clips[clip].newBufferInTransit(true);
|
2013-08-06 23:10:27 +02:00
|
|
|
}
|
2013-08-07 01:16:27 +02:00
|
|
|
|
|
|
|
// copy data from input buffer to recording buffer
|
|
|
|
float* input = buffers->audio[Buffers::MASTER_INPUT];
|
|
|
|
clips[clip].record( nframes, input, 0 );
|
2013-08-07 01:33:38 +02:00
|
|
|
|
2013-07-27 20:06:28 +02:00
|
|
|
}
|
2013-07-31 12:46:45 +02:00
|
|
|
else if ( clips[clip].playing() )
|
2013-07-27 20:06:28 +02:00
|
|
|
{
|
2013-07-31 02:05:14 +02:00
|
|
|
//printf("Looper %i playing()\n", track );
|
2013-07-27 20:06:28 +02:00
|
|
|
// copy data into tmpBuffer, then pitch-stretch into track buffer
|
2013-07-31 02:43:24 +02:00
|
|
|
for(int i = 0; i < nframes; i++ )
|
2013-07-31 02:05:14 +02:00
|
|
|
{
|
2013-07-31 12:46:45 +02:00
|
|
|
out[i] = clips[clip].getSample();
|
2013-07-31 02:05:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// update UI of progress
|
|
|
|
if ( uiUpdateCounter > uiUpdateConstant )
|
2013-07-27 20:06:28 +02:00
|
|
|
{
|
2013-08-04 19:04:26 +02:00
|
|
|
jack->getControllerUpdater()->setTrackSceneProgress(track, clip, clips[clip].getProgress() );
|
2013-07-31 02:05:14 +02:00
|
|
|
uiUpdateCounter = 0;
|
2013-07-27 20:06:28 +02:00
|
|
|
}
|
2013-07-31 02:05:14 +02:00
|
|
|
uiUpdateCounter += nframes;
|
2013-07-27 20:06:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2013-05-17 12:01:56 +02:00
|
|
|
float playbackSpeed = endPoint / ( float(numBeats) * fpb );
|
2013-05-19 03:34:39 +02:00
|
|
|
semitoneShift = -( 12 * log ( playbackSpeed ) / log (2) );
|
2013-05-17 12:01:56 +02:00
|
|
|
|
2013-07-26 03:05:35 +02:00
|
|
|
if ( state == STATE_PLAYING ||
|
2013-07-30 19:34:47 +02:00
|
|
|
state == STATE_STOP_QUEUED )
|
2013-05-16 14:38:46 +02:00
|
|
|
{
|
|
|
|
for(int i = 0; i < nframes; i++)
|
|
|
|
{
|
|
|
|
if ( playPoint < endPoint )
|
|
|
|
{
|
2013-05-19 03:34:39 +02:00
|
|
|
tmpBuffer[i] = sample[int(playPoint)] * gain;
|
2013-05-16 14:38:46 +02:00
|
|
|
}
|
2013-07-30 04:07:46 +02:00
|
|
|
playPoint += 1.0; //playbackSpeed;
|
2013-07-30 01:56:13 +02:00
|
|
|
|
2013-07-30 04:07:46 +02:00
|
|
|
//*out++ = sin( playPoint * 440 * 6.24 );
|
2013-07-30 19:34:47 +02:00
|
|
|
*trk = tmpBuffer[i];
|
|
|
|
*out++ = *trk++;
|
2013-05-16 14:38:46 +02:00
|
|
|
}
|
|
|
|
|
2013-05-18 21:33:13 +02:00
|
|
|
// now pitch-shift the audio in the buffer
|
2013-07-30 01:56:13 +02:00
|
|
|
//pitchShift( nframes, &tmpBuffer[0], out);
|
2013-05-17 12:01:56 +02:00
|
|
|
|
2013-07-30 19:34:47 +02:00
|
|
|
if ( uiUpdateCounter > uiUpdateConstant )
|
|
|
|
{
|
|
|
|
float prog = (float(playPoint) / (fpb*numBeats));
|
|
|
|
EventLooperProgress e(track, prog );
|
|
|
|
writeToGuiRingbuffer( &e );
|
|
|
|
}
|
|
|
|
uiUpdateCounter += nframes;
|
2013-05-16 14:38:46 +02:00
|
|
|
}
|
2013-07-30 19:34:47 +02:00
|
|
|
/*
|
2013-05-16 17:31:22 +02:00
|
|
|
// stopRecordOnBar ensures we record right up to the bar measure
|
2013-05-16 17:14:14 +02:00
|
|
|
else if ( state == STATE_RECORDING || stopRecordOnBar )
|
2013-05-16 14:38:46 +02:00
|
|
|
{
|
|
|
|
for(int i = 0; i < nframes; i++)
|
|
|
|
{
|
2013-07-30 01:56:13 +02:00
|
|
|
if ( lastWrittenSampleIndex < SAMPLE_SIZE )
|
2013-05-16 14:38:46 +02:00
|
|
|
{
|
|
|
|
sample[lastWrittenSampleIndex++] = in[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-07-30 19:34:47 +02:00
|
|
|
*/
|
2013-05-16 14:38:46 +02:00
|
|
|
}
|
2013-05-16 16:17:49 +02:00
|
|
|
|
2013-05-17 10:24:24 +02:00
|
|
|
void Looper::pitchShift(int count, float* input, float* output)
|
|
|
|
{
|
|
|
|
float fSlow0 = windowSize;
|
|
|
|
float fSlow1 = ((1 + fSlow0) - powf(2,(0.08333333333333333f * semitoneShift)));
|
|
|
|
float fSlow2 = (1.0f / crossfadeSize);
|
|
|
|
float fSlow3 = (fSlow0 - 1);
|
|
|
|
float* input0 = &input[0];
|
2013-07-30 02:17:40 +02:00
|
|
|
//float* output0 = &output[0];
|
2013-05-17 10:24:24 +02:00
|
|
|
|
|
|
|
for (int i=0; i<count; i++)
|
|
|
|
{
|
|
|
|
float fTemp0 = (float)input0[i];
|
|
|
|
fVec0[IOTA&65535] = fTemp0;
|
|
|
|
fRec0[0] = fmodf((fRec0[1] + fSlow1),fSlow0);
|
|
|
|
int iTemp1 = int(fRec0[0]);
|
|
|
|
int iTemp2 = (1 + iTemp1);
|
|
|
|
float fTemp3 = min((fSlow2 * fRec0[0]), 1.f );
|
|
|
|
float fTemp4 = (fSlow0 + fRec0[0]);
|
|
|
|
int iTemp5 = int(fTemp4);
|
2013-05-18 21:33:13 +02:00
|
|
|
|
2013-07-26 03:26:12 +02:00
|
|
|
*output++ += (float)(((1 - fTemp3) * (((fTemp4 - iTemp5) *
|
2013-05-17 10:24:24 +02:00
|
|
|
fVec0[(IOTA-int((int((1 + iTemp5)) & 65535)))&65535]) + ((0 - ((
|
|
|
|
fRec0[0] + fSlow3) - iTemp5)) * fVec0[(IOTA-int((iTemp5 & 65535)))
|
|
|
|
&65535]))) + (fTemp3 * (((fRec0[0] - iTemp1) * fVec0[(IOTA-int((int(
|
|
|
|
iTemp2) & 65535)))&65535]) + ((iTemp2 - fRec0[0]) * fVec0[(IOTA-int((
|
|
|
|
iTemp1 & 65535)))&65535]))));
|
2013-05-18 21:33:13 +02:00
|
|
|
|
2013-05-17 10:24:24 +02:00
|
|
|
fRec0[1] = fRec0[0];
|
|
|
|
IOTA = IOTA+1;
|
|
|
|
}
|
|
|
|
}
|