]> git.sesse.net Git - vlc/blob - modules/audio_output/file.c
aout: pass audio buffer explicitly to pf_play
[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
29 #ifdef HAVE_CONFIG_H
30 # include "config.h"
31 #endif
32
33 #include <vlc_common.h>
34 #include <vlc_plugin.h>
35 #include <vlc_aout.h>
36 #include <vlc_codecs.h> /* WAVEHEADER */
37 #include <vlc_fs.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     bool b_add_wav_header;
52
53     WAVEHEADER waveh;                      /* Wave header of the output file */
54 };
55
56 #define CHANNELS_MAX 6
57 static const 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        ( audio_output_t *, block_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 const char *const format_list[] = { "u8", "s8", "u16", "s16", "u16_le",
94                                      "s16_le", "u16_be", "s16_be", "fixed32",
95                                      "float32", "spdif" };
96 static const int format_int[] = { VLC_CODEC_U8,
97                                   VLC_CODEC_S8,
98                                   VLC_CODEC_U16N, VLC_CODEC_S16N,
99                                   VLC_CODEC_U16L,
100                                   VLC_CODEC_S16L,
101                                   VLC_CODEC_U16B,
102                                   VLC_CODEC_S16B,
103                                   VLC_CODEC_FI32,
104                                   VLC_CODEC_FL32,
105                                   VLC_CODEC_SPDIFL };
106
107 #define FILE_TEXT N_("Output file")
108 #define FILE_LONGTEXT N_("File to which the audio samples will be written to. (\"-\" for stdout")
109
110 vlc_module_begin ()
111     set_description( N_("File audio output") )
112     set_shortname( N_("File") )
113     set_category( CAT_AUDIO )
114     set_subcategory( SUBCAT_AUDIO_AOUT )
115
116     add_string( "audiofile-format", "s16",
117                 FORMAT_TEXT, FORMAT_LONGTEXT, true )
118         change_string_list( format_list, 0, 0 )
119     add_integer( "audiofile-channels", 0,
120                  CHANNELS_TEXT, CHANNELS_LONGTEXT, true )
121     add_savefile( "audiofile-file", "audiofile.wav", FILE_TEXT,
122                   FILE_LONGTEXT, false )
123     add_bool( "audiofile-wav", true, WAV_TEXT, WAV_LONGTEXT, true )
124
125     set_capability( "audio output", 0 )
126     add_shortcut( "file", "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     audio_output_t * p_aout = (audio_output_t *)p_this;
136     char * psz_name, * psz_format;
137     const char * const * ppsz_compare = format_list;
138     int i_channels, i = 0;
139
140     psz_name = var_CreateGetString( p_this, "audiofile-file" );
141     if( !psz_name || !*psz_name )
142     {
143         msg_Err( p_aout, "you need to specify an output file name" );
144         free( psz_name );
145         return VLC_EGENERIC;
146     }
147
148     /* Allocate structure */
149     p_aout->sys = malloc( sizeof( aout_sys_t ) );
150     if( p_aout->sys == NULL )
151         return VLC_ENOMEM;
152
153     if( !strcmp( psz_name, "-" ) )
154         p_aout->sys->p_file = stdout;
155     else
156         p_aout->sys->p_file = vlc_fopen( psz_name, "wb" );
157
158     free( psz_name );
159     if ( p_aout->sys->p_file == NULL )
160     {
161         free( p_aout->sys );
162         return VLC_EGENERIC;
163     }
164
165     p_aout->pf_play = Play;
166     p_aout->pf_pause = NULL;
167     p_aout->pf_flush = NULL;
168
169     /* Audio format */
170     psz_format = var_CreateGetString( p_this, "audiofile-format" );
171
172     while ( *ppsz_compare != NULL )
173     {
174         if ( !strncmp( *ppsz_compare, psz_format, strlen(*ppsz_compare) ) )
175         {
176             break;
177         }
178         ppsz_compare++; i++;
179     }
180
181     if ( *ppsz_compare == NULL )
182     {
183         msg_Err( p_aout, "cannot understand the format string (%s)",
184                  psz_format );
185         if( p_aout->sys->p_file != stdout )
186             fclose( p_aout->sys->p_file );
187         free( p_aout->sys );
188         free( psz_format );
189         return VLC_EGENERIC;
190     }
191     free( psz_format );
192
193     p_aout->format.i_format = format_int[i];
194     if ( AOUT_FMT_NON_LINEAR( &p_aout->format ) )
195     {
196         p_aout->i_nb_samples = A52_FRAME_NB;
197         p_aout->format.i_bytes_per_frame = AOUT_SPDIF_SIZE;
198         p_aout->format.i_frame_length = A52_FRAME_NB;
199         aout_VolumeNoneInit( p_aout );
200     }
201     else
202     {
203         p_aout->i_nb_samples = FRAME_SIZE;
204         aout_VolumeSoftInit( p_aout );
205     }
206
207     /* Channels number */
208     i_channels = var_CreateGetInteger( p_this, "audiofile-channels" );
209
210     if( i_channels > 0 && i_channels <= CHANNELS_MAX )
211     {
212         p_aout->format.i_physical_channels =
213             pi_channels_maps[i_channels];
214     }
215
216     /* WAV header */
217     p_aout->sys->b_add_wav_header = var_CreateGetBool( p_this,
218                                                         "audiofile-wav" );
219
220     if( p_aout->sys->b_add_wav_header )
221     {
222         /* Write wave header */
223         WAVEHEADER *wh = &p_aout->sys->waveh;
224
225         memset( wh, 0, sizeof(*wh) );
226
227         switch( p_aout->format.i_format )
228         {
229         case VLC_CODEC_FL32:
230             wh->Format     = WAVE_FORMAT_IEEE_FLOAT;
231             wh->BitsPerSample = sizeof(float) * 8;
232             break;
233         case VLC_CODEC_U8:
234             wh->Format     = WAVE_FORMAT_PCM;
235             wh->BitsPerSample = 8;
236             break;
237         case VLC_CODEC_S16L:
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->format );
251         wh->SampleFreq = p_aout->format.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->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     audio_output_t * p_aout = (audio_output_t *)p_this;
283
284     msg_Dbg( p_aout, "closing audio file" );
285
286     if( p_aout->sys->b_add_wav_header )
287     {
288         /* Update Wave Header */
289         p_aout->sys->waveh.Length =
290             p_aout->sys->waveh.DataLength + sizeof(WAVEHEADER) - 4;
291
292         /* Write Wave Header */
293         if( fseek( p_aout->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->sys->waveh.Length,
300                  p_aout->sys->waveh.Length );
301         SetDWLE( &p_aout->sys->waveh.DataLength,
302                  p_aout->sys->waveh.DataLength );
303
304         if( fwrite( &p_aout->sys->waveh, sizeof(WAVEHEADER), 1,
305                     p_aout->sys->p_file ) != 1 )
306         {
307             msg_Err( p_aout, "write error (%m)" );
308         }
309     }
310
311     if( p_aout->sys->p_file != stdout )
312         fclose( p_aout->sys->p_file );
313     free( p_aout->sys );
314 }
315
316 /*****************************************************************************
317  * Play: pretend to play a sound
318  *****************************************************************************/
319 static void Play( audio_output_t * p_aout, block_t *p_buffer )
320 {
321     if( fwrite( p_buffer->p_buffer, p_buffer->i_buffer, 1,
322                 p_aout->sys->p_file ) != 1 )
323     {
324         msg_Err( p_aout, "write error (%m)" );
325     }
326
327     if( p_aout->sys->b_add_wav_header )
328     {
329         /* Update Wave Header */
330         p_aout->sys->waveh.DataLength += p_buffer->i_buffer;
331     }
332
333     aout_BufferFree( p_buffer );
334 }