1 /*****************************************************************************
2 * file.c : audio output which writes the samples to a file
3 *****************************************************************************
4 * Copyright (C) 2002 VideoLAN
5 * $Id: file.c,v 1.26 2004/01/25 17:58:29 murray Exp $
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., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
23 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
35 #include "aout_internal.h"
38 #define FRAME_SIZE 2048
39 #define A52_FRAME_NB 1536
41 typedef struct WAVEHEADER
43 uint32_t MainChunkID; // it will be 'RIFF'
45 uint32_t ChunkTypeID; // it will be 'WAVE'
46 uint32_t SubChunkID; // it will be 'fmt '
47 uint32_t SubChunkLength;
52 uint16_t BytesPerSample;
53 uint16_t BitsPerSample;
54 uint32_t DataChunkID; // it will be 'data'
58 /*****************************************************************************
59 * aout_sys_t: audio output method descriptor
60 *****************************************************************************
61 * This structure is part of the audio output thread descriptor.
62 * It describes the direct sound specific properties of an audio device.
63 *****************************************************************************/
67 vlc_bool_t b_add_wav_header;
69 WAVEHEADER waveh; /* Wave header of the output file */
72 /*****************************************************************************
74 *****************************************************************************/
75 static int Open ( vlc_object_t * );
76 static void Close ( vlc_object_t * );
77 static void Play ( aout_instance_t * );
79 /*****************************************************************************
81 *****************************************************************************/
82 #define FORMAT_TEXT N_("Output Format")
83 #define FORMAT_LONGTEXT N_("One of \"u8\", \"s8\", \"u16\", \"s16\", " \
84 "\"u16_le\", \"s16_le\", \"u16_be\", " \
85 "\"s16_be\", \"fixed32\", \"float32\" or \"spdif\"")
86 #define WAV_TEXT N_("Add wave header")
87 #define WAV_LONGTEXT N_("Instead of writing a raw file, you can add a wav " \
90 static char *format_list[] = { "u8", "s8", "u16", "s16", "u16_le", "s16_le",
91 "u16_be", "s16_be", "fixed32", "float32",
93 static int format_int[] = { VLC_FOURCC('u','8',' ',' '),
94 VLC_FOURCC('s','8',' ',' '),
95 AOUT_FMT_U16_NE, AOUT_FMT_S16_NE,
96 VLC_FOURCC('u','1','6','l'),
97 VLC_FOURCC('s','1','6','l'),
98 VLC_FOURCC('u','1','6','b'),
99 VLC_FOURCC('s','1','6','b'),
100 VLC_FOURCC('f','i','3','2'),
101 VLC_FOURCC('f','l','3','2'),
102 VLC_FOURCC('s','p','i','f') };
104 #define FILE_TEXT N_("Output File")
105 #define FILE_LONGTEXT N_("File to which the audio samples will be written to")
108 add_category_hint( N_("Audio"), NULL, VLC_FALSE );
109 add_string( "audiofile-format", "s16", NULL,
110 FORMAT_TEXT, FORMAT_LONGTEXT, VLC_TRUE );
111 change_string_list( format_list, 0, 0 );
112 add_file( "audiofile", "audiofile.wav", NULL, FILE_TEXT,
113 FILE_LONGTEXT, VLC_FALSE );
114 add_bool( "audiofile-wav", 1, NULL, WAV_TEXT, WAV_LONGTEXT, VLC_TRUE );
115 set_description( N_("File audio output") );
116 set_capability( "audio output", 0 );
117 add_shortcut( "file" );
118 add_shortcut( "audiofile" );
119 set_callbacks( Open, Close );
122 /*****************************************************************************
123 * Open: open a dummy audio device
124 *****************************************************************************/
125 static int Open( vlc_object_t * p_this )
127 aout_instance_t * p_aout = (aout_instance_t *)p_this;
128 char * psz_name = config_GetPsz( p_this, "audiofile" );
129 char * psz_format = config_GetPsz( p_aout, "audiofile-format" );
130 char ** ppsz_compare = format_list;
133 /* Allocate structure */
134 p_aout->output.p_sys = malloc( sizeof( aout_sys_t ) );
135 if( p_aout->output.p_sys == NULL )
137 msg_Err( p_aout, "out of memory" );
141 p_aout->output.p_sys->p_file = fopen( psz_name, "wb" );
143 if ( p_aout->output.p_sys->p_file == NULL )
145 free( p_aout->output.p_sys );
149 p_aout->output.pf_play = Play;
151 while ( *ppsz_compare != NULL )
153 if ( !strncmp( *ppsz_compare, psz_format, strlen(*ppsz_compare) ) )
160 if ( *ppsz_compare == NULL )
162 msg_Err( p_aout, "cannot understand the format string (%s)",
164 fclose( p_aout->output.p_sys->p_file );
165 free( p_aout->output.p_sys );
169 p_aout->output.output.i_format = format_int[i];
170 if ( AOUT_FMT_NON_LINEAR( &p_aout->output.output ) )
172 p_aout->output.i_nb_samples = A52_FRAME_NB;
173 p_aout->output.output.i_bytes_per_frame = AOUT_SPDIF_SIZE;
174 p_aout->output.output.i_frame_length = A52_FRAME_NB;
175 aout_VolumeNoneInit( p_aout );
179 p_aout->output.i_nb_samples = FRAME_SIZE;
180 aout_VolumeSoftInit( p_aout );
183 p_aout->output.p_sys->b_add_wav_header =
184 config_GetInt( p_this, "audiofile-wav" );
186 if( p_aout->output.p_sys->b_add_wav_header )
188 /* Write wave header */
189 WAVEHEADER *wh = &p_aout->output.p_sys->waveh;
191 memset( wh, 0, sizeof(wh) );
193 switch( p_aout->output.output.i_format )
195 case VLC_FOURCC('f','l','3','2'):
196 wh->Format = WAVE_FORMAT_IEEE_FLOAT;
197 wh->BitsPerSample = sizeof(float) * 8;
199 case VLC_FOURCC('u','8',' ',' '):
200 wh->Format = WAVE_FORMAT_PCM;
201 wh->BitsPerSample = 8;
203 case VLC_FOURCC('s','1','6','l'):
205 wh->Format = WAVE_FORMAT_PCM;
206 wh->BitsPerSample = 16;
210 wh->MainChunkID = VLC_FOURCC('R', 'I', 'F', 'F');
211 wh->Length = 0; /* temp, to be filled in as we go */
212 wh->ChunkTypeID = VLC_FOURCC('W', 'A', 'V', 'E');
213 wh->SubChunkID = VLC_FOURCC('f', 'm', 't', ' ');
214 wh->SubChunkLength = 16;
216 wh->Modus = aout_FormatNbChannels( &p_aout->output.output );
217 wh->SampleFreq = p_aout->output.output.i_rate;
218 wh->BytesPerSample = wh->Modus * ( wh->BitsPerSample / 8 );
219 wh->BytesPerSec = wh->BytesPerSample * wh->SampleFreq;
221 wh->DataChunkID = VLC_FOURCC('d', 'a', 't', 'a');
222 wh->DataLength = 0; /* temp, to be filled in as we go */
224 if( fwrite( wh, sizeof(WAVEHEADER), 1,
225 p_aout->output.p_sys->p_file ) != 1 )
227 msg_Err( p_aout, "write error (%s)", strerror(errno) );
234 /*****************************************************************************
235 * Close: close our file
236 *****************************************************************************/
237 static void Close( vlc_object_t * p_this )
239 aout_instance_t * p_aout = (aout_instance_t *)p_this;
241 msg_Dbg( p_aout, "closing audio file" );
243 if( p_aout->output.p_sys->b_add_wav_header )
245 /* Update Wave Header */
246 p_aout->output.p_sys->waveh.Length =
247 p_aout->output.p_sys->waveh.DataLength + sizeof(WAVEHEADER) - 4;
249 /* Write Wave Header */
250 if( fseek( p_aout->output.p_sys->p_file, 0, SEEK_SET ) )
252 msg_Err( p_aout, "seek error (%s)", strerror(errno) );
254 if( fwrite( &p_aout->output.p_sys->waveh, sizeof(WAVEHEADER), 1,
255 p_aout->output.p_sys->p_file ) != 1 )
257 msg_Err( p_aout, "write error (%s)", strerror(errno) );
261 fclose( p_aout->output.p_sys->p_file );
262 free( p_aout->output.p_sys );
265 /*****************************************************************************
266 * Play: pretend to play a sound
267 *****************************************************************************/
268 static void Play( aout_instance_t * p_aout )
270 aout_buffer_t * p_buffer;
272 p_buffer = aout_FifoPop( p_aout, &p_aout->output.fifo );
274 if( fwrite( p_buffer->p_buffer, p_buffer->i_nb_bytes, 1,
275 p_aout->output.p_sys->p_file ) != 1 )
277 msg_Err( p_aout, "write error (%s)", strerror(errno) );
280 if( p_aout->output.p_sys->b_add_wav_header )
282 /* Update Wave Header */
283 p_aout->output.p_sys->waveh.DataLength += p_buffer->i_nb_bytes;
286 aout_BufferFree( p_buffer );