]> git.sesse.net Git - vlc/blob - modules/audio_output/file.c
* ALL: removed a bunch of unused add_category_hint().
[vlc] / modules / audio_output / file.c
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.27 2004/01/25 18:53:07 gbazin Exp $
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., 59 Temple Place - Suite 330, Boston, MA  02111, 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
35 #include "aout_internal.h"
36 #include "codecs.h"
37
38 #define FRAME_SIZE 2048
39 #define A52_FRAME_NB 1536
40
41 typedef struct WAVEHEADER
42 {
43     uint32_t MainChunkID;                      // it will be 'RIFF'
44     uint32_t Length;
45     uint32_t ChunkTypeID;                      // it will be 'WAVE'
46     uint32_t SubChunkID;                       // it will be 'fmt '
47     uint32_t SubChunkLength;
48     uint16_t Format;
49     uint16_t Modus;
50     uint32_t SampleFreq;
51     uint32_t BytesPerSec;
52     uint16_t BytesPerSample;
53     uint16_t BitsPerSample;
54     uint32_t DataChunkID;                      // it will be 'data'
55     uint32_t DataLength;
56 } WAVEHEADER;
57
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  *****************************************************************************/
64 struct aout_sys_t
65 {
66     FILE     * p_file;
67     vlc_bool_t b_add_wav_header;
68
69     WAVEHEADER waveh;                      /* Wave header of the output file */
70 };
71
72 /*****************************************************************************
73  * Local prototypes.
74  *****************************************************************************/
75 static int     Open        ( vlc_object_t * );
76 static void    Close       ( vlc_object_t * );
77 static void    Play        ( aout_instance_t * );
78
79 /*****************************************************************************
80  * Module descriptor
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 " \
88                         "header to the file")
89
90 static char *format_list[] = { "u8", "s8", "u16", "s16", "u16_le", "s16_le",
91                                "u16_be", "s16_be", "fixed32", "float32",
92                                "spdif" };
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') };
103
104 #define FILE_TEXT N_("Output File")
105 #define FILE_LONGTEXT N_("File to which the audio samples will be written to")
106
107 vlc_module_begin();
108     set_description( N_("File audio output") );
109
110     add_string( "audiofile-format", "s16", NULL,
111                 FORMAT_TEXT, FORMAT_LONGTEXT, VLC_TRUE );
112         change_string_list( format_list, 0, 0 );
113     add_file( "audiofile", "audiofile.wav", NULL, FILE_TEXT,
114               FILE_LONGTEXT, VLC_FALSE );
115     add_bool( "audiofile-wav", 1, NULL, WAV_TEXT, WAV_LONGTEXT, VLC_TRUE );
116
117     set_capability( "audio output", 0 );
118     add_shortcut( "file" );
119     add_shortcut( "audiofile" );
120     set_callbacks( Open, Close );
121 vlc_module_end();
122
123 /*****************************************************************************
124  * Open: open a dummy audio device
125  *****************************************************************************/
126 static int Open( vlc_object_t * p_this )
127 {
128     aout_instance_t * p_aout = (aout_instance_t *)p_this;
129     char * psz_name = config_GetPsz( p_this, "audiofile" );
130     char * psz_format = config_GetPsz( p_aout, "audiofile-format" );
131     char ** ppsz_compare = format_list;
132     int i = 0;
133
134    /* Allocate structure */
135     p_aout->output.p_sys = malloc( sizeof( aout_sys_t ) );
136     if( p_aout->output.p_sys == NULL )
137     {
138         msg_Err( p_aout, "out of memory" );
139         return VLC_EGENERIC;
140     }
141
142     p_aout->output.p_sys->p_file = fopen( psz_name, "wb" );
143     free( psz_name );
144     if ( p_aout->output.p_sys->p_file == NULL )
145     {
146         free( p_aout->output.p_sys );
147         return -1;
148     }
149
150     p_aout->output.pf_play = Play;
151
152     while ( *ppsz_compare != NULL )
153     {
154         if ( !strncmp( *ppsz_compare, psz_format, strlen(*ppsz_compare) ) )
155         {
156             break;
157         }
158         ppsz_compare++; i++;
159     }
160
161     if ( *ppsz_compare == NULL )
162     {
163         msg_Err( p_aout, "cannot understand the format string (%s)",
164                  psz_format );
165         fclose( p_aout->output.p_sys->p_file );
166         free( p_aout->output.p_sys );
167         return -1;
168     }
169
170     p_aout->output.output.i_format = format_int[i];
171     if ( AOUT_FMT_NON_LINEAR( &p_aout->output.output ) )
172     {
173         p_aout->output.i_nb_samples = A52_FRAME_NB;
174         p_aout->output.output.i_bytes_per_frame = AOUT_SPDIF_SIZE;
175         p_aout->output.output.i_frame_length = A52_FRAME_NB;
176         aout_VolumeNoneInit( p_aout );
177     }
178     else
179     {
180         p_aout->output.i_nb_samples = FRAME_SIZE;
181         aout_VolumeSoftInit( p_aout );
182     }
183
184     p_aout->output.p_sys->b_add_wav_header =
185         config_GetInt( p_this, "audiofile-wav" );
186
187     if( p_aout->output.p_sys->b_add_wav_header )
188     {
189         /* Write wave header */
190         WAVEHEADER *wh = &p_aout->output.p_sys->waveh;
191
192         memset( wh, 0, sizeof(wh) );
193
194         switch( p_aout->output.output.i_format )
195         {
196         case VLC_FOURCC('f','l','3','2'):
197             wh->Format     = WAVE_FORMAT_IEEE_FLOAT;
198             wh->BitsPerSample = sizeof(float) * 8;
199             break;
200         case VLC_FOURCC('u','8',' ',' '):
201             wh->Format     = WAVE_FORMAT_PCM;
202             wh->BitsPerSample = 8;
203             break;
204         case VLC_FOURCC('s','1','6','l'):
205         default:
206             wh->Format     = WAVE_FORMAT_PCM;
207             wh->BitsPerSample = 16;
208             break;
209         }
210
211         wh->MainChunkID = VLC_FOURCC('R', 'I', 'F', 'F');
212         wh->Length = 0;                    /* temp, to be filled in as we go */
213         wh->ChunkTypeID = VLC_FOURCC('W', 'A', 'V', 'E');
214         wh->SubChunkID = VLC_FOURCC('f', 'm', 't', ' ');
215         wh->SubChunkLength = 16;
216
217         wh->Modus = aout_FormatNbChannels( &p_aout->output.output );
218         wh->SampleFreq = p_aout->output.output.i_rate;
219         wh->BytesPerSample = wh->Modus * ( wh->BitsPerSample / 8 );
220         wh->BytesPerSec = wh->BytesPerSample * wh->SampleFreq;
221
222         wh->DataChunkID = VLC_FOURCC('d', 'a', 't', 'a');
223         wh->DataLength = 0;                /* temp, to be filled in as we go */
224
225         if( fwrite( wh, sizeof(WAVEHEADER), 1,
226                     p_aout->output.p_sys->p_file ) != 1 )
227         {
228             msg_Err( p_aout, "write error (%s)", strerror(errno) );
229         }
230     }
231
232     return 0;
233 }
234
235 /*****************************************************************************
236  * Close: close our file
237  *****************************************************************************/
238 static void Close( vlc_object_t * p_this )
239 {
240     aout_instance_t * p_aout = (aout_instance_t *)p_this;
241
242     msg_Dbg( p_aout, "closing audio file" );
243
244     if( p_aout->output.p_sys->b_add_wav_header )
245     {
246         /* Update Wave Header */
247         p_aout->output.p_sys->waveh.Length =
248             p_aout->output.p_sys->waveh.DataLength + sizeof(WAVEHEADER) - 4;
249
250         /* Write Wave Header */
251         if( fseek( p_aout->output.p_sys->p_file, 0, SEEK_SET ) )
252         {
253             msg_Err( p_aout, "seek error (%s)", strerror(errno) );
254         }
255         if( fwrite( &p_aout->output.p_sys->waveh, sizeof(WAVEHEADER), 1,
256                     p_aout->output.p_sys->p_file ) != 1 )
257         {
258             msg_Err( p_aout, "write error (%s)", strerror(errno) );
259         }
260     }
261
262     fclose( p_aout->output.p_sys->p_file );
263     free( p_aout->output.p_sys );
264 }
265
266 /*****************************************************************************
267  * Play: pretend to play a sound
268  *****************************************************************************/
269 static void Play( aout_instance_t * p_aout )
270 {
271     aout_buffer_t * p_buffer;
272
273     p_buffer = aout_FifoPop( p_aout, &p_aout->output.fifo );
274
275     if( fwrite( p_buffer->p_buffer, p_buffer->i_nb_bytes, 1,
276                 p_aout->output.p_sys->p_file ) != 1 )
277     {
278         msg_Err( p_aout, "write error (%s)", strerror(errno) );
279     }
280
281     if( p_aout->output.p_sys->b_add_wav_header )
282     {
283         /* Update Wave Header */
284         p_aout->output.p_sys->waveh.DataLength += p_buffer->i_nb_bytes;
285     }
286
287     aout_BufferFree( p_buffer );
288 }