45 void Audiofile::reset(
void) {
60 if ((_sndfile = sf_open(name.c_str(), SFM_READ, &I)) == 0)
return ERR_OPEN;
62 switch (I.format & SF_FORMAT_TYPEMASK) {
73 #ifdef SFC_WAVEX_GET_AMBISONIC
74 if (sf_command(_sndfile, SFC_WAVEX_GET_AMBISONIC, 0, 0) == SF_AMBISONIC_B_FORMAT)
81 switch (I.format & SF_FORMAT_SUBMASK) {
82 case SF_FORMAT_PCM_16:
85 case SF_FORMAT_PCM_24:
88 case SF_FORMAT_PCM_32:
105 if (_sndfile) sf_close(_sndfile);
113 if (sf_seek(_sndfile, posit, SEEK_SET) != posit)
return ERR_SEEK;
119 return sf_readf_float(_sndfile, data, frames);
122 bool read_audio(
const std::string& filename,
unsigned int *audio_size,
int *audio_chan,
123 int *audio_type,
int *audio_form,
int *audio_rate,
float **buffer) {
127 *audio_size = *audio_chan = *audio_type = *audio_form = *audio_rate = 0;
131 *audio_size = audio.
size();
132 *audio_chan = audio.
chan();
133 *audio_type = audio.
type();
134 *audio_form = audio.
form();
135 *audio_rate = audio.
rate();
136 const unsigned int limit = 2000000;
137 if (*audio_size > limit) {
139 "jconvolver", (boost::format(_(
"too many samples (%1%), truncated to %2%"))
140 % *audio_size % limit).str());
143 if (*audio_size * *audio_chan == 0) {
145 *audio_size = *audio_chan = *audio_type = *audio_form = *audio_rate = 0;
149 *buffer =
new float[*audio_size * *audio_chan];
150 if (audio.
read(*buffer, *audio_size) !=
static_cast<int>(*audio_size)) {
153 *audio_size = *audio_chan = *audio_type = *audio_form = *audio_rate = 0;
171 unsigned int audio_size,
unsigned int& count,
unsigned int& offset,
172 unsigned int& delay,
unsigned int& ldelay,
unsigned int& length,
173 unsigned int& size,
unsigned int& bufsize) {
175 if (bufsize < count) {
178 if (bufsize < Convproc::MINPART) {
179 bufsize = Convproc::MINPART;
181 if (offset > audio_size) {
185 if (offset + length > audio_size) {
188 (boost::format(
"length adjusted (%1% + %2% > %3%")
189 % offset % length % audio_size).str());
190 length = audio_size - offset;
193 length = audio_size - offset;
195 size =
max(delay, ldelay) + offset + length;
203 if (offset > size -
max(delay, ldelay)) {
204 offset = size -
max(delay, ldelay);
206 if (length > size -
max(delay, ldelay) - offset) {
207 length = size -
max(delay, ldelay) - offset;
211 length = size -
max(delay, ldelay) - offset;
217 int rc = start_process(priority, policy);
227 if (state() == Convproc::ST_WAIT) {
233 }
else if (state() == ST_STOP) {
267 bool GxConvolver::read_sndfile(
268 Audiofile& audio,
int nchan,
int samplerate,
const float *gain,
269 unsigned int *delay,
unsigned int offset,
unsigned int length,
270 const Gainline& points) {
277 const unsigned int BSIZE = 0x8000;
280 if (offset && audio.seek(offset)) {
286 buff =
new float[BSIZE * audio.chan()];
294 "convolver", Glib::ustring::compose(
295 _(
"resampling from %1 to %2"), audio.rate(),
samplerate));
313 unsigned int idx = 0;
314 double gp = 0.0, fct = 0.0;
316 while ((
unsigned int)points[idx].i < offset) {
318 assert(idx < points.size());
320 if ((
unsigned int)points[idx].i > offset) {
328 nfram = (length > BSIZE) ? BSIZE : length;
330 nfram = audio.read(buff, nfram);
338 for (
int ix = 0; ix < nfram; ix++) {
339 if (idx+1 < points.size() && (
unsigned int)points[idx].i == offset + ix) {
343 for (
int ichan = 0; ichan <
min(audio.chan(), nchan); ichan++) {
344 buff[ix*audio.chan()+ichan] *= pow(10, gp + ix*fct) * gain[ichan];
351 cnt = resamp.
process(nfram, buff, rbuff);
355 cnt = resamp.
flush(rbuff);
363 for (
int ichan = 0; ichan < nchan; ichan++) {
365 if (ichan >= audio.chan()) {
366 rc = impdata_copy(0, 0, ichan, ichan);
368 rc = impdata_create(ichan, ichan, audio.chan(), bufp + ichan,
369 delay[ichan], delay[ichan] + cnt);
392 string fname,
float gain,
float lgain,
393 unsigned int delay,
unsigned int ldelay,
unsigned int offset,
394 unsigned int length,
unsigned int size,
unsigned int bufsize,
395 const Gainline& points) {
401 if (audio.open_read(fname)) {
402 gx_print_error(
"convolver", Glib::ustring::compose(
"Unable to open '%1'", fname));
405 if (audio.chan() > 2) {
408 Glib::ustring::compose(
"only taking first 2 of %1 channels in impulse response", audio.chan()));
413 if (
samplerate !=
static_cast<unsigned int>(audio.rate())) {
415 size = round(size * f) + 2;
416 delay = round(delay * f);
417 ldelay = round(ldelay * f);
419 #if ZITA_CONVOLVER_VERSION == 4
420 if (Convproc::configure(2, 2, size,
buffersize, bufsize, Convproc::MAXPART, 0.0)) {
425 if (Convproc::configure(2, 2, size,
buffersize, bufsize, Convproc::MAXPART)) {
431 float gain_a[2] = {gain, lgain};
432 unsigned int delay_a[2] = {delay, ldelay};
433 return read_sndfile(audio, 2,
samplerate, gain_a, delay_a, offset, length, points);
437 float *output1,
float *output2) {
438 if (state() != Convproc::ST_PROC) {
439 if (input1 != output1) {
440 memcpy(output1, input1, count *
sizeof(
float));
442 if (input2 != output2) {
443 memcpy(output2, input2, count *
sizeof(
float));
445 if (state() == Convproc::ST_WAIT) {
448 if (state() == ST_STOP) {
453 memcpy(inpdata(0), input1, count *
sizeof(
float));
454 memcpy(inpdata(1), input2, count *
sizeof(
float));
456 int flags = process(
sync);
458 memcpy(output1, outdata(0), count *
sizeof(
float));
459 memcpy(output2, outdata(1), count *
sizeof(
float));
464 unsigned int length,
unsigned int size,
unsigned int bufsize,
465 const Gainline& points) {
471 if (audio.open_read(fname)) {
472 gx_print_error(
"convolver", Glib::ustring::compose(
"Unable to open '%1'", fname));
475 if (audio.chan() > 1) {
478 Glib::ustring::compose(
"only taking first channel of %1 channels in impulse response", audio.chan()));
481 unsigned int ldelay = delay;
484 if (
samplerate !=
static_cast<unsigned int>(audio.rate())) {
486 size = round(size * f) + 2;
487 delay = round(delay * f);
489 #if ZITA_CONVOLVER_VERSION == 4
490 if (Convproc::configure(1, 1, size,
buffersize, bufsize, Convproc::MAXPART,0.0)) {
495 if (Convproc::configure(1, 1, size,
buffersize, bufsize, Convproc::MAXPART)) {
501 float gain_a[1] = {gain};
502 unsigned int delay_a[1] = {delay};
503 return read_sndfile(audio, 1,
samplerate, gain_a, delay_a, offset, length, points);
507 if (state() != Convproc::ST_PROC) {
508 if (input != output) {
509 memcpy(output, input, count *
sizeof(
float));
511 if (state() == Convproc::ST_WAIT) {
514 if (state() == ST_STOP) {
519 memcpy(inpdata(0), input, count *
sizeof(
float));
521 int flags = process(
sync);
523 memcpy(output, outdata(0), count *
sizeof(
float));
532 class CheckResample {
538 float *
resample(
int *count,
float *impresp,
unsigned int imprate,
unsigned int samplerate) {
539 if (imprate != samplerate) {
540 vec = resamp.
process(imprate, *count, impresp, samplerate, count);
542 boost::format msg = boost::format(
"failed to resample %1% -> %2%") % imprate % samplerate;
564 impresp = r.resample(&count, impresp, imprate,
samplerate);
570 if (bufsize < Convproc::MINPART) {
571 bufsize = Convproc::MINPART;
573 #if ZITA_CONVOLVER_VERSION == 4
574 if (Convproc::configure(1, 1, count,
buffersize,
575 bufsize, Convproc::MAXPART,0.0)) {
580 if (Convproc::configure(1, 1, count,
buffersize,
581 bufsize, Convproc::MAXPART)) {
586 if (impdata_create(0, 0, 1, impresp, 0, count)) {
594 CheckResample r(resamp);
595 impresp = r.resample(&count, impresp, imprate,
samplerate);
599 #if ZITA_CONVOLVER_VERSION == 4
602 if (impdata_update(0, 0, 1, impresp, 0, count)) {
610 if (state() != Convproc::ST_PROC) {
611 if (input != output) {
612 memcpy(output, input, count *
sizeof(
float));
614 if (state() == Convproc::ST_WAIT) {
617 if (state() == ST_STOP) {
623 if (
static_cast<unsigned int>(count) ==
buffersize)
625 memcpy(inpdata(0), input, count *
sizeof(
float));
627 flags = process(
sync);
629 memcpy(output, outdata(0), count *
sizeof(
float));
636 for(
int i = 0; i<count; ++i){
642 output[d*c] = out[d];
656 CheckResample r(resamp);
660 printf(
"no impresp\n");
665 if (bufsize < Convproc::MINPART)
667 bufsize = Convproc::MINPART;
669 #if ZITA_CONVOLVER_VERSION == 4
670 if (Convproc::configure(2, 2, count,
buffersize,
671 bufsize, bufsize,0.0))
673 printf(
"no configure\n");
678 if (Convproc::configure(2, 2, count,
buffersize,
681 printf(
"no configure\n");
685 if (impdata_create(0, 0, 1, impresp, 0, count) & impdata_create(1, 1, 1, impresp, 0, count))
687 printf(
"no impdata_create()\n");
697 CheckResample r(resamp);
698 impresp = r.resample(&count, impresp, imprate,
samplerate);
703 #if ZITA_CONVOLVER_VERSION == 4
707 if (impdata_update(0, 0, 1, impresp, 0, count) & impdata_update(1, 1, 1, impresp, 0, count))
717 if (state() != Convproc::ST_PROC)
722 memcpy(output, input, count *
sizeof(
float));
723 memcpy(output1, input1, count *
sizeof(
float));
725 if (state() == Convproc::ST_WAIT)
730 if (state() == ST_STOP)
738 if (
static_cast<unsigned int>(count) ==
buffersize)
740 memcpy(inpdata(0), input, count *
sizeof(
float));
741 memcpy(inpdata(1), input1, count *
sizeof(
float));
743 flags = process(
sync);
745 memcpy(output, outdata(0), count *
sizeof(
float));
746 memcpy(output1, outdata(1), count *
sizeof(
float));
748 float *in, *in1, *out, *out1;
755 for(
int i = 0; i<count; ++i){
762 output[d*c] = out[d];
763 output1[d*c] = out1[d];