1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480
|
// ------------------------------------------------------------------------
// audioio_sndfile.cpp: Interface to the sndfile library.
// Copyright (C) 2003-2004,2006-2007,2009,2012 Kai Vehmanen
// Copyright (C) 2004 Jesse Chappell
//
// Attributes:
// eca-style-version: 3 (see Ecasound Programmer's Guide)
//
// References:
// http://www.mega-nerd.com/libsndfile/
//
// 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 2 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, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
// ------------------------------------------------------------------------
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <algorithm>
#include <string>
#include <iostream>
#include <fstream>
#include <cmath>
#include <cstring>
#include <kvu_message_item.h>
#include <kvu_numtostr.h>
#include <kvu_dbc.h>
#include "audioio_sndfile.h"
#include "samplebuffer.h"
#include "eca-version.h"
#include "eca-error.h"
#include "eca-logger.h"
#ifdef WORDS_BIGENDIAN
static const ECA_AUDIO_FORMAT::Sample_format audioio_sndfile_sfmt = ECA_AUDIO_FORMAT::sfmt_f32_be;
#else
static const ECA_AUDIO_FORMAT::Sample_format audioio_sndfile_sfmt = ECA_AUDIO_FORMAT::sfmt_f32_le;
#endif
using namespace std;
SNDFILE_INTERFACE::SNDFILE_INTERFACE (const string& name)
{
finished_rep = false;
snd_repp = 0;
closing_rep = false;
seek_supported_rep = true;
set_label(name);
}
SNDFILE_INTERFACE::~SNDFILE_INTERFACE(void)
{
if (is_open() == true) {
close();
}
}
SNDFILE_INTERFACE* SNDFILE_INTERFACE::clone(void) const
{
SNDFILE_INTERFACE* target = new SNDFILE_INTERFACE();
for(int n = 0; n < number_of_params(); n++) {
target->set_parameter(n + 1, get_parameter(n + 1));
}
return target;
}
/**
* Parses the information given in 'sfinfo'.
*/
void SNDFILE_INTERFACE::open_parse_info(const SF_INFO* sfinfo) throw(AUDIO_IO::SETUP_ERROR&)
{
ECA_LOG_MSG(ECA_LOGGER::user_objects, "audio file format: " + kvu_numtostr(sfinfo->format & SF_FORMAT_SUBMASK));
string format;
if (sfinfo->seekable)
seek_supported_rep = true;
else
seek_supported_rep = false;
set_samples_per_second(static_cast<long int>(sfinfo->samplerate));
set_channels(sfinfo->channels);
switch(sfinfo->format & SF_FORMAT_SUBMASK)
{
case SF_FORMAT_PCM_S8: { format = "s8"; break; }
case SF_FORMAT_PCM_U8: { format = "u8"; break; }
case SF_FORMAT_PCM_16: { format = "s16"; break; }
case SF_FORMAT_PCM_24: { format = "s24"; break; }
case SF_FORMAT_PCM_32: { format = "s32"; break; }
default:
{
/* SF_FORMAT_FLOAT */
format = "f32";
break;
}
}
if (sfinfo->format & SF_ENDIAN_LITTLE)
format += "_le";
else if (sfinfo->format & SF_ENDIAN_BIG)
format += "_be";
set_sample_format_string(format);
/* note: we have no way to find out whether frame count of
* zero means an empty file, or that that the audio
* format does not provide this information, so we
* lean towards being cautious; see also finished() */
if (sfinfo->frames > 0)
set_length_in_samples(sfinfo->frames);
ECA_LOG_MSG(ECA_LOGGER::user_objects,
string("file length (frames): ") +
kvu_numtostr(sfinfo->frames));
}
/**
* Returns a list of support file extensions in lower-case.
*/
std::list<std::string> SNDFILE_INTERFACE::supported_extensions(void) const
{
list<string> exts;
int i, count;
SF_FORMAT_INFO format_info;
sf_command (0, SFC_GET_FORMAT_MAJOR_COUNT, &count, sizeof (int));
for (i = 0 ; i < count ; i++) {
format_info.format = i;
sf_command (0, SFC_GET_FORMAT_MAJOR, &format_info, sizeof (format_info)) ;
exts.push_back(string(format_info.extension));
}
return exts;
}
/**
* Discovers a matching libsndfile file format for the filename
* 'fname'. The filename extension is used to find a matching
* format.
*
* @return sndfile major format identifier
*/
int SNDFILE_INTERFACE::find_file_format(const std::string& fname)
{
int file_format = -1, i, count;
SF_FORMAT_INFO format_info;
size_t pos = fname.rfind(".");
string fext;
if (pos != string::npos) {
fext = string(fname, pos + 1);
}
ECA_LOG_MSG(ECA_LOGGER::user_objects,
string("Searching for fileformat matching extension '") +
fext + "'.");
sf_command (0, SFC_GET_FORMAT_MAJOR_COUNT, &count, sizeof (int));
for (i = 0 ; i < count ; i++) {
format_info.format = i;
sf_command (0, SFC_GET_FORMAT_MAJOR, &format_info, sizeof (format_info)) ;
if (fext == string(format_info.extension)) {
file_format = format_info.format;
ECA_LOG_MSG(ECA_LOGGER::user_objects,
string("Found matching file format: ") +
format_info.name + " (ext=." + fext + ")");
break;
}
}
if (file_format < 0) {
ECA_LOG_MSG(ECA_LOGGER::info,
string("Warning! Unknown audio format extension '")
+ fext + "', using WAV format instead.");
file_format = SF_FORMAT_WAV;
}
return file_format;
}
void SNDFILE_INTERFACE::open(void) throw(AUDIO_IO::SETUP_ERROR&)
{
SF_INFO sfinfo;
string real_filename = label();
if (real_filename == "sndfile") {
real_filename = opt_filename_rep;
}
string mod_filename = real_filename;
if (!opt_format_rep.empty()) {
mod_filename = opt_format_rep;
}
kvu_to_lowercase(mod_filename);
// need to treat raw specially for read-only opening
bool is_raw = false;
if (strstr(mod_filename.c_str(),".raw") != 0) {
is_raw = true;
}
if (io_mode() == io_read && !is_raw) {
ECA_LOG_MSG(ECA_LOGGER::info,
"Using libsndfile to open file \"" + real_filename + "\" for reading.");
snd_repp = sf_open(real_filename.c_str(), SFM_READ, &sfinfo);
if (snd_repp == NULL) {
throw(SETUP_ERROR(SETUP_ERROR::io_mode, "AUDIOIO-SNDFILE: Can't open file \"" + real_filename
+ "\" for reading."));
}
else {
open_parse_info(&sfinfo);
}
}
else {
/* write or readwrite */
int file_format = -1;
// note: support 1.0.0 formats by default, and others via
// SF_FORMAT_GET_MAJOR
file_format = find_file_format(mod_filename);
if (format_string()[0] == 'u' && bits() == 8)
file_format |= SF_FORMAT_PCM_S8;
else if (format_string()[0] == 's') {
if (bits() == 8) { file_format |= SF_FORMAT_PCM_S8; }
else if (bits() == 16) { file_format |= SF_FORMAT_PCM_16; }
else if (bits() == 24) { file_format |= SF_FORMAT_PCM_24; }
else if (bits() == 32) { file_format |= SF_FORMAT_PCM_32; }
else { file_format = 0; }
}
else if (format_string()[0] == 'f') {
if (bits() == 32) { file_format |= SF_FORMAT_FLOAT; }
else if (bits() == 64) { file_format |= SF_FORMAT_DOUBLE; }
else { file_format = 0; }
}
else { file_format = 0; }
if (file_format == 0) {
throw(SETUP_ERROR(SETUP_ERROR::io_mode, "AUDIOIO-SNDFILE: Error! Unknown audio format requested."));
}
// set endianess
if (sample_endianess() == se_little) {
file_format |= SF_ENDIAN_LITTLE;
}
else if (sample_endianess() == se_big) {
file_format |= SF_ENDIAN_BIG;
}
/* set samplerate and channels */
sfinfo.samplerate = samples_per_second();
sfinfo.channels = channels();
sfinfo.format = file_format;
if (io_mode() == io_write) {
ECA_LOG_MSG(ECA_LOGGER::info, "Using libsndfile to open file \"" +
real_filename + "\" for writing.");
/* open the file */
snd_repp = sf_open(real_filename.c_str(), SFM_WRITE, &sfinfo);
if (snd_repp == NULL) {
throw(SETUP_ERROR(SETUP_ERROR::io_mode, "AUDIOIO-SNDFILE: Can't open file \"" + real_filename
+ "\" for writing."));
}
}
else if (io_mode() == io_read) {
ECA_LOG_MSG(ECA_LOGGER::info, "Using libsndfile to open file \"" +
real_filename + "\" for reading.");
snd_repp = sf_open(real_filename.c_str(), SFM_READ, &sfinfo);
if (snd_repp == NULL) {
throw(SETUP_ERROR(SETUP_ERROR::io_mode, "AUDIOIO-SNDFILE: Can't open file \"" + real_filename
+ "\" for reading."));
}
else {
open_parse_info(&sfinfo);
}
}
else {
ECA_LOG_MSG(ECA_LOGGER::info, "Using libsndfile to open file \"" +
real_filename + "\" for read/write.");
DBC_CHECK(sf_format_check(&sfinfo));
/* io_readwrite */
snd_repp = sf_open(real_filename.c_str(), SFM_RDWR, &sfinfo);
if (snd_repp == NULL) {
/* if open fails, try with SFM_WRITE (formats like flac are not
* supported in RDWR mode */
snd_repp = sf_open(real_filename.c_str(), SFM_WRITE, &sfinfo);
if (snd_repp == NULL) {
throw(SETUP_ERROR(SETUP_ERROR::io_mode, "AUDIOIO-SNDFILE: Can't open file \"" + real_filename
+ "\" for updating (read/write)."));
}
else {
set_io_mode(io_write);
open_parse_info(&sfinfo);
}
}
else {
open_parse_info(&sfinfo);
}
}
}
/* we need to reserve extra memory as we using 32bit
* floats as the internal sample unit */
reserve_buffer_space((sizeof(float) * channels()) * buffersize());
AUDIO_IO::open();
}
void SNDFILE_INTERFACE::close(void)
{
if (is_open() == true) {
DBC_CHECK(closing_rep != true);
if (snd_repp != 0 && closing_rep != true) {
closing_rep = true;
sf_close(snd_repp);
snd_repp = 0;
}
}
AUDIO_IO::close();
closing_rep = false;
}
bool SNDFILE_INTERFACE::finished(void) const
{
if (finished_rep == true ||
(length_set() == true &&
(io_mode() == io_read && out_position()))) return true;
return false;
}
long int SNDFILE_INTERFACE::read_samples(void* target_buffer,
long int samples)
{
// samples_read = sf_read_raw(snd_repp, target_buffer, frame_size() * samples);
// samples_read /= frame_size();
samples_read = sf_readf_float(snd_repp, (float*)target_buffer, samples);
finished_rep = (samples_read < samples) ? true : false;
return samples_read;
}
void SNDFILE_INTERFACE::write_samples(void* target_buffer,
long int samples)
{
//sf_write_raw(snd_repp, target_buffer, frame_size() * samples);
sf_writef_float(snd_repp, (float*)target_buffer, samples);
}
void SNDFILE_INTERFACE::read_buffer(SAMPLE_BUFFER* sbuf)
{
// --------
DBC_REQUIRE(get_iobuf() != 0);
DBC_REQUIRE(static_cast<long int>(get_iobuf_size()) >= buffersize() * frame_size());
// --------
/* note! modified from audioio-buffered.cpp */
DBC_CHECK(interleaved_channels() == true);
/* in normal conditions this won't cause memory reallocs */
reserve_buffer_space((sizeof(float) * channels()) * buffersize());
sbuf->import_interleaved(get_iobuf(),
read_samples(get_iobuf(), buffersize()),
audioio_sndfile_sfmt,
channels());
change_position_in_samples(sbuf->length_in_samples());
}
void SNDFILE_INTERFACE::write_buffer(SAMPLE_BUFFER* sbuf)
{
// --------
DBC_REQUIRE(get_iobuf() != 0);
DBC_REQUIRE(static_cast<long int>(get_iobuf_size()) >= buffersize() * frame_size());
// --------
/* note! modified from audioio-buffered.cpp */
DBC_CHECK(interleaved_channels() == true);
/* in normal conditions this won't cause memory reallocs */
reserve_buffer_space((sizeof(float) * channels()) * buffersize());
set_buffersize(sbuf->length_in_samples());
sbuf->export_interleaved(get_iobuf(),
audioio_sndfile_sfmt,
channels());
write_samples(get_iobuf(), sbuf->length_in_samples());
change_position_in_samples(sbuf->length_in_samples());
extend_position();
}
SAMPLE_SPECS::sample_pos_t SNDFILE_INTERFACE::seek_position(SAMPLE_SPECS::sample_pos_t pos)
{
finished_rep = false;
sf_count_t res =
sf_seek(snd_repp, pos, SEEK_SET);
if (res != pos &&
length_set() == true &&
io_mode() == io_read &&
pos >= length_in_samples()) {
pos = sf_seek(snd_repp, 0, SEEK_END);
}
else if (res != pos) {
ECA_LOG_MSG(ECA_LOGGER::info,
"invalid seek for file " + opt_filename_rep +
", req was to " + kvu_numtostr(pos) +
", result was " + kvu_numtostr(res));
if (res < 0) {
res = sf_seek(snd_repp, 0, SEEK_CUR);
DBC_CHECK(res >= 0);
if (res >= 0)
pos = res;
else
pos = position_in_samples();
}
}
return pos;
}
void SNDFILE_INTERFACE::set_parameter(int param,
string value)
{
switch (param) {
case 1:
set_label(value);
break;
case 2:
opt_filename_rep = value;
break;
case 3:
opt_format_rep = value;
break;
}
}
string SNDFILE_INTERFACE::get_parameter(int param) const
{
switch (param) {
case 1:
return label();
case 2:
return opt_filename_rep;
case 3:
return opt_format_rep;
}
return "";
}
|