]> git.sesse.net Git - vlc/blob - modules/audio_output/file.c
Another bunch of Unicode file names fixes (refs #528)
[vlc] / modules / audio_output / file.c
1 /*****************************************************************************
2  * file.c : audio output which writes the samples to a file
3  *****************************************************************************
4  * Copyright (C) 2002 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Christophe Massiot <massiot@via.ecp.fr>
8  *          Gildas Bazin <gbazin@netcourrier.com>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28 #include <string.h>
29 #include <stdlib.h>
30 #include <errno.h>
31
32 #include <vlc/vlc.h>
33 #include <vlc/aout.h>
34
35 #include "aout_internal.h"
36 #include "codecs.h"
37
38 #define FRAME_SIZE 2048
39 #define A52_FRAME_NB 1536
40
41 /*****************************************************************************
42  * aout_sys_t: audio output method descriptor
43  *****************************************************************************
44  * This structure is part of the audio output thread descriptor.
45  * It describes the direct sound specific properties of an audio device.
46  *****************************************************************************/
47 struct aout_sys_t
48 {
49     FILE     * p_file;
50     vlc_bool_t b_add_wav_header;
51
52     WAVEHEADER waveh;                      /* Wave header of the output file */
53 };
54
55 #define CHANNELS_MAX 6
56 static int pi_channels_maps[CHANNELS_MAX+1] =
57 {
58     0,
59     AOUT_CHAN_CENTER,
60     AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
61     AOUT_CHAN_CENTER | AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
62     AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARLEFT
63      | AOUT_CHAN_REARRIGHT,
64     AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
65      | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT,
66     AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
67      | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT | AOUT_CHAN_LFE
68 };
69
70 /*****************************************************************************
71  * Local prototypes.
72  *****************************************************************************/
73 static int     Open        ( vlc_object_t * );
74 static void    Close       ( vlc_object_t * );
75 static void    Play        ( aout_instance_t * );
76
77 /*****************************************************************************
78  * Module descriptor
79  *****************************************************************************/
80 #define FORMAT_TEXT N_("Output format")
81 #define FORMAT_LONGTEXT N_("One of \"u8\", \"s8\", \"u16\", \"s16\", " \
82     "\"u16_le\", \"s16_le\", \"u16_be\", \"s16_be\", \"fixed32\", " \
83     "\"float32\" or \"spdif\"")
84 #define CHANNELS_TEXT N_("Output channels number")
85 #define CHANNELS_LONGTEXT N_("By default, all the channels of the incoming " \
86     "will be saved but you can restrict the number of channels here.")
87
88 #define WAV_TEXT N_("Add wave header")
89 #define WAV_LONGTEXT N_("Instead of writing a raw file, you can add a WAV " \
90                         "header to the file")
91
92 static char *format_list[] = { "u8", "s8", "u16", "s16", "u16_le", "s16_le",
93                                "u16_be", "s16_be", "fixed32", "float32",
94                                "spdif" };
95 static int format_int[] = { VLC_FOURCC('u','8',' ',' '),
96                             VLC_FOURCC('s','8',' ',' '),
97                             AOUT_FMT_U16_NE, AOUT_FMT_S16_NE,
98                             VLC_FOURCC('u','1','6','l'),
99                             VLC_FOURCC('s','1','6','l'),
100                             VLC_FOURCC('u','1','6','b'),
101                             VLC_FOURCC('s','1','6','b'),
102                             VLC_FOURCC('f','i','3','2'),
103                             VLC_FOURCC('f','l','3','2'),
104                             VLC_FOURCC('s','p','i','f') };
105
106 #define FILE_TEXT N_("Output file")
107 #define FILE_LONGTEXT N_("File to which the audio samples will be written to")
108
109 vlc_module_begin();
110     set_description( N_("File audio output") );
111     set_shortname( _("File") );
112     set_category( CAT_AUDIO );
113     set_subcategory( SUBCAT_AUDIO_AOUT );
114
115     add_string( "audiofile-format", "s16", NULL,
116                 FORMAT_TEXT, FORMAT_LONGTEXT, VLC_TRUE );
117         change_string_list( format_list, 0, 0 );
118     add_integer( "audiofile-channels", 0, NULL,
119                  CHANNELS_TEXT, CHANNELS_LONGTEXT, VLC_TRUE );
120     add_file( "audiofile-file", "audiofile.wav", NULL, FILE_TEXT,
121               FILE_LONGTEXT, VLC_FALSE );
122     add_bool( "audiofile-wav", 1, NULL, WAV_TEXT, WAV_LONGTEXT, VLC_TRUE );
123
124     set_capability( "audio output", 0 );
125     add_shortcut( "file" );
126     add_shortcut( "audiofile" );
127     set_callbacks( Open, Close );
128 vlc_module_end();
129
130 /*****************************************************************************
131  * Open: open a dummy audio device
132  *****************************************************************************/
133 static int Open( vlc_object_t * p_this )
134 {
135     aout_instance_t * p_aout = (aout_instance_t *)p_this;
136     char * psz_name, * psz_format;
137     char ** ppsz_compare = format_list;
138     vlc_value_t val;
139     int i_channels, i = 0;
140
141     var_Create( p_this, "audiofile-file", VLC_VAR_STRING|VLC_VAR_DOINHERIT );
142     var_Get( p_this, "audiofile-file", &val );
143     psz_name = val.psz_string;
144     if( !psz_name || !*psz_name )
145     {
146         msg_Err( p_aout, "you need to specify an output file name" );
147         if( psz_name ) free( psz_name );
148         return VLC_EGENERIC;
149     }
150
151     /* Allocate structure */
152     p_aout->output.p_sys = malloc( sizeof( aout_sys_t ) );
153     if( p_aout->output.p_sys == NULL )
154     {
155         msg_Err( p_aout, "out of memory" );
156         return VLC_EGENERIC;
157     }
158
159     p_aout->output.p_sys->p_file = utf8_fopen( psz_name, "wb" );
160     free( psz_name );
161     if ( p_aout->output.p_sys->p_file == NULL )
162     {
163         free( p_aout->output.p_sys );
164         return VLC_EGENERIC;
165     }
166
167     p_aout->output.pf_play = Play;
168
169     /* Audio format */
170     var_Create( p_this, "audiofile-format", VLC_VAR_STRING|VLC_VAR_DOINHERIT );
171     var_Get( p_this, "audiofile-format", &val );
172     psz_format = val.psz_string;
173
174     while ( *ppsz_compare != NULL )
175     {
176         if ( !strncmp( *ppsz_compare, psz_format, strlen(*ppsz_compare) ) )
177         {
178             break;
179         }
180         ppsz_compare++; i++;
181     }
182
183     if ( *ppsz_compare == NULL )
184     {
185         msg_Err( p_aout, "cannot understand the format string (%s)",
186                  psz_format );
187         fclose( p_aout->output.p_sys->p_file );
188         free( p_aout->output.p_sys );
189         return VLC_EGENERIC;
190     }
191
192     p_aout->output.output.i_format = format_int[i];
193     if ( AOUT_FMT_NON_LINEAR( &p_aout->output.output ) )
194     {
195         p_aout->output.i_nb_samples = A52_FRAME_NB;
196         p_aout->output.output.i_bytes_per_frame = AOUT_SPDIF_SIZE;
197         p_aout->output.output.i_frame_length = A52_FRAME_NB;
198         aout_VolumeNoneInit( p_aout );
199     }
200     else
201     {
202         p_aout->output.i_nb_samples = FRAME_SIZE;
203         aout_VolumeSoftInit( p_aout );
204     }
205
206     /* Channels number */
207     var_Create( p_this, "audiofile-channels",
208                 VLC_VAR_INTEGER|VLC_VAR_DOINHERIT );
209     var_Get( p_this, "audiofile-channels", &val );
210     i_channels = val.i_int;
211
212     if( i_channels > 0 && i_channels <= CHANNELS_MAX )
213     {
214         p_aout->output.output.i_physical_channels =
215             pi_channels_maps[i_channels];
216     }
217
218     /* WAV header */
219     var_Create( p_this, "audiofile-wav", VLC_VAR_BOOL|VLC_VAR_DOINHERIT );
220     var_Get( p_this, "audiofile-wav", &val );
221     p_aout->output.p_sys->b_add_wav_header = val.b_bool;
222
223     if( p_aout->output.p_sys->b_add_wav_header )
224     {
225         /* Write wave header */
226         WAVEHEADER *wh = &p_aout->output.p_sys->waveh;
227
228         memset( wh, 0, sizeof(wh) );
229
230         switch( p_aout->output.output.i_format )
231         {
232         case VLC_FOURCC('f','l','3','2'):
233             wh->Format     = WAVE_FORMAT_IEEE_FLOAT;
234             wh->BitsPerSample = sizeof(float) * 8;
235             break;
236         case VLC_FOURCC('u','8',' ',' '):
237             wh->Format     = WAVE_FORMAT_PCM;
238             wh->BitsPerSample = 8;
239             break;
240         case VLC_FOURCC('s','1','6','l'):
241         default:
242             wh->Format     = WAVE_FORMAT_PCM;
243             wh->BitsPerSample = 16;
244             break;
245         }
246
247         wh->MainChunkID = VLC_FOURCC('R', 'I', 'F', 'F');
248         wh->Length = 0;                    /* temp, to be filled in as we go */
249         wh->ChunkTypeID = VLC_FOURCC('W', 'A', 'V', 'E');
250         wh->SubChunkID = VLC_FOURCC('f', 'm', 't', ' ');
251         wh->SubChunkLength = 16;
252
253         wh->Modus = aout_FormatNbChannels( &p_aout->output.output );
254         wh->SampleFreq = p_aout->output.output.i_rate;
255         wh->BytesPerSample = wh->Modus * ( wh->BitsPerSample / 8 );
256         wh->BytesPerSec = wh->BytesPerSample * wh->SampleFreq;
257
258         wh->DataChunkID = VLC_FOURCC('d', 'a', 't', 'a');
259         wh->DataLength = 0;                /* temp, to be filled in as we go */
260
261         /* Header -> little endian format */
262         SetWLE( &wh->Format, wh->Format );
263         SetWLE( &wh->BitsPerSample, wh->BitsPerSample );
264         SetDWLE( &wh->SubChunkLength, wh->SubChunkLength );
265         SetWLE( &wh->Modus, wh->Modus );
266         SetDWLE( &wh->SampleFreq, wh->SampleFreq );
267         SetWLE( &wh->BytesPerSample, wh->BytesPerSample );
268         SetDWLE( &wh->BytesPerSec, wh->BytesPerSec );
269
270         if( fwrite( wh, sizeof(WAVEHEADER), 1,
271                     p_aout->output.p_sys->p_file ) != 1 )
272         {
273             msg_Err( p_aout, "write error (%s)", strerror(errno) );
274         }
275     }
276
277     return 0;
278 }
279
280 /*****************************************************************************
281  * Close: close our file
282  *****************************************************************************/
283 static void Close( vlc_object_t * p_this )
284 {
285     aout_instance_t * p_aout = (aout_instance_t *)p_this;
286
287     msg_Dbg( p_aout, "closing audio file" );
288
289     if( p_aout->output.p_sys->b_add_wav_header )
290     {
291         /* Update Wave Header */
292         p_aout->output.p_sys->waveh.Length =
293             p_aout->output.p_sys->waveh.DataLength + sizeof(WAVEHEADER) - 4;
294
295         /* Write Wave Header */
296         if( fseek( p_aout->output.p_sys->p_file, 0, SEEK_SET ) )
297         {
298             msg_Err( p_aout, "seek error (%s)", strerror(errno) );
299         }
300
301         /* Header -> little endian format */
302         SetDWLE( &p_aout->output.p_sys->waveh.Length,
303                  p_aout->output.p_sys->waveh.Length );
304         SetDWLE( &p_aout->output.p_sys->waveh.DataLength,
305                  p_aout->output.p_sys->waveh.DataLength );
306
307         if( fwrite( &p_aout->output.p_sys->waveh, sizeof(WAVEHEADER), 1,
308                     p_aout->output.p_sys->p_file ) != 1 )
309         {
310             msg_Err( p_aout, "write error (%s)", strerror(errno) );
311         }
312     }
313
314     fclose( p_aout->output.p_sys->p_file );
315     free( p_aout->output.p_sys );
316 }
317
318 /*****************************************************************************
319  * Play: pretend to play a sound
320  *****************************************************************************/
321 static void Play( aout_instance_t * p_aout )
322 {
323     aout_buffer_t * p_buffer;
324
325     p_buffer = aout_FifoPop( p_aout, &p_aout->output.fifo );
326
327     if( fwrite( p_buffer->p_buffer, p_buffer->i_nb_bytes, 1,
328                 p_aout->output.p_sys->p_file ) != 1 )
329     {
330         msg_Err( p_aout, "write error (%s)", strerror(errno) );
331     }
332
333     if( p_aout->output.p_sys->b_add_wav_header )
334     {
335         /* Update Wave Header */
336         p_aout->output.p_sys->waveh.DataLength += p_buffer->i_nb_bytes;
337     }
338
339     aout_BufferFree( p_buffer );
340 }