]> git.sesse.net Git - vlc/blob - modules/audio_output/file.c
* modules/demux/mpeg: Added DVB stream type for A/52 streams (0x6),
[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.13 2002/10/20 12:23:47 massiot Exp $
6  *
7  * Authors: Christophe Massiot <massiot@via.ecp.fr>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  * 
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <string.h>
28 #include <stdlib.h>
29 #include <errno.h>
30
31 #include <vlc/vlc.h>
32 #include <vlc/aout.h>
33
34 #include "aout_internal.h"
35
36 #define FRAME_SIZE 2048
37 #define A52_FRAME_NB 1536
38
39 /*****************************************************************************
40  * Local prototypes.
41  *****************************************************************************/
42 static int     Open        ( vlc_object_t * );
43 static void    Close       ( vlc_object_t * );
44 static void    Play        ( aout_instance_t * );
45
46 /*****************************************************************************
47  * Module descriptor
48  *****************************************************************************/
49 #define FORMAT_TEXT N_("output format")
50 #define FORMAT_LONGTEXT N_("one of \"u8\", \"s8\", \"u16\", \"s16\"," \
51                            " \"u16_le\", \"s16_le\", \"u16_be\"," \
52                            " \"s16_be\", \"fixed32\", \"float32\" or \"spdif\"")
53
54 static char *format_list[] = { "u8", "s8", "u16", "s16", "u16_le", "s16_le",
55                                "u16_be", "s16_be", "fixed32", "float32",
56                                "spdif", NULL };
57 static int format_int[] = { VLC_FOURCC('u','8',' ',' '),
58                             VLC_FOURCC('s','8',' ',' '),
59                             AOUT_FMT_U16_NE, AOUT_FMT_S16_NE,
60                             VLC_FOURCC('u','1','6','l'),
61                             VLC_FOURCC('s','1','6','l'),
62                             VLC_FOURCC('u','1','6','b'),
63                             VLC_FOURCC('s','1','6','b'),
64                             VLC_FOURCC('f','i','3','2'),
65                             VLC_FOURCC('f','l','3','2'),
66                             VLC_FOURCC('s','p','i','f') };
67
68 #define PATH_TEXT N_("path of the output file")
69 #define PATH_LONGTEXT N_("By default samples.raw")
70
71 vlc_module_begin();
72     add_category_hint( N_("Audio"), NULL );
73     add_string_from_list( "format", "s16", format_list, NULL,
74                           FORMAT_TEXT, FORMAT_LONGTEXT );
75     add_string( "path", "samples.raw", NULL, PATH_TEXT, PATH_LONGTEXT );
76     set_description( _("file output module") );
77     set_capability( "audio output", 0 );
78     add_shortcut( "file" );
79     set_callbacks( Open, Close );
80 vlc_module_end();
81
82 /*****************************************************************************
83  * Open: open a dummy audio device
84  *****************************************************************************/
85 static int Open( vlc_object_t * p_this )
86 {
87     aout_instance_t * p_aout = (aout_instance_t *)p_this;
88     FILE * p_file;
89     char * psz_name = config_GetPsz( p_this, "path" );
90     char * psz_format = config_GetPsz( p_aout, "format" );
91     char ** ppsz_compare = format_list;
92     int i = 0;
93
94
95     p_file = fopen( psz_name, "wb" );
96     p_aout->output.p_sys = (void *)p_file;
97     free( psz_name );
98     if ( p_file == NULL ) return -1;
99
100     p_aout->output.pf_play = Play;
101
102     while ( *ppsz_compare != NULL )
103     {
104         if ( !strncmp( *ppsz_compare, psz_format, strlen(*ppsz_compare) ) )
105         {
106             break;
107         }
108         ppsz_compare++; i++;
109     }
110
111     if ( *ppsz_compare == NULL )
112     {
113         msg_Err( p_aout, "Cannot understand the format string (%s)",
114                  psz_format );
115         return -1;
116     }
117
118     p_aout->output.output.i_format = format_int[i];
119     if ( AOUT_FMT_NON_LINEAR( &p_aout->output.output ) )
120     {
121         p_aout->output.i_nb_samples = A52_FRAME_NB;
122         p_aout->output.output.i_bytes_per_frame = AOUT_SPDIF_SIZE;
123         p_aout->output.output.i_frame_length = A52_FRAME_NB;
124         aout_VolumeNoneInit( p_aout );
125     }
126     else
127     {
128         p_aout->output.i_nb_samples = FRAME_SIZE;
129         aout_VolumeSoftInit( p_aout );
130     }
131     return 0;
132 }
133
134 /*****************************************************************************
135  * Close: close our file
136  *****************************************************************************/
137 static void Close( vlc_object_t * p_this )
138 {
139     aout_instance_t * p_aout = (aout_instance_t *)p_this;
140
141     fclose( (FILE *)p_aout->output.p_sys );
142 }
143
144 /*****************************************************************************
145  * Play: pretend to play a sound
146  *****************************************************************************/
147 static void Play( aout_instance_t * p_aout )
148 {
149     aout_buffer_t * p_buffer;
150
151     p_buffer = aout_FifoPop( p_aout, &p_aout->output.fifo );
152
153     if( fwrite( p_buffer->p_buffer, p_buffer->i_nb_bytes, 1,
154                 (FILE *)p_aout->output.p_sys ) != 1 )
155     {
156         msg_Err( p_aout, "write error (%s)", strerror(errno) );
157     }
158
159     aout_BufferFree( p_buffer );
160 }
161