Use (de|con)structors

master
Peter Nelson 2013-02-09 10:02:48 +00:00
parent 933e67c687
commit eb5be86a6e
1 changed files with 23 additions and 25 deletions

View File

@ -40,8 +40,30 @@ struct PTap
float *buffers[NUM_TAPS]; ///< Tap audio buffers
const float *buffers_end[NUM_TAPS]; ///< Audio buffer end points
float *wp[NUM_TAPS]; ///< Write pointers
PTap(double sample_rate);
~PTap();
};
PTap::PTap(double sample_rate)
{
this->sample_rate = sample_rate;
this->buffer_max = sample_rate * BUFFER_SECONDS + 1;
for (int i = 0; i < NUM_TAPS; i++) {
this->buffers[i] = new float[this->buffer_max]();
this->buffers_end[i] = this->buffers[i] + this->buffer_max;
this->wp[i] = this->buffers[i];
}
}
PTap::~PTap()
{
for (int i = 0; i < NUM_TAPS; i++) {
delete[] this->buffers[i];
}
}
static LV2_Handle ptap_instantiate(
const LV2_Descriptor *descriptor,
double sample_rate,
@ -49,26 +71,7 @@ static LV2_Handle ptap_instantiate(
const LV2_Feature *const *host_features)
{
/* Allocate local data */
PTap *ptap = new PTap();
ptap->sample_rate = sample_rate;
ptap->buffer_max = ptap->sample_rate * BUFFER_SECONDS + 1;
for (int i = 0; i < NUM_TAPS; i++) {
ptap->buffers[i] = new float[ptap->buffer_max]();
if (ptap->buffers[i] == NULL) {
for (int j = 0; j < i; j++) {
delete[] ptap->buffers[i];
}
delete ptap;
printf("Failed to allocate buffers\n");
return NULL;
}
ptap->buffers_end[i] = ptap->buffers[i] + ptap->buffer_max;
ptap->wp[i] = ptap->buffers[i];
}
PTap *ptap = new PTap(sample_rate);
return (LV2_Handle)ptap;
}
@ -172,11 +175,6 @@ static void ptap_run(LV2_Handle lv2instance, uint32_t sample_count)
static void ptap_cleanup(LV2_Handle lv2instance)
{
PTap *ptap = (PTap *)lv2instance;
for (int i = 0; i < NUM_TAPS; i++) {
delete[] ptap->buffers[i];
}
delete ptap;
}