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