4 * Copyright (C) 2008 Adam Williams <broadcast at earthling dot net>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #include "bcsignals.h"
25 #include "bitspopup.h"
28 #include "filesndfile.h"
30 #include "mwindow.inc"
31 #include "mainerror.h"
33 FileSndFile::FileSndFile(Asset *asset, File *file)
34 : FileBase(asset, file)
40 // if(asset->format == FILE_UNKNOWN)
41 // asset->format = FILE_PCM;
44 FileSndFile::~FileSndFile()
46 if(temp_double) delete [] temp_double;
49 int FileSndFile::check_sig(Asset *asset)
54 SNDFILE *fd = sf_open(asset->path, SFM_READ, &fd_config);
64 void FileSndFile::asset_to_format()
68 case FILE_PCM: fd_config.format = SF_FORMAT_RAW; break;
69 case FILE_WAV: fd_config.format = SF_FORMAT_WAV; break;
70 case FILE_AU: fd_config.format = SF_FORMAT_AU; break;
71 case FILE_AIFF: fd_config.format = SF_FORMAT_AIFF; break;
74 // Not all of these are allowed in all sound formats.
75 // Raw can't be float.
79 if(asset->format == FILE_WAV)
80 fd_config.format |= SF_FORMAT_PCM_U8;
83 fd_config.format |= SF_FORMAT_PCM_S8;
85 fd_config.format |= SF_FORMAT_PCM_U8;
89 // Only signed is supported
90 fd_config.format |= SF_FORMAT_PCM_16;
92 if(asset->byte_order || asset->format == FILE_WAV)
93 fd_config.format |= SF_ENDIAN_LITTLE;
95 fd_config.format |= SF_ENDIAN_BIG;
99 fd_config.format |= SF_FORMAT_PCM_24;
101 if(asset->byte_order || asset->format == FILE_WAV)
102 fd_config.format |= SF_ENDIAN_LITTLE;
104 fd_config.format |= SF_ENDIAN_BIG;
108 fd_config.format |= SF_FORMAT_PCM_32;
110 if(asset->byte_order || asset->format == FILE_WAV)
111 fd_config.format |= SF_ENDIAN_LITTLE;
113 fd_config.format |= SF_ENDIAN_BIG;
117 fd_config.format |= SF_FORMAT_ULAW;
121 fd_config.format |= SF_FORMAT_FLOAT;
125 if(fd_config.format == FILE_WAV)
126 fd_config.format |= SF_FORMAT_MS_ADPCM;
128 fd_config.format |= SF_FORMAT_IMA_ADPCM;
129 fd_config.format |= SF_FORMAT_PCM_16;
133 fd_config.seekable = 1;
134 fd_config.samplerate = asset->sample_rate;
135 fd_config.channels = asset->channels;
136 //printf("FileSndFile::asset_to_format %x %d %d\n", fd_config.format, fd_config.pcmbitwidth, fd_config.channels);
139 void FileSndFile::format_to_asset()
141 //printf("FileSndFile::format_to_asset 1\n");
142 // User supplies values if PCM
143 if(asset->format == 0)
145 asset->byte_order = 0;
147 switch(fd_config.format & SF_FORMAT_TYPEMASK)
150 asset->format = FILE_WAV;
151 asset->byte_order = 1;
154 case SF_FORMAT_AIFF: asset->format = FILE_AIFF; break;
155 case SF_FORMAT_AU: asset->format = FILE_AU; break;
156 case SF_FORMAT_RAW: asset->format = FILE_PCM; break;
157 case SF_FORMAT_PAF: asset->format = FILE_SND; break;
158 case SF_FORMAT_SVX: asset->format = FILE_SND; break;
159 case SF_FORMAT_NIST: asset->format = FILE_SND; break;
162 switch(fd_config.format & SF_FORMAT_SUBMASK)
164 case SF_FORMAT_FLOAT:
165 asset->bits = BITSFLOAT;
168 asset->bits = BITSULAW;
170 case SF_FORMAT_IMA_ADPCM:
171 case SF_FORMAT_MS_ADPCM:
172 asset->bits = BITS_ADPCM;
174 case SF_FORMAT_PCM_16:
178 case SF_FORMAT_PCM_24:
182 case SF_FORMAT_PCM_32:
186 case SF_FORMAT_PCM_S8:
188 asset->bits = BITSLINEAR8;
190 case SF_FORMAT_PCM_U8:
192 asset->bits = BITSLINEAR8;
196 switch(fd_config.format & SF_FORMAT_ENDMASK)
198 case SF_ENDIAN_LITTLE:
199 asset->byte_order = 1;
202 asset->byte_order = 0;
206 asset->channels = fd_config.channels;
209 asset->audio_data = 1;
210 asset->audio_length = fd_config.frames;
211 if(!asset->sample_rate)
212 asset->sample_rate = fd_config.samplerate;
213 //printf("FileSndFile::format_to_asset %x %d %d %x\n", fd_config.format & SF_FORMAT_TYPEMASK, fd_config.pcmbitwidth, fd_config.samples, fd_config.format & SF_FORMAT_SUBMASK);
217 int FileSndFile::open_file(int rd, int wr)
223 if(asset->format == FILE_PCM)
226 fd = sf_open(asset->path, SFM_READ, &fd_config);
227 // Already given by user
228 if(fd) format_to_asset();
232 fd = sf_open(asset->path, SFM_READ, &fd_config);
233 // Doesn't calculate the length
234 if(fd) format_to_asset();
242 fd = sf_open(asset->path, SFM_WRITE, &fd_config);
248 eprintf("%s", sf_strerror(0));
254 int FileSndFile::close_file()
258 FileBase::close_file();
259 fd_config.format = 0;
263 int FileSndFile::set_audio_position(int64_t sample)
265 // Commented out /* && psf->dataoffset */ in sndfile.c: 761
266 if(sf_seek(fd, sample, SEEK_SET) < 0)
268 eprintf(_("sf_seek() to sample %jd failed, reason: %s\n"), sample, sf_strerror(fd));
275 int FileSndFile::read_samples(double *buffer, int64_t len)
279 //printf("FileSndFile::read_samples %jd %jd\n", file->current_sample, len);
280 // Get temp buffer for interleaved channels
281 if(len <= 0 || len > 1000000)
282 eprintf("FileSndFile::read_samples len=%jd\n", len);
285 eprintf(_("buffer=%p\n"), buffer);
287 if(temp_allocated && temp_allocated < len)
289 delete [] temp_double;
296 temp_allocated = len;
297 temp_double = new double[len * asset->channels];
300 result = !sf_read_double(fd, temp_double, len * asset->channels);
303 eprintf(_("FileSndFile::read_samples fd=%p temp_double=%p"
304 " len=%jd asset=%p asset->channels=%d\n"),
305 fd, temp_double, len, asset, asset->channels);
307 // Extract single channel
308 for(int i = 0, j = file->current_channel;
310 i++, j += asset->channels)
312 buffer[i] = temp_double[j];
318 int FileSndFile::write_samples(double **buffer, int64_t len)
322 // Get temp buffer for interleaved channels
323 //printf("FileSndFile::read_samples 1\n");
324 if(temp_allocated && temp_allocated < len)
327 delete [] temp_double;
331 //printf("FileSndFile::read_samples 2\n");
334 temp_allocated = len;
335 temp_double = new double[len * asset->channels];
338 // Interleave channels
339 for(int i = 0; i < asset->channels; i++)
341 for(int j = 0; j < len; j++)
343 double sample = buffer[i][j];
344 // Libsndfile does not limit values
345 //if(sample > 1.0 || sample < -1.0) printf("FileSndFile::write_samples %f\n", sample);
346 //printf("FileSndFile::write_samples %d %d\n", asset->bits, BITSFLOAT);
347 if(asset->bits != BITSFLOAT) CLAMP(sample, -1.0, (32767.0 / 32768.0));
348 temp_double[j * asset->channels + i] = sample;
352 result = !sf_writef_double(fd, temp_double, len);
357 void FileSndFile::get_parameters(BC_WindowBase *parent_window,
359 BC_WindowBase* &format_window,
365 SndFileConfig *window = new SndFileConfig(parent_window, asset);
366 format_window = window;
367 window->create_objects();
368 window->run_window();
373 SndFileConfig::SndFileConfig(BC_WindowBase *parent_window, Asset *asset)
374 : BC_Window(_(PROGRAM_NAME ": Audio Compression"),
375 parent_window->get_abs_cursor_x(1),
376 parent_window->get_abs_cursor_y(1),
380 this->parent_window = parent_window;
384 SndFileConfig::~SndFileConfig()
388 lock_window("SndFileConfig::~SndFileConfig");
394 void SndFileConfig::create_objects()
396 lock_window("SndFileConfig::create_objects()");
400 switch(asset->format)
405 add_tool(new BC_Title(x, y, _("Compression:")));
407 if(asset->format == FILE_WAV)
408 bits_popup = new BitsPopup(this, x, y, &asset->bits, 0, 0, 1, 1, 0);
410 bits_popup = new BitsPopup(this, x, y, &asset->bits, 0, 0, 0, 0, 0);
412 bits_popup->create_objects();
417 if(asset->format != FILE_AU)
418 add_subwindow(new BC_CheckBox(x, y, &asset->dither, _("Dither")));
420 if(asset->format == FILE_PCM)
422 add_subwindow(new BC_CheckBox(x, y, &asset->signed_, _("Signed")));
424 add_subwindow(new BC_Title(x, y, _("Byte order:")));
425 add_subwindow(hilo = new SndFileHILO(this, x + 100, y));
426 add_subwindow(lohi = new SndFileLOHI(this, x + 170, y));
428 add_subwindow(new BC_OKButton(this));
433 int SndFileConfig::close_event()
441 SndFileHILO::SndFileHILO(SndFileConfig *gui, int x, int y)
442 : BC_Radial(x, y, gui->asset->byte_order == 0, _("Hi Lo"))
446 int SndFileHILO::handle_event()
448 gui->asset->byte_order = 0;
449 gui->lohi->update(0);
456 SndFileLOHI::SndFileLOHI(SndFileConfig *gui, int x, int y)
457 : BC_Radial(x, y, gui->asset->byte_order == 1, _("Lo Hi"))
461 int SndFileLOHI::handle_event()
463 gui->asset->byte_order = 1;
464 gui->hilo->update(0);