1
// -------------------------------------------------------------------------
3
// Copyright (C) 2009 Fons Adriaensen <fons@kokkinizita.net>
5
// This program is free software; you can redistribute it and/or modify
6
// it under the terms of the GNU General Public License as published by
7
// the Free Software Foundation; either version 2 of the License, or
8
// (at your option) any later version.
10
// This program is distributed in the hope that it will be useful,
11
// but WITHOUT ANY WARRANTY; without even the implied warranty of
12
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
// GNU General Public License for more details.
15
// You should have received a copy of the GNU General Public License
16
// along with this program; if not, write to the Free Software
17
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19
// -------------------------------------------------------------------------
24
#include "audiofile.h"
27
Audiofile::Audiofile (void)
33
Audiofile::~Audiofile (void)
39
void Audiofile::reset (void)
51
int Audiofile::open_read (const char *name)
55
if (_mode) return ERR_MODE;
58
if ((_sndfile = sf_open (name, SFM_READ, &I)) == 0) return ERR_OPEN;
62
switch (I.format & SF_FORMAT_TYPEMASK)
71
if (sf_command (_sndfile, SFC_WAVEX_GET_AMBISONIC, 0, 0) == SF_AMBISONIC_B_FORMAT)
77
switch (I.format & SF_FORMAT_SUBMASK)
79
case SF_FORMAT_PCM_16:
82
case SF_FORMAT_PCM_24:
85
case SF_FORMAT_PCM_32:
101
int Audiofile::open_write (const char *name, int type, int form, int rate, int chan)
105
if (_mode) return ERR_MODE;
106
if (!rate || !chan) return ERR_OPEN;
112
I.format = SF_FORMAT_CAF;
116
I.format = (chan > 2) ? SF_FORMAT_WAVEX : SF_FORMAT_WAV;
125
I.format |= SF_FORMAT_PCM_16;
128
I.format |= SF_FORMAT_PCM_24;
131
I.format |= SF_FORMAT_PCM_32;
134
I.format |= SF_FORMAT_FLOAT;
144
if ((_sndfile = sf_open (name, SFM_WRITE, &I)) == 0) return ERR_OPEN;
146
if (type == TYPE_AMB)
148
sf_command (_sndfile, SFC_WAVEX_SET_AMBISONIC, 0, SF_AMBISONIC_B_FORMAT);
161
int Audiofile::close (void)
163
if (_sndfile) sf_close (_sndfile);
169
int Audiofile::seek (uint32_t posit)
171
if (!_sndfile) return ERR_MODE;
172
if (sf_seek (_sndfile, posit, SEEK_SET) != posit) return ERR_SEEK;
177
int Audiofile::read (float *data, uint32_t frames)
179
if (_mode != MODE_READ) return ERR_MODE;
180
return sf_readf_float (_sndfile, data, frames);
184
int Audiofile::write (float *data, uint32_t frames)
186
if (_mode != MODE_WRITE) return ERR_MODE;
187
return sf_writef_float (_sndfile, data, frames);