Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 192 lines (154 sloc) 3.953 kb
fd65cee @hzeller o add basic zita filter.
authored
1 // -------------------------------------------------------------------------
2 //
3 // Copyright (C) 2009 Fons Adriaensen <fons@linuxaudio.org>
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 "zita-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
2c21e9a @hzeller o ..
authored
58 memset(&I, 0, sizeof(I)); // shut up valgrind.
fd65cee @hzeller o add basic zita filter.
authored
59 if ((_sndfile = sf_open (name, SFM_READ, &I)) == 0) return ERR_OPEN;
60
61 _mode = MODE_READ;
62
63 switch (I.format & SF_FORMAT_TYPEMASK)
64 {
65 case SF_FORMAT_CAF:
66 _type = TYPE_CAF;
67 break;
68 case SF_FORMAT_WAV:
69 _type = TYPE_WAV;
70 break;
71 case SF_FORMAT_WAVEX:
72 if (sf_command (_sndfile, SFC_WAVEX_GET_AMBISONIC, 0, 0) == SF_AMBISONIC_B_FORMAT)
73 _type = TYPE_AMB;
74 else
75 _type = TYPE_WAV;
76 }
77
78 switch (I.format & SF_FORMAT_SUBMASK)
79 {
80 case SF_FORMAT_PCM_16:
81 _form = FORM_16BIT;
82 break;
83 case SF_FORMAT_PCM_24:
84 _form = FORM_24BIT;
85 break;
86 case SF_FORMAT_PCM_32:
87 _form = FORM_32BIT;
88 break;
89 case SF_FORMAT_FLOAT:
90 _form = FORM_FLOAT;
91 break;
92 }
93
94 _rate = I.samplerate;
95 _chan = I.channels;
96 _size = I.frames;
97
98 return 0;
99 }
100
101
102 int Audiofile::open_write (const char *name, int type, int form, int rate, int chan)
103 {
104 SF_INFO I;
105
106 if (_mode) return ERR_MODE;
107 if (!rate || !chan) return ERR_OPEN;
108 reset ();
109
110 switch (type)
111 {
112 case TYPE_CAF:
113 I.format = SF_FORMAT_CAF;
114 break;
115 case TYPE_WAV:
116 case TYPE_AMB:
117 I.format = (chan > 2) ? SF_FORMAT_WAVEX : SF_FORMAT_WAV;
118 break;
119 default:
120 return ERR_TYPE;
121 }
122
123 switch (form)
124 {
125 case FORM_16BIT:
126 I.format |= SF_FORMAT_PCM_16;
127 break;
128 case FORM_24BIT:
129 I.format |= SF_FORMAT_PCM_24;
130 break;
131 case FORM_32BIT:
132 I.format |= SF_FORMAT_PCM_32;
133 break;
134 case FORM_FLOAT:
135 I.format |= SF_FORMAT_FLOAT;
136 break;
137 default:
138 return ERR_FORM;
139 }
140
141 I.samplerate = rate;
142 I.channels = chan;
143 I.sections = 1;
144
145 if ((_sndfile = sf_open (name, SFM_WRITE, &I)) == 0) return ERR_OPEN;
146
147 if (type == TYPE_AMB)
148 {
149 sf_command (_sndfile, SFC_WAVEX_SET_AMBISONIC, 0, SF_AMBISONIC_B_FORMAT);
150 }
151
152 _mode = MODE_WRITE;
153 _type = type;
154 _form = form;
155 _rate = rate;
156 _chan = chan;
157
158 return 0;
159 }
160
161
162 int Audiofile::close (void)
163 {
164 if (_sndfile) sf_close (_sndfile);
165 reset ();
166 return 0;
167 }
168
169
170 int Audiofile::seek (uint32_t posit)
171 {
172 if (!_sndfile) return ERR_MODE;
173 if (sf_seek (_sndfile, posit, SEEK_SET) != posit) return ERR_SEEK;
174 return 0;
175 }
176
177
178 int Audiofile::read (float *data, uint32_t frames)
179 {
180 if (_mode != MODE_READ) return ERR_MODE;
181 return sf_readf_float (_sndfile, data, frames);
182 }
183
184
185 int Audiofile::write (float *data, uint32_t frames)
186 {
187 if (_mode != MODE_WRITE) return ERR_MODE;
188 return sf_writef_float (_sndfile, data, frames);
189 }
190
191
Something went wrong with that request. Please try again.