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;
158 case SF_FORMAT_PAF: asset->format = FILE_SND; break;
159 case SF_FORMAT_SVX: asset->format = FILE_SND; break;
160 case SF_FORMAT_NIST: asset->format = FILE_SND; break;
163 switch(fd_config.format & SF_FORMAT_SUBMASK)
165 case SF_FORMAT_FLOAT:
166 asset->bits = BITSFLOAT;
169 asset->bits = BITSULAW;
171 case SF_FORMAT_IMA_ADPCM:
172 case SF_FORMAT_MS_ADPCM:
173 asset->bits = BITS_ADPCM;
175 case SF_FORMAT_PCM_16:
179 case SF_FORMAT_PCM_24:
183 case SF_FORMAT_PCM_32:
187 case SF_FORMAT_PCM_S8:
189 asset->bits = BITSLINEAR8;
191 case SF_FORMAT_PCM_U8:
193 asset->bits = BITSLINEAR8;
197 switch(fd_config.format & SF_FORMAT_ENDMASK)
199 case SF_ENDIAN_LITTLE:
200 asset->byte_order = 1;
203 asset->byte_order = 0;
207 asset->channels = fd_config.channels;
210 asset->audio_data = 1;
211 asset->audio_length = fd_config.frames;
212 if(!asset->sample_rate)
213 asset->sample_rate = fd_config.samplerate;
214 //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);
218 int FileSndFile::open_file(int rd, int wr)
224 if(asset->format == FILE_PCM)
227 fd = sf_open(asset->path, SFM_READ, &fd_config);
228 // Already given by user
229 if(fd) format_to_asset();
233 fd = sf_open(asset->path, SFM_READ, &fd_config);
234 // Doesn't calculate the length
235 if(fd) format_to_asset();
243 fd = sf_open(asset->path, SFM_WRITE, &fd_config);
249 eprintf("%s", sf_strerror(0));
255 int FileSndFile::close_file()
259 FileBase::close_file();
260 fd_config.format = 0;
264 int FileSndFile::set_audio_position(int64_t sample)
266 // Commented out /* && psf->dataoffset */ in sndfile.c: 761
267 if(sf_seek(fd, sample, SEEK_SET) < 0)
269 eprintf(_("sf_seek() to sample %jd failed, reason: %s\n"), sample, sf_strerror(fd));
276 int FileSndFile::read_samples(double *buffer, int64_t len)
280 //printf("FileSndFile::read_samples %jd %jd\n", file->current_sample, len);
281 // Get temp buffer for interleaved channels
282 if(len <= 0 || len > 1000000)
283 eprintf("FileSndFile::read_samples len=%jd\n", len);
286 eprintf(_("buffer=%p\n"), buffer);
288 if(temp_allocated && temp_allocated < len)
290 delete [] temp_double;
297 temp_allocated = len;
298 temp_double = new double[len * asset->channels];
301 result = !sf_read_double(fd, temp_double, len * asset->channels);
304 eprintf(_("FileSndFile::read_samples fd=%p temp_double=%p"
305 " len=%jd asset=%p asset->channels=%d\n"),
306 fd, temp_double, len, asset, asset->channels);
308 // Extract single channel
309 for(int i = 0, j = file->current_channel;
311 i++, j += asset->channels)
313 buffer[i] = temp_double[j];
319 int FileSndFile::write_samples(double **buffer, int64_t len)
323 // Get temp buffer for interleaved channels
324 //printf("FileSndFile::read_samples 1\n");
325 if(temp_allocated && temp_allocated < len)
328 delete [] temp_double;
332 //printf("FileSndFile::read_samples 2\n");
335 temp_allocated = len;
336 temp_double = new double[len * asset->channels];
339 // Interleave channels
340 for(int i = 0; i < asset->channels; i++)
342 for(int j = 0; j < len; j++)
344 double sample = buffer[i][j];
345 // Libsndfile does not limit values
346 //if(sample > 1.0 || sample < -1.0) printf("FileSndFile::write_samples %f\n", sample);
347 //printf("FileSndFile::write_samples %d %d\n", asset->bits, BITSFLOAT);
348 if(asset->bits != BITSFLOAT) CLAMP(sample, -1.0, (32767.0 / 32768.0));
349 temp_double[j * asset->channels + i] = sample;
353 result = !sf_writef_double(fd, temp_double, len);
358 void FileSndFile::get_parameters(BC_WindowBase *parent_window,
359 Asset *asset, BC_WindowBase* &format_window,
360 int audio_options, int video_options, EDL *edl)
364 SndFileConfig *window = new SndFileConfig(parent_window, asset);
365 format_window = window;
366 window->create_objects();
367 window->run_window();
372 SndFileConfig::SndFileConfig(BC_WindowBase *parent_window, Asset *asset)
373 : BC_Window(_(PROGRAM_NAME ": Audio Compression"),
374 parent_window->get_abs_cursor_x(1), parent_window->get_abs_cursor_y(1),
377 this->parent_window = parent_window;
379 // *** CONTEXT_HELP ***
380 context_help_set_keyword("Single File Rendering");
383 SndFileConfig::~SndFileConfig()
387 lock_window("SndFileConfig::~SndFileConfig");
393 void SndFileConfig::create_objects()
396 lock_window("SndFileConfig::create_objects");
397 int x = xs10, y = yS(10);
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 + xS(100), y));
426 add_subwindow(lohi = new SndFileLOHI(this, x + xS(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);