~ubuntu-branches/ubuntu/utopic/jconvolver/utopic-proposed

« back to all changes in this revision

Viewing changes to source/audiofile.cc

  • Committer: Bazaar Package Importer
  • Author(s): Alessio Treglia
  • Date: 2010-01-17 17:43:09 UTC
  • Revision ID: james.westby@ubuntu.com-20100117174309-hc5fbqk31xwi1aok
Tags: upstream-0.8.4
ImportĀ upstreamĀ versionĀ 0.8.4

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// -------------------------------------------------------------------------
 
2
//
 
3
//    Copyright (C) 2009 Fons Adriaensen <fons@kokkinizita.net>
 
4
//    
 
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.
 
9
//
 
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.
 
14
//
 
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.
 
18
//
 
19
// -------------------------------------------------------------------------
 
20
 
 
21
 
 
22
#include <stdlib.h>
 
23
#include <string.h>
 
24
#include "audiofile.h"
 
25
 
 
26
 
 
27
Audiofile::Audiofile (void)
 
28
{
 
29
    reset ();
 
30
}
 
31
 
 
32
 
 
33
Audiofile::~Audiofile (void)
 
34
{
 
35
    close ();
 
36
}
 
37
 
 
38
 
 
39
void Audiofile::reset (void)
 
40
{
 
41
    _sndfile = 0;
 
42
    _mode = MODE_NONE;
 
43
    _type = TYPE_OTHER;
 
44
    _form = FORM_OTHER;
 
45
    _rate = 0;
 
46
    _chan = 0;
 
47
    _size = 0;
 
48
}
 
49
 
 
50
 
 
51
int Audiofile::open_read (const char *name)
 
52
{
 
53
    SF_INFO I;
 
54
 
 
55
    if (_mode) return ERR_MODE;
 
56
    reset ();
 
57
 
 
58
    if ((_sndfile = sf_open (name, SFM_READ, &I)) == 0) return ERR_OPEN;
 
59
 
 
60
    _mode = MODE_READ;
 
61
 
 
62
    switch (I.format & SF_FORMAT_TYPEMASK)
 
63
    {
 
64
    case SF_FORMAT_CAF:
 
65
        _type = TYPE_CAF;
 
66
        break;
 
67
    case SF_FORMAT_WAV:
 
68
        _type = TYPE_WAV;
 
69
        break;
 
70
    case SF_FORMAT_WAVEX:
 
71
        if (sf_command (_sndfile, SFC_WAVEX_GET_AMBISONIC, 0, 0) == SF_AMBISONIC_B_FORMAT)
 
72
            _type = TYPE_AMB;
 
73
        else
 
74
            _type = TYPE_WAV;
 
75
    }
 
76
 
 
77
    switch (I.format & SF_FORMAT_SUBMASK)
 
78
    {
 
79
    case SF_FORMAT_PCM_16:
 
80
        _form = FORM_16BIT;
 
81
        break;
 
82
    case SF_FORMAT_PCM_24:
 
83
        _form = FORM_24BIT;
 
84
        break;
 
85
    case SF_FORMAT_PCM_32:
 
86
        _form = FORM_32BIT;
 
87
        break;
 
88
    case SF_FORMAT_FLOAT:
 
89
        _form = FORM_FLOAT;
 
90
        break;
 
91
    }
 
92
 
 
93
    _rate = I.samplerate;
 
94
    _chan = I.channels;
 
95
    _size = I.frames;
 
96
 
 
97
    return 0;
 
98
}
 
99
 
 
100
 
 
101
int Audiofile::open_write (const char *name, int type, int form, int rate, int chan)    
 
102
{
 
103
    SF_INFO I;
 
104
 
 
105
    if (_mode) return ERR_MODE;
 
106
    if (!rate || !chan) return ERR_OPEN;
 
107
    reset ();
 
108
 
 
109
    switch (type)
 
110
    {
 
111
    case TYPE_CAF:
 
112
        I.format = SF_FORMAT_CAF;
 
113
        break;
 
114
    case TYPE_WAV:
 
115
    case TYPE_AMB:
 
116
        I.format = (chan > 2) ? SF_FORMAT_WAVEX : SF_FORMAT_WAV;
 
117
        break;
 
118
    default:
 
119
        return ERR_TYPE;
 
120
    }
 
121
 
 
122
    switch (form)
 
123
    {
 
124
    case FORM_16BIT:
 
125
        I.format |= SF_FORMAT_PCM_16;
 
126
        break;
 
127
    case FORM_24BIT:
 
128
        I.format |= SF_FORMAT_PCM_24;
 
129
        break;
 
130
    case FORM_32BIT:
 
131
        I.format |= SF_FORMAT_PCM_32;
 
132
        break;
 
133
    case FORM_FLOAT:
 
134
        I.format |= SF_FORMAT_FLOAT;
 
135
        break;
 
136
    default:
 
137
        return ERR_FORM;
 
138
    }
 
139
 
 
140
    I.samplerate = rate;
 
141
    I.channels = chan;
 
142
    I.sections = 1;
 
143
 
 
144
    if ((_sndfile = sf_open (name, SFM_WRITE, &I)) == 0) return ERR_OPEN;
 
145
 
 
146
    if (type == TYPE_AMB)
 
147
    {
 
148
        sf_command (_sndfile, SFC_WAVEX_SET_AMBISONIC, 0, SF_AMBISONIC_B_FORMAT);
 
149
    }
 
150
 
 
151
    _mode = MODE_WRITE;
 
152
    _type = type;
 
153
    _form = form;
 
154
    _rate = rate;
 
155
    _chan = chan;
 
156
 
 
157
    return 0;
 
158
}
 
159
 
 
160
 
 
161
int Audiofile::close (void)
 
162
{
 
163
    if (_sndfile) sf_close (_sndfile);
 
164
    reset ();
 
165
    return 0;
 
166
}
 
167
 
 
168
 
 
169
int Audiofile::seek (uint32_t posit)
 
170
{
 
171
    if (!_sndfile) return ERR_MODE;
 
172
    if (sf_seek (_sndfile, posit, SEEK_SET) != posit) return ERR_SEEK;
 
173
    return 0;
 
174
}
 
175
 
 
176
 
 
177
int Audiofile::read (float *data, uint32_t frames)
 
178
{
 
179
    if (_mode != MODE_READ) return ERR_MODE;
 
180
    return sf_readf_float (_sndfile, data, frames);
 
181
}
 
182
 
 
183
 
 
184
int Audiofile::write (float *data, uint32_t frames)
 
185
{
 
186
    if (_mode != MODE_WRITE) return ERR_MODE;
 
187
    return sf_writef_float (_sndfile, data, frames);
 
188
}
 
189
 
 
190