2013-12-08 22:44:43 +01:00
|
|
|
/*
|
|
|
|
* Author: Harry van Haaren 2013
|
|
|
|
* harryhaaren@gmail.com
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2013-05-20 00:57:12 +02:00
|
|
|
|
|
|
|
#ifndef LUPPP_AUDIOBUFFER_H
|
|
|
|
#define LUPPP_AUDIOBUFFER_H
|
|
|
|
|
|
|
|
#include <vector>
|
2013-09-05 19:33:16 +02:00
|
|
|
#include <string>
|
|
|
|
#include <cstring>
|
2013-08-13 18:03:33 +02:00
|
|
|
#include <iostream>
|
2013-09-11 17:39:00 +02:00
|
|
|
#include <stdlib.h>
|
2013-05-20 00:57:12 +02:00
|
|
|
|
2013-09-05 19:33:16 +02:00
|
|
|
using namespace std;
|
|
|
|
|
2013-08-06 23:20:37 +02:00
|
|
|
/// AudioBuffer stores float samples in a big vector.
|
2013-05-20 00:57:12 +02:00
|
|
|
class AudioBuffer
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
AudioBuffer()
|
|
|
|
{
|
2013-08-22 02:29:55 +02:00
|
|
|
ID = privateID++;
|
2013-09-06 02:28:14 +02:00
|
|
|
init();
|
2013-08-06 23:10:27 +02:00
|
|
|
}
|
|
|
|
AudioBuffer(unsigned long size)
|
|
|
|
{
|
2013-09-06 00:54:58 +02:00
|
|
|
// FIXME recorded buffers don't get an ID, using garbage IDs
|
2013-08-22 02:35:43 +02:00
|
|
|
/// no ID assigned: it *takes* the one from the previous buffer!
|
2013-09-06 02:28:14 +02:00
|
|
|
init();
|
|
|
|
buffer.resize(size);
|
|
|
|
}
|
|
|
|
|
|
|
|
void init()
|
|
|
|
{
|
2013-08-22 02:29:55 +02:00
|
|
|
numBeats = 0;
|
2013-09-06 00:07:19 +02:00
|
|
|
audioFrames = 0;
|
2013-09-11 17:39:00 +02:00
|
|
|
memset( name, 0, sizeof(char)*20 );
|
2013-11-02 15:10:26 +01:00
|
|
|
sprintf( name, "%i", ID );
|
2013-10-12 14:28:47 +02:00
|
|
|
}
|
2013-08-07 01:16:27 +02:00
|
|
|
|
2013-08-22 02:35:43 +02:00
|
|
|
/// this function is used for "resizing" an exisiting buffer, and should
|
|
|
|
/// not be called for any other reason.
|
|
|
|
void setID(int id)
|
|
|
|
{
|
|
|
|
ID = id;
|
|
|
|
}
|
2013-08-22 02:29:55 +02:00
|
|
|
|
2013-05-20 00:57:12 +02:00
|
|
|
int getID()
|
|
|
|
{
|
|
|
|
return ID;
|
|
|
|
}
|
2013-08-22 02:29:55 +02:00
|
|
|
|
2013-09-05 16:06:23 +02:00
|
|
|
void setName(const char* n)
|
|
|
|
{
|
|
|
|
if ( strlen(n) > 19 )
|
2013-09-05 19:33:16 +02:00
|
|
|
{
|
2013-09-05 16:06:23 +02:00
|
|
|
#ifdef DEBUG_BUFFER
|
2013-09-05 19:33:16 +02:00
|
|
|
cout << "AudioBuffer setName too long!" << endl;
|
|
|
|
#endif
|
|
|
|
return;
|
|
|
|
}
|
2013-09-05 16:06:23 +02:00
|
|
|
|
|
|
|
memcpy( name, n, sizeof(char)* 20 );
|
|
|
|
}
|
|
|
|
|
2013-05-20 00:57:12 +02:00
|
|
|
int getBeats()
|
|
|
|
{
|
|
|
|
return numBeats;
|
|
|
|
}
|
|
|
|
|
|
|
|
void setBeats(int b)
|
|
|
|
{
|
2013-08-22 15:36:56 +02:00
|
|
|
#ifdef DEBUG_BUFFER
|
2013-09-05 19:33:16 +02:00
|
|
|
cout << "AudioBuffer now has " << b << " beats\n" << endl;
|
2013-08-22 15:36:56 +02:00
|
|
|
#endif
|
2013-05-20 00:57:12 +02:00
|
|
|
numBeats = b;
|
|
|
|
}
|
|
|
|
|
2013-09-06 00:07:19 +02:00
|
|
|
void setAudioFrames(long af)
|
|
|
|
{
|
|
|
|
audioFrames = af;
|
|
|
|
#ifdef DEBUG_BUFFER
|
|
|
|
cout << "AudioBuffer " << ID << " has " << audioFrames << " audioFrames\n" << endl;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
long getAudioFrames()
|
|
|
|
{
|
|
|
|
return audioFrames;
|
|
|
|
}
|
|
|
|
|
2013-07-27 18:50:10 +02:00
|
|
|
std::vector<float>& getData()
|
2013-05-20 00:57:12 +02:00
|
|
|
{
|
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
void nonRtSetSample(std::vector<float>& sample)
|
|
|
|
{
|
|
|
|
buffer.swap(sample);
|
|
|
|
}
|
2013-09-11 14:07:21 +02:00
|
|
|
void nonRtResize(unsigned long size)
|
|
|
|
{
|
|
|
|
buffer.resize(size);
|
|
|
|
}
|
2013-09-11 17:39:00 +02:00
|
|
|
|
|
|
|
friend ostream& operator<<(ostream& o, const AudioBuffer& a)
|
|
|
|
{
|
|
|
|
o << "AudioBuffer " << a.name <<
|
|
|
|
" beats: " << a.numBeats <<
|
|
|
|
" audioFrames: " << a.audioFrames << endl;
|
|
|
|
return o;
|
|
|
|
}
|
2013-05-20 00:57:12 +02:00
|
|
|
|
|
|
|
protected:
|
2013-08-22 02:29:55 +02:00
|
|
|
static int privateID;
|
|
|
|
int ID;
|
2013-05-20 00:57:12 +02:00
|
|
|
|
|
|
|
int numBeats;
|
|
|
|
|
2013-09-06 00:07:19 +02:00
|
|
|
/// holds the number of samples that are usable audio, as opposed to
|
|
|
|
/// buffer.size(), which also has non-used space at the end.
|
|
|
|
long audioFrames;
|
|
|
|
|
2013-09-05 16:06:23 +02:00
|
|
|
char name[20];
|
|
|
|
|
2013-05-20 00:57:12 +02:00
|
|
|
std::vector<float> buffer;
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|