1 /*****************************************************************************
2 * file.c : audio output which writes the samples to a file
3 *****************************************************************************
4 * Copyright (C) 2002 the VideoLAN team
7 * Authors: Christophe Massiot <massiot@via.ecp.fr>
8 * Gildas Bazin <gbazin@netcourrier.com>
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.
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.
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 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
33 #include <vlc_common.h>
34 #include <vlc_plugin.h>
36 #include <vlc_codecs.h> /* WAVEHEADER */
39 #define A52_FRAME_NB 1536
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 *****************************************************************************/
50 bool b_add_wav_header;
52 WAVEHEADER waveh; /* Wave header of the output file */
55 #define CHANNELS_MAX 6
56 static const int pi_channels_maps[CHANNELS_MAX+1] =
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
70 /*****************************************************************************
72 *****************************************************************************/
73 static int Open ( vlc_object_t * );
74 static void Close ( vlc_object_t * );
75 static void Play ( audio_output_t *, block_t * );
77 /*****************************************************************************
79 *****************************************************************************/
80 #define FORMAT_TEXT N_("Output format")
82 #define CHANNELS_TEXT N_("Number of output channels")
83 #define CHANNELS_LONGTEXT N_("By default (0), all the channels of the incoming " \
84 "will be saved but you can restrict the number of channels here.")
86 #define WAV_TEXT N_("Add WAVE header")
87 #define WAV_LONGTEXT N_("Instead of writing a raw file, you can add a WAV " \
88 "header to the file.")
90 static const char *const format_list[] = { "u8", "s8", "u16", "s16", "u16_le",
91 "s16_le", "u16_be", "s16_be", "fixed32",
93 static const int format_int[] = { VLC_CODEC_U8,
95 VLC_CODEC_U16N, VLC_CODEC_S16N,
104 #define FILE_TEXT N_("Output file")
105 #define FILE_LONGTEXT N_("File to which the audio samples will be written to. (\"-\" for stdout")
108 set_description( N_("File audio output") )
109 set_shortname( N_("File") )
110 set_category( CAT_AUDIO )
111 set_subcategory( SUBCAT_AUDIO_AOUT )
113 add_savefile( "audiofile-file", "audiofile.wav", FILE_TEXT,
114 FILE_LONGTEXT, false )
115 add_string( "audiofile-format", "s16",
116 FORMAT_TEXT, FORMAT_TEXT, true )
117 change_string_list( format_list, 0, 0 )
118 add_integer( "audiofile-channels", 0,
119 CHANNELS_TEXT, CHANNELS_LONGTEXT, true )
120 change_integer_range( 0, 6 )
121 add_bool( "audiofile-wav", true, WAV_TEXT, WAV_LONGTEXT, true )
123 set_capability( "audio output", 0 )
124 add_shortcut( "file", "audiofile" )
125 set_callbacks( Open, Close )
128 /*****************************************************************************
129 * Open: open a dummy audio device
130 *****************************************************************************/
131 static int Open( vlc_object_t * p_this )
133 audio_output_t * p_aout = (audio_output_t *)p_this;
134 char * psz_name, * psz_format;
135 const char * const * ppsz_compare = format_list;
136 int i_channels, i = 0;
138 psz_name = var_CreateGetString( p_this, "audiofile-file" );
139 if( !psz_name || !*psz_name )
141 msg_Err( p_aout, "you need to specify an output file name" );
146 /* Allocate structure */
147 p_aout->sys = malloc( sizeof( aout_sys_t ) );
148 if( p_aout->sys == NULL )
151 if( !strcmp( psz_name, "-" ) )
152 p_aout->sys->p_file = stdout;
154 p_aout->sys->p_file = vlc_fopen( psz_name, "wb" );
157 if ( p_aout->sys->p_file == NULL )
163 p_aout->pf_play = Play;
164 p_aout->pf_pause = NULL;
165 p_aout->pf_flush = NULL;
168 psz_format = var_CreateGetString( p_this, "audiofile-format" );
170 while ( *ppsz_compare != NULL )
172 if ( !strncmp( *ppsz_compare, psz_format, strlen(*ppsz_compare) ) )
179 if ( *ppsz_compare == NULL )
181 msg_Err( p_aout, "cannot understand the format string (%s)",
183 if( p_aout->sys->p_file != stdout )
184 fclose( p_aout->sys->p_file );
191 p_aout->format.i_format = format_int[i];
192 if ( AOUT_FMT_SPDIF( &p_aout->format ) )
194 p_aout->format.i_bytes_per_frame = AOUT_SPDIF_SIZE;
195 p_aout->format.i_frame_length = A52_FRAME_NB;
196 aout_VolumeNoneInit( p_aout );
199 aout_VolumeSoftInit( p_aout );
201 /* Channels number */
202 i_channels = var_CreateGetInteger( p_this, "audiofile-channels" );
204 if( i_channels > 0 && i_channels <= CHANNELS_MAX )
206 p_aout->format.i_physical_channels =
207 pi_channels_maps[i_channels];
211 p_aout->sys->b_add_wav_header = var_CreateGetBool( p_this,
214 if( p_aout->sys->b_add_wav_header )
216 /* Write wave header */
217 WAVEHEADER *wh = &p_aout->sys->waveh;
219 memset( wh, 0, sizeof(*wh) );
221 switch( p_aout->format.i_format )
224 wh->Format = WAVE_FORMAT_IEEE_FLOAT;
225 wh->BitsPerSample = sizeof(float) * 8;
228 wh->Format = WAVE_FORMAT_PCM;
229 wh->BitsPerSample = 8;
233 wh->Format = WAVE_FORMAT_PCM;
234 wh->BitsPerSample = 16;
238 wh->MainChunkID = VLC_FOURCC('R', 'I', 'F', 'F');
239 wh->Length = 0; /* temp, to be filled in as we go */
240 wh->ChunkTypeID = VLC_FOURCC('W', 'A', 'V', 'E');
241 wh->SubChunkID = VLC_FOURCC('f', 'm', 't', ' ');
242 wh->SubChunkLength = 16;
244 wh->Modus = aout_FormatNbChannels( &p_aout->format );
245 wh->SampleFreq = p_aout->format.i_rate;
246 wh->BytesPerSample = wh->Modus * ( wh->BitsPerSample / 8 );
247 wh->BytesPerSec = wh->BytesPerSample * wh->SampleFreq;
249 wh->DataChunkID = VLC_FOURCC('d', 'a', 't', 'a');
250 wh->DataLength = 0; /* temp, to be filled in as we go */
252 /* Header -> little endian format */
253 SetWLE( &wh->Format, wh->Format );
254 SetWLE( &wh->BitsPerSample, wh->BitsPerSample );
255 SetDWLE( &wh->SubChunkLength, wh->SubChunkLength );
256 SetWLE( &wh->Modus, wh->Modus );
257 SetDWLE( &wh->SampleFreq, wh->SampleFreq );
258 SetWLE( &wh->BytesPerSample, wh->BytesPerSample );
259 SetDWLE( &wh->BytesPerSec, wh->BytesPerSec );
261 if( fwrite( wh, sizeof(WAVEHEADER), 1,
262 p_aout->sys->p_file ) != 1 )
264 msg_Err( p_aout, "write error (%m)" );
271 /*****************************************************************************
272 * Close: close our file
273 *****************************************************************************/
274 static void Close( vlc_object_t * p_this )
276 audio_output_t * p_aout = (audio_output_t *)p_this;
278 msg_Dbg( p_aout, "closing audio file" );
280 if( p_aout->sys->b_add_wav_header )
282 /* Update Wave Header */
283 p_aout->sys->waveh.Length =
284 p_aout->sys->waveh.DataLength + sizeof(WAVEHEADER) - 4;
286 /* Write Wave Header */
287 if( fseek( p_aout->sys->p_file, 0, SEEK_SET ) )
289 msg_Err( p_aout, "seek error (%m)" );
292 /* Header -> little endian format */
293 SetDWLE( &p_aout->sys->waveh.Length,
294 p_aout->sys->waveh.Length );
295 SetDWLE( &p_aout->sys->waveh.DataLength,
296 p_aout->sys->waveh.DataLength );
298 if( fwrite( &p_aout->sys->waveh, sizeof(WAVEHEADER), 1,
299 p_aout->sys->p_file ) != 1 )
301 msg_Err( p_aout, "write error (%m)" );
305 if( p_aout->sys->p_file != stdout )
306 fclose( p_aout->sys->p_file );
310 /*****************************************************************************
311 * Play: pretend to play a sound
312 *****************************************************************************/
313 static void Play( audio_output_t * p_aout, block_t *p_buffer )
315 if( fwrite( p_buffer->p_buffer, p_buffer->i_buffer, 1,
316 p_aout->sys->p_file ) != 1 )
318 msg_Err( p_aout, "write error (%m)" );
321 if( p_aout->sys->b_add_wav_header )
323 /* Update Wave Header */
324 p_aout->sys->waveh.DataLength += p_buffer->i_buffer;
327 aout_BufferFree( p_buffer );