]> code.delx.au - pulseaudio/blob - src/pulsecore/sound-file-stream.c
big s/polyp/pulse/g
[pulseaudio] / src / pulsecore / sound-file-stream.c
1 /* $Id$ */
2
3 /***
4 This file is part of PulseAudio.
5
6 PulseAudio is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as published
8 by the Free Software Foundation; either version 2 of the License,
9 or (at your option) any later version.
10
11 PulseAudio is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with PulseAudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19 USA.
20 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <stdlib.h>
27 #include <assert.h>
28 #include <stdio.h>
29 #include <string.h>
30
31 #include <sndfile.h>
32
33 #include <pulse/xmalloc.h>
34
35 #include <pulsecore/sink-input.h>
36 #include <pulsecore/log.h>
37
38 #include "sound-file-stream.h"
39
40 #define BUF_SIZE (1024*10)
41
42 struct userdata {
43 SNDFILE *sndfile;
44 pa_sink_input *sink_input;
45 pa_memchunk memchunk;
46 sf_count_t (*readf_function)(SNDFILE *sndfile, void *ptr, sf_count_t frames);
47 };
48
49 static void free_userdata(struct userdata *u) {
50 assert(u);
51 if (u->sink_input) {
52 pa_sink_input_disconnect(u->sink_input);
53 pa_sink_input_unref(u->sink_input);
54 }
55
56 if (u->memchunk.memblock)
57 pa_memblock_unref(u->memchunk.memblock);
58 if (u->sndfile)
59 sf_close(u->sndfile);
60
61 pa_xfree(u);
62 }
63
64 static void sink_input_kill(pa_sink_input *i) {
65 assert(i && i->userdata);
66 free_userdata(i->userdata);
67 }
68
69 static int sink_input_peek(pa_sink_input *i, pa_memchunk *chunk) {
70 struct userdata *u;
71 assert(i && chunk && i->userdata);
72 u = i->userdata;
73
74 if (!u->memchunk.memblock) {
75 uint32_t fs = pa_frame_size(&i->sample_spec);
76 sf_count_t n;
77
78 u->memchunk.memblock = pa_memblock_new(BUF_SIZE, i->sink->core->memblock_stat);
79 u->memchunk.index = 0;
80
81 if (u->readf_function) {
82 if ((n = u->readf_function(u->sndfile, u->memchunk.memblock->data, BUF_SIZE/fs)) <= 0)
83 n = 0;
84
85 u->memchunk.length = n * fs;
86 } else {
87 if ((n = sf_read_raw(u->sndfile, u->memchunk.memblock->data, BUF_SIZE)) <= 0)
88 n = 0;
89
90 u->memchunk.length = n;
91 }
92
93 if (!u->memchunk.length) {
94 free_userdata(u);
95 return -1;
96 }
97 }
98
99 *chunk = u->memchunk;
100 pa_memblock_ref(chunk->memblock);
101 assert(chunk->length);
102 return 0;
103 }
104
105 static void sink_input_drop(pa_sink_input *i, const pa_memchunk*chunk, size_t length) {
106 struct userdata *u;
107 assert(i && chunk && length && i->userdata);
108 u = i->userdata;
109
110 assert(!memcmp(chunk, &u->memchunk, sizeof(chunk)));
111 assert(length <= u->memchunk.length);
112
113 u->memchunk.index += length;
114 u->memchunk.length -= length;
115
116 if (u->memchunk.length <= 0) {
117 pa_memblock_unref(u->memchunk.memblock);
118 u->memchunk.memblock = NULL;
119 u->memchunk.index = u->memchunk.length = 0;
120 }
121 }
122
123 int pa_play_file(pa_sink *sink, const char *fname, const pa_cvolume *volume) {
124 struct userdata *u = NULL;
125 SF_INFO sfinfo;
126 pa_sample_spec ss;
127 assert(sink && fname);
128
129 u = pa_xmalloc(sizeof(struct userdata));
130 u->sink_input = NULL;
131 u->memchunk.memblock = NULL;
132 u->memchunk.index = u->memchunk.length = 0;
133 u->sndfile = NULL;
134
135 memset(&sfinfo, 0, sizeof(sfinfo));
136
137 if (!(u->sndfile = sf_open(fname, SFM_READ, &sfinfo))) {
138 pa_log(__FILE__": Failed to open file %s", fname);
139 goto fail;
140 }
141
142 u->readf_function = NULL;
143
144 switch (sfinfo.format & 0xFF) {
145 case SF_FORMAT_PCM_16:
146 case SF_FORMAT_PCM_U8:
147 case SF_FORMAT_PCM_S8:
148 ss.format = PA_SAMPLE_S16NE;
149 u->readf_function = (sf_count_t (*)(SNDFILE *sndfile, void *ptr, sf_count_t frames)) sf_readf_short;
150 break;
151
152 case SF_FORMAT_ULAW:
153 ss.format = PA_SAMPLE_ULAW;
154 break;
155
156 case SF_FORMAT_ALAW:
157 ss.format = PA_SAMPLE_ALAW;
158 break;
159
160 case SF_FORMAT_FLOAT:
161 default:
162 ss.format = PA_SAMPLE_FLOAT32NE;
163 u->readf_function = (sf_count_t (*)(SNDFILE *sndfile, void *ptr, sf_count_t frames)) sf_readf_float;
164 break;
165 }
166
167 ss.rate = sfinfo.samplerate;
168 ss.channels = sfinfo.channels;
169
170 if (!pa_sample_spec_valid(&ss)) {
171 pa_log(__FILE__": Unsupported sample format in file %s", fname);
172 goto fail;
173 }
174
175 if (!(u->sink_input = pa_sink_input_new(sink, __FILE__, fname, &ss, NULL, volume, 0, -1)))
176 goto fail;
177
178 u->sink_input->peek = sink_input_peek;
179 u->sink_input->drop = sink_input_drop;
180 u->sink_input->kill = sink_input_kill;
181 u->sink_input->userdata = u;
182
183 pa_sink_notify(sink);
184
185 return 0;
186
187 fail:
188 if (u)
189 free_userdata(u);
190
191 return -1;
192 }