Revert r1404 and keep it on a development branch until it is fully tested.
[profile/ivi/pulseaudio-panda.git] / 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(i->sink->core->mempool, BUF_SIZE);
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(
124         pa_sink *sink,
125         const char *fname,
126         const pa_cvolume *volume) {
127     
128     struct userdata *u = NULL;
129     SF_INFO sfinfo;
130     pa_sample_spec ss;
131     pa_sink_input_new_data data;
132     
133     assert(sink);
134     assert(fname);
135
136     u = pa_xnew(struct userdata, 1);
137     u->sink_input = NULL;
138     u->memchunk.memblock = NULL;
139     u->memchunk.index = u->memchunk.length = 0;
140     u->sndfile = NULL;
141
142     memset(&sfinfo, 0, sizeof(sfinfo));
143
144     if (!(u->sndfile = sf_open(fname, SFM_READ, &sfinfo))) {
145         pa_log("Failed to open file %s", fname);
146         goto fail;
147     }
148
149     u->readf_function = NULL;
150     
151     switch (sfinfo.format & 0xFF) {
152         case SF_FORMAT_PCM_16:
153         case SF_FORMAT_PCM_U8:
154         case SF_FORMAT_PCM_S8:
155             ss.format = PA_SAMPLE_S16NE;
156             u->readf_function = (sf_count_t (*)(SNDFILE *sndfile, void *ptr, sf_count_t frames)) sf_readf_short;
157             break;
158
159         case SF_FORMAT_ULAW:
160             ss.format = PA_SAMPLE_ULAW;
161             break;
162             
163         case SF_FORMAT_ALAW:
164             ss.format = PA_SAMPLE_ALAW;
165             break;
166
167         case SF_FORMAT_FLOAT:
168         default:
169             ss.format = PA_SAMPLE_FLOAT32NE;
170             u->readf_function = (sf_count_t (*)(SNDFILE *sndfile, void *ptr, sf_count_t frames)) sf_readf_float;
171             break;
172     }
173             
174     ss.rate = sfinfo.samplerate;
175     ss.channels = sfinfo.channels;
176
177     if (!pa_sample_spec_valid(&ss)) {
178         pa_log("Unsupported sample format in file %s", fname);
179         goto fail;
180     }
181
182     pa_sink_input_new_data_init(&data);
183     data.sink = sink;
184     data.driver = __FILE__;
185     data.name = fname;
186     pa_sink_input_new_data_set_sample_spec(&data, &ss);
187     pa_sink_input_new_data_set_volume(&data, volume);
188     
189     if (!(u->sink_input = pa_sink_input_new(sink->core, &data, 0)))
190         goto fail;
191
192     u->sink_input->peek = sink_input_peek;
193     u->sink_input->drop = sink_input_drop;
194     u->sink_input->kill = sink_input_kill;
195     u->sink_input->userdata = u;
196     
197     pa_sink_notify(u->sink_input->sink);
198
199     return 0;
200
201 fail:
202     if (u)
203         free_userdata(u);
204     
205     return -1;
206 }