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