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