1 /*
2 *
3 * ao_raw.c
4 *
5 * Copyright (C) Stan Seibert - January 2001, July 2001
6 *
7 * This file is part of libao, a cross-platform audio output library. See
8 * README for a history of this source code.
9 *
10 * libao 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, or (at your option)
13 * any later version.
14 *
15 * libao 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 GNU Make; see the file COPYING. If not, write to
22 * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
23 *
24 ********************************************************************
25
26 last mod: $Id: ao_raw.c 18200 2012-02-14 00:44:01Z ph3-der-loewe $
27
28 ********************************************************************/
29
30 #include <stdio.h>
31 #include <string.h>
32 #include <errno.h>
33 #include <ao/ao.h>
34 #include <ao/plugin.h>
35
36 static char *ao_raw_options[] = {"byteorder","matrix","verbose","quiet","debug"};
37 static ao_info ao_raw_info =
38 {
39 AO_TYPE_FILE,
40 "RAW sample output",
41 "raw",
42 "Stan Seibert <indigo@aztec.asu.edu>",
43 "Writes raw audio samples to a file",
44 AO_FMT_NATIVE,
45 0,
46 ao_raw_options,
47 sizeof(ao_raw_options)/sizeof(*ao_raw_options)
48 };
49
50 typedef struct ao_raw_internal
51 {
52 int byte_order;
53 } ao_raw_internal;
54
55
ao_raw_test(void)56 static int ao_raw_test(void)
57 {
58 return 1; /* Always works */
59 }
60
61
ao_raw_driver_info(void)62 static ao_info *ao_raw_driver_info(void)
63 {
64 return &ao_raw_info;
65 }
66
67
ao_raw_device_init(ao_device * device)68 static int ao_raw_device_init(ao_device *device)
69 {
70 ao_raw_internal *internal;
71
72 internal = (ao_raw_internal *) malloc(sizeof(ao_raw_internal));
73
74 if (internal == NULL)
75 return 0; /* Could not initialize device memory */
76
77 internal->byte_order = AO_FMT_NATIVE;
78
79 device->internal = internal;
80 device->output_matrix_order = AO_OUTPUT_MATRIX_FIXED;
81
82 return 1; /* Memory alloc successful */
83 }
84
ao_raw_set_option(ao_device * device,const char * key,const char * value)85 static int ao_raw_set_option(ao_device *device, const char *key,
86 const char *value)
87 {
88 ao_raw_internal *internal = (ao_raw_internal *)device->internal;
89
90 if (!strcmp(key, "byteorder")) {
91 if (!strcmp(value, "native"))
92 internal->byte_order = AO_FMT_NATIVE;
93 else if (!strcmp(value, "big"))
94 internal->byte_order = AO_FMT_BIG;
95 else if (!strcmp(value, "little"))
96 internal->byte_order = AO_FMT_LITTLE;
97 else
98 return 0; /* Bad option value */
99 }
100
101 return 1;
102 }
103
104
ao_raw_open(ao_device * device,ao_sample_format * format)105 static int ao_raw_open(ao_device *device, ao_sample_format *format)
106 {
107 ao_raw_internal *internal = (ao_raw_internal *)device->internal;
108
109 device->driver_byte_format = internal->byte_order;
110
111 //if(!device->inter_matrix){
112 ///* by default, inter == in */
113 //if(format->matrix)
114 // device->inter_matrix = strdup(format->matrix);
115 //}
116
117 return 1;
118 }
119
120
121 /*
122 * play the sample to the already opened file descriptor
123 */
ao_raw_play(ao_device * device,const char * output_samples,uint_32 num_bytes)124 static int ao_raw_play(ao_device *device, const char *output_samples,
125 uint_32 num_bytes)
126 {
127 if (fwrite(output_samples, sizeof(char), num_bytes,
128 device->file) < num_bytes)
129 return 0;
130 else
131 return 1;
132 }
133
134
ao_raw_close(ao_device * device)135 static int ao_raw_close(ao_device *device)
136 {
137 /* No closeout needed */
138 return 1;
139 }
140
141
ao_raw_device_clear(ao_device * device)142 static void ao_raw_device_clear(ao_device *device)
143 {
144 ao_raw_internal *internal = (ao_raw_internal *) device->internal;
145
146 free(internal);
147 device->internal=NULL;
148 }
149
ao_raw_file_extension(void)150 const char *ao_raw_file_extension(void)
151 {
152 return "raw";
153 }
154
155
156 ao_functions ao_raw = {
157 ao_raw_test,
158 ao_raw_driver_info,
159 ao_raw_device_init,
160 ao_raw_set_option,
161 ao_raw_open,
162 ao_raw_play,
163 ao_raw_close,
164 ao_raw_device_clear,
165 ao_raw_file_extension
166 };
167