add bluray dv, misc fixes
[goodguy/history.git] / cinelerra-5.1 / cinelerra / filesndfile.C
1
2 /*
3  * CINELERRA
4  * Copyright (C) 2008 Adam Williams <broadcast at earthling dot net>
5  *
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.
10  *
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.
15  *
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
19  *
20  */
21
22 #include "asset.h"
23 #include "assets.h"
24 #include "bcsignals.h"
25 #include "bitspopup.h"
26 #include "clip.h"
27 #include "file.h"
28 #include "filesndfile.h"
29 #include "language.h"
30 #include "mwindow.inc"
31 #include "mainerror.h"
32
33 FileSndFile::FileSndFile(Asset *asset, File *file)
34  : FileBase(asset, file)
35 {
36         temp_double = 0;
37         temp_allocated = 0;
38         fd_config.format = 0;
39         fd = 0;
40 //      if(asset->format == FILE_UNKNOWN)
41 //              asset->format = FILE_PCM;
42 }
43
44 FileSndFile::~FileSndFile()
45 {
46         if(temp_double) delete [] temp_double;
47 }
48
49 int FileSndFile::check_sig(Asset *asset)
50 {
51         int result = 0;
52         SF_INFO fd_config;
53         fd_config.format = 0;
54         SNDFILE *fd = sf_open(asset->path, SFM_READ, &fd_config);
55         if(fd)
56         {
57                 sf_close(fd);
58                 result = 1;
59         }
60
61         return result;
62 }
63
64 void FileSndFile::asset_to_format()
65 {
66         switch(asset->format)
67         {
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;
72         }
73
74 // Not all of these are allowed in all sound formats.
75 // Raw can't be float.
76         switch(asset->bits)
77         {
78                 case BITSLINEAR8:
79                         if(asset->format == FILE_WAV)
80                                 fd_config.format |= SF_FORMAT_PCM_U8;
81                         else
82                         if(asset->signed_)
83                                 fd_config.format |= SF_FORMAT_PCM_S8;
84                         else
85                                 fd_config.format |= SF_FORMAT_PCM_U8;
86                         break;
87
88                 case BITSLINEAR16:
89 // Only signed is supported
90                         fd_config.format |= SF_FORMAT_PCM_16;
91
92                         if(asset->byte_order || asset->format == FILE_WAV)
93                                 fd_config.format |= SF_ENDIAN_LITTLE;
94                         else
95                                 fd_config.format |= SF_ENDIAN_BIG;
96                         break;
97
98                 case BITSLINEAR24:
99                         fd_config.format |= SF_FORMAT_PCM_24;
100
101                         if(asset->byte_order || asset->format == FILE_WAV)
102                                 fd_config.format |= SF_ENDIAN_LITTLE;
103                         else
104                                 fd_config.format |= SF_ENDIAN_BIG;
105                         break;
106
107                 case BITSLINEAR32:
108                         fd_config.format |= SF_FORMAT_PCM_32;
109
110                         if(asset->byte_order || asset->format == FILE_WAV)
111                                 fd_config.format |= SF_ENDIAN_LITTLE;
112                         else
113                                 fd_config.format |= SF_ENDIAN_BIG;
114                         break;
115
116                 case BITSULAW:
117                         fd_config.format |= SF_FORMAT_ULAW;
118                         break;
119
120                 case BITSFLOAT:
121                         fd_config.format |= SF_FORMAT_FLOAT;
122                         break;
123
124                 case BITS_ADPCM:
125                         if(fd_config.format == FILE_WAV)
126                                 fd_config.format |= SF_FORMAT_MS_ADPCM;
127                         else
128                                 fd_config.format |= SF_FORMAT_IMA_ADPCM;
129                         fd_config.format |= SF_FORMAT_PCM_16;
130                         break;
131         }
132
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);
137 }
138
139 void FileSndFile::format_to_asset()
140 {
141 //printf("FileSndFile::format_to_asset 1\n");
142 // User supplies values if PCM
143         if(asset->format == 0)
144         {
145                 asset->byte_order = 0;
146                 asset->signed_ = 1;
147                 switch(fd_config.format & SF_FORMAT_TYPEMASK)
148                 {
149                         case SF_FORMAT_WAV:
150                                 asset->format = FILE_WAV;
151                                 asset->byte_order = 1;
152                                 asset->header = 44;
153                                 break;
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;
160                 }
161
162                 switch(fd_config.format & SF_FORMAT_SUBMASK)
163                 {
164                         case SF_FORMAT_FLOAT:
165                                 asset->bits = BITSFLOAT;
166                                 break;
167                         case SF_FORMAT_ULAW:
168                                 asset->bits = BITSULAW;
169                                 break;
170                         case SF_FORMAT_IMA_ADPCM:
171                         case SF_FORMAT_MS_ADPCM:
172                                 asset->bits = BITS_ADPCM;
173                                 break;
174                         case SF_FORMAT_PCM_16:
175                                 asset->signed_ = 1;
176                                 asset->bits = 16;
177                                 break;
178                         case SF_FORMAT_PCM_24:
179                                 asset->signed_ = 1;
180                                 asset->bits = 24;
181                                 break;
182                         case SF_FORMAT_PCM_32:
183                                 asset->signed_ = 1;
184                                 asset->bits = 32;
185                                 break;
186                         case SF_FORMAT_PCM_S8:
187                                 asset->signed_ = 1;
188                                 asset->bits = BITSLINEAR8;
189                                 break;
190                         case SF_FORMAT_PCM_U8:
191                                 asset->signed_ = 0;
192                                 asset->bits = BITSLINEAR8;
193                                 break;
194                 }
195
196                 switch(fd_config.format & SF_FORMAT_ENDMASK)
197                 {
198                         case SF_ENDIAN_LITTLE:
199                                 asset->byte_order = 1;
200                                 break;
201                         case SF_ENDIAN_BIG:
202                                 asset->byte_order = 0;
203                                 break;
204                 }
205
206                 asset->channels = fd_config.channels;
207         }
208
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);
214 //asset->dump();
215 }
216
217 int FileSndFile::open_file(int rd, int wr)
218 {
219         int result = 0;
220
221         if(rd)
222         {
223                 if(asset->format == FILE_PCM)
224                 {
225                         asset_to_format();
226                         fd = sf_open(asset->path, SFM_READ, &fd_config);
227 // Already given by user
228                         if(fd) format_to_asset();
229                 }
230                 else
231                 {
232                         fd = sf_open(asset->path, SFM_READ, &fd_config);
233 // Doesn't calculate the length
234                         if(fd) format_to_asset();
235                 }
236 SET_TRACE
237         }
238         else
239         if(wr)
240         {
241                 asset_to_format();
242                 fd = sf_open(asset->path, SFM_WRITE, &fd_config);
243         }
244
245         if(!fd)
246         {
247                 result = 1;
248                 eprintf("%s", sf_strerror(0));
249         }
250
251         return result;
252 }
253
254 int FileSndFile::close_file()
255 {
256         if(fd) sf_close(fd);
257         fd = 0;
258         FileBase::close_file();
259         fd_config.format = 0;
260         return 0;
261 }
262
263 int FileSndFile::set_audio_position(int64_t sample)
264 {
265 // Commented out /* && psf->dataoffset */ in sndfile.c: 761
266         if(sf_seek(fd, sample, SEEK_SET) < 0)
267         {
268                 eprintf(_("sf_seek() to sample %jd failed, reason: %s\n"), sample, sf_strerror(fd));
269                 sf_perror(fd);
270                 return 1;
271         }
272         return 0;
273 }
274
275 int FileSndFile::read_samples(double *buffer, int64_t len)
276 {
277         int result = 0;
278
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);
283
284         if(!buffer)
285                 eprintf(_("buffer=%p\n"), buffer);
286
287         if(temp_allocated && temp_allocated < len)
288         {
289                 delete [] temp_double;
290                 temp_double = 0;
291                 temp_allocated = 0;
292         }
293
294         if(!temp_allocated)
295         {
296                 temp_allocated = len;
297                 temp_double = new double[len * asset->channels];
298         }
299
300         result = !sf_read_double(fd, temp_double, len * asset->channels);
301
302         if(result)
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);
306
307 // Extract single channel
308         for(int i = 0, j = file->current_channel;
309                 i < len;
310                 i++, j += asset->channels)
311         {
312                 buffer[i] = temp_double[j];
313         }
314
315         return result;
316 }
317
318 int FileSndFile::write_samples(double **buffer, int64_t len)
319 {
320         int result = 0;
321
322 // Get temp buffer for interleaved channels
323 //printf("FileSndFile::read_samples 1\n");
324         if(temp_allocated && temp_allocated < len)
325         {
326                 temp_allocated = 0;
327                 delete [] temp_double;
328                 temp_double = 0;
329         }
330
331 //printf("FileSndFile::read_samples 2\n");
332         if(!temp_allocated)
333         {
334                 temp_allocated = len;
335                 temp_double = new double[len * asset->channels];
336         }
337
338 // Interleave channels
339         for(int i = 0; i < asset->channels; i++)
340         {
341                 for(int j = 0; j < len; j++)
342                 {
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;
349                 }
350         }
351
352         result = !sf_writef_double(fd, temp_double, len);
353
354         return result;
355 }
356
357 void FileSndFile::get_parameters(BC_WindowBase *parent_window,
358                 Asset *asset,
359                 BC_WindowBase* &format_window,
360                 int audio_options,
361                 int video_options)
362 {
363         if(audio_options)
364         {
365                 SndFileConfig *window = new SndFileConfig(parent_window, asset);
366                 format_window = window;
367                 window->create_objects();
368                 window->run_window();
369                 delete window;
370         }
371 }
372
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),
377         250,
378         250)
379 {
380         this->parent_window = parent_window;
381         this->asset = asset;
382 }
383
384 SndFileConfig::~SndFileConfig()
385 {
386         if(bits_popup)
387         {
388                 lock_window("SndFileConfig::~SndFileConfig");
389                 delete bits_popup;
390                 unlock_window();
391         }
392 }
393
394 void SndFileConfig::create_objects()
395 {
396         lock_window("SndFileConfig::create_objects()");
397         int x = 10, y = 10;
398
399         bits_popup = 0;
400         switch(asset->format)
401         {
402                 case FILE_WAV:
403                 case FILE_PCM:
404                 case FILE_AIFF:
405                         add_tool(new BC_Title(x, y, _("Compression:")));
406                         y += 25;
407                         if(asset->format == FILE_WAV)
408                                 bits_popup = new BitsPopup(this, x, y, &asset->bits, 0, 0, 1, 1, 0);
409                         else
410                                 bits_popup = new BitsPopup(this, x, y, &asset->bits, 0, 0, 0, 0, 0);
411                         y += 40;
412                         bits_popup->create_objects();
413                         break;
414         }
415
416         x = 10;
417         if(asset->format != FILE_AU)
418                 add_subwindow(new BC_CheckBox(x, y, &asset->dither, _("Dither")));
419         y += 30;
420         if(asset->format == FILE_PCM)
421         {
422                 add_subwindow(new BC_CheckBox(x, y, &asset->signed_, _("Signed")));
423                 y += 35;
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));
427         }
428         add_subwindow(new BC_OKButton(this));
429         show_window(1);
430         unlock_window();
431 }
432
433 int SndFileConfig::close_event()
434 {
435         set_done(0);
436         return 1;
437 }
438
439
440
441 SndFileHILO::SndFileHILO(SndFileConfig *gui, int x, int y)
442  : BC_Radial(x, y, gui->asset->byte_order == 0, _("Hi Lo"))
443 {
444         this->gui = gui;
445 }
446 int SndFileHILO::handle_event()
447 {
448         gui->asset->byte_order = 0;
449         gui->lohi->update(0);
450         return 1;
451 }
452
453
454
455
456 SndFileLOHI::SndFileLOHI(SndFileConfig *gui, int x, int y)
457  : BC_Radial(x, y, gui->asset->byte_order == 1, _("Lo Hi"))
458 {
459         this->gui = gui;
460 }
461 int SndFileLOHI::handle_event()
462 {
463         gui->asset->byte_order = 1;
464         gui->hilo->update(0);
465         return 1;
466 }
467
468