Credit ffmpeg team with upgrade to 6.1
[goodguy/cinelerra.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                         default:
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;
161                 }
162
163                 switch(fd_config.format & SF_FORMAT_SUBMASK)
164                 {
165                         case SF_FORMAT_FLOAT:
166                                 asset->bits = BITSFLOAT;
167                                 break;
168                         case SF_FORMAT_ULAW:
169                                 asset->bits = BITSULAW;
170                                 break;
171                         case SF_FORMAT_IMA_ADPCM:
172                         case SF_FORMAT_MS_ADPCM:
173                                 asset->bits = BITS_ADPCM;
174                                 break;
175                         case SF_FORMAT_PCM_16:
176                                 asset->signed_ = 1;
177                                 asset->bits = 16;
178                                 break;
179                         case SF_FORMAT_PCM_24:
180                                 asset->signed_ = 1;
181                                 asset->bits = 24;
182                                 break;
183                         case SF_FORMAT_PCM_32:
184                                 asset->signed_ = 1;
185                                 asset->bits = 32;
186                                 break;
187                         case SF_FORMAT_PCM_S8:
188                                 asset->signed_ = 1;
189                                 asset->bits = BITSLINEAR8;
190                                 break;
191                         case SF_FORMAT_PCM_U8:
192                                 asset->signed_ = 0;
193                                 asset->bits = BITSLINEAR8;
194                                 break;
195                 }
196
197                 switch(fd_config.format & SF_FORMAT_ENDMASK)
198                 {
199                         case SF_ENDIAN_LITTLE:
200                                 asset->byte_order = 1;
201                                 break;
202                         case SF_ENDIAN_BIG:
203                                 asset->byte_order = 0;
204                                 break;
205                 }
206
207                 asset->channels = fd_config.channels;
208         }
209
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);
215 //asset->dump();
216 }
217
218 int FileSndFile::open_file(int rd, int wr)
219 {
220         int result = 0;
221
222         if(rd)
223         {
224                 if(asset->format == FILE_PCM)
225                 {
226                         asset_to_format();
227                         fd = sf_open(asset->path, SFM_READ, &fd_config);
228 // Already given by user
229                         if(fd) format_to_asset();
230                 }
231                 else
232                 {
233                         fd = sf_open(asset->path, SFM_READ, &fd_config);
234 // Doesn't calculate the length
235                         if(fd) format_to_asset();
236                 }
237 SET_TRACE
238         }
239         else
240         if(wr)
241         {
242                 asset_to_format();
243                 fd = sf_open(asset->path, SFM_WRITE, &fd_config);
244         }
245
246         if(!fd)
247         {
248                 result = 1;
249                 eprintf("%s", sf_strerror(0));
250         }
251
252         return result;
253 }
254
255 int FileSndFile::close_file()
256 {
257         if(fd) sf_close(fd);
258         fd = 0;
259         FileBase::close_file();
260         fd_config.format = 0;
261         return 0;
262 }
263
264 int FileSndFile::set_audio_position(int64_t sample)
265 {
266 // Commented out /* && psf->dataoffset */ in sndfile.c: 761
267         if(sf_seek(fd, sample, SEEK_SET) < 0)
268         {
269                 eprintf(_("sf_seek() to sample %jd failed, reason: %s\n"), sample, sf_strerror(fd));
270                 sf_perror(fd);
271                 return 1;
272         }
273         return 0;
274 }
275
276 int FileSndFile::read_samples(double *buffer, int64_t len)
277 {
278         int result = 0;
279
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);
284
285         if(!buffer)
286                 eprintf(_("buffer=%p\n"), buffer);
287
288         if(temp_allocated && temp_allocated < len)
289         {
290                 delete [] temp_double;
291                 temp_double = 0;
292                 temp_allocated = 0;
293         }
294
295         if(!temp_allocated)
296         {
297                 temp_allocated = len;
298                 temp_double = new double[len * asset->channels];
299         }
300
301         result = !sf_read_double(fd, temp_double, len * asset->channels);
302
303         if(result)
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);
307
308 // Extract single channel
309         for(int i = 0, j = file->current_channel;
310                 i < len;
311                 i++, j += asset->channels)
312         {
313                 buffer[i] = temp_double[j];
314         }
315
316         return result;
317 }
318
319 int FileSndFile::write_samples(double **buffer, int64_t len)
320 {
321         int result = 0;
322
323 // Get temp buffer for interleaved channels
324 //printf("FileSndFile::read_samples 1\n");
325         if(temp_allocated && temp_allocated < len)
326         {
327                 temp_allocated = 0;
328                 delete [] temp_double;
329                 temp_double = 0;
330         }
331
332 //printf("FileSndFile::read_samples 2\n");
333         if(!temp_allocated)
334         {
335                 temp_allocated = len;
336                 temp_double = new double[len * asset->channels];
337         }
338
339 // Interleave channels
340         for(int i = 0; i < asset->channels; i++)
341         {
342                 for(int j = 0; j < len; j++)
343                 {
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;
350                 }
351         }
352
353         result = !sf_writef_double(fd, temp_double, len);
354
355         return result;
356 }
357
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)
361 {
362         if(audio_options)
363         {
364                 SndFileConfig *window = new SndFileConfig(parent_window, asset);
365                 format_window = window;
366                 window->create_objects();
367                 window->run_window();
368                 delete window;
369         }
370 }
371
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),
375         xS(250), yS(250))
376 {
377         this->parent_window = parent_window;
378         this->asset = asset;
379 // *** CONTEXT_HELP ***
380         context_help_set_keyword("Single File Rendering");
381 }
382
383 SndFileConfig::~SndFileConfig()
384 {
385         if(bits_popup)
386         {
387                 lock_window("SndFileConfig::~SndFileConfig");
388                 delete bits_popup;
389                 unlock_window();
390         }
391 }
392
393 void SndFileConfig::create_objects()
394 {
395         int xs10 = xS(10);
396         lock_window("SndFileConfig::create_objects");
397         int x = xs10, y = yS(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 += yS(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 += yS(40);
412                         bits_popup->create_objects();
413                         break;
414         }
415
416         x = xs10;
417         if(asset->format != FILE_AU)
418                 add_subwindow(new BC_CheckBox(x, y, &asset->dither, _("Dither")));
419         y += yS(30);
420         if(asset->format == FILE_PCM)
421         {
422                 add_subwindow(new BC_CheckBox(x, y, &asset->signed_, _("Signed")));
423                 y += yS(35);
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));
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