Add copyright notices to all relevant files. (based on svn log)
[profile/ivi/pulseaudio.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
80         u->memchunk.memblock = pa_memblock_new(i->sink->core->mempool, BUF_SIZE);
81         u->memchunk.index = 0;
82
83         if (u->readf_function) {
84             if ((n = u->readf_function(u->sndfile, u->memchunk.memblock->data, BUF_SIZE/fs)) <= 0)
85                 n = 0;
86
87             u->memchunk.length = n * fs;
88         } else {
89             if ((n = sf_read_raw(u->sndfile, u->memchunk.memblock->data, BUF_SIZE)) <= 0)
90                 n = 0;
91
92             u->memchunk.length = n;
93         }
94
95         if (!u->memchunk.length) {
96             free_userdata(u);
97             return -1;
98         }
99     }
100
101     *chunk = u->memchunk;
102     pa_memblock_ref(chunk->memblock);
103     assert(chunk->length);
104     return 0;
105 }
106
107 static void sink_input_drop(pa_sink_input *i, const pa_memchunk*chunk, size_t length) {
108     struct userdata *u;
109     assert(i && chunk && length && i->userdata);
110     u = i->userdata;
111
112     assert(!memcmp(chunk, &u->memchunk, sizeof(chunk)));
113     assert(length <= u->memchunk.length);
114
115     u->memchunk.index += length;
116     u->memchunk.length -= length;
117
118     if (u->memchunk.length <= 0) {
119         pa_memblock_unref(u->memchunk.memblock);
120         u->memchunk.memblock = NULL;
121         u->memchunk.index = u->memchunk.length = 0;
122     }
123 }
124
125 int pa_play_file(
126         pa_sink *sink,
127         const char *fname,
128         const pa_cvolume *volume) {
129
130     struct userdata *u = NULL;
131     SF_INFO sfinfo;
132     pa_sample_spec ss;
133     pa_sink_input_new_data data;
134
135     assert(sink);
136     assert(fname);
137
138     u = pa_xnew(struct userdata, 1);
139     u->sink_input = NULL;
140     u->memchunk.memblock = NULL;
141     u->memchunk.index = u->memchunk.length = 0;
142     u->sndfile = NULL;
143
144     memset(&sfinfo, 0, sizeof(sfinfo));
145
146     if (!(u->sndfile = sf_open(fname, SFM_READ, &sfinfo))) {
147         pa_log("Failed to open file %s", fname);
148         goto fail;
149     }
150
151     u->readf_function = NULL;
152
153     switch (sfinfo.format & 0xFF) {
154         case SF_FORMAT_PCM_16:
155         case SF_FORMAT_PCM_U8:
156         case SF_FORMAT_PCM_S8:
157             ss.format = PA_SAMPLE_S16NE;
158             u->readf_function = (sf_count_t (*)(SNDFILE *sndfile, void *ptr, sf_count_t frames)) sf_readf_short;
159             break;
160
161         case SF_FORMAT_ULAW:
162             ss.format = PA_SAMPLE_ULAW;
163             break;
164
165         case SF_FORMAT_ALAW:
166             ss.format = PA_SAMPLE_ALAW;
167             break;
168
169         case SF_FORMAT_FLOAT:
170         default:
171             ss.format = PA_SAMPLE_FLOAT32NE;
172             u->readf_function = (sf_count_t (*)(SNDFILE *sndfile, void *ptr, sf_count_t frames)) sf_readf_float;
173             break;
174     }
175
176     ss.rate = sfinfo.samplerate;
177     ss.channels = sfinfo.channels;
178
179     if (!pa_sample_spec_valid(&ss)) {
180         pa_log("Unsupported sample format in file %s", fname);
181         goto fail;
182     }
183
184     pa_sink_input_new_data_init(&data);
185     data.sink = sink;
186     data.driver = __FILE__;
187     data.name = fname;
188     pa_sink_input_new_data_set_sample_spec(&data, &ss);
189     pa_sink_input_new_data_set_volume(&data, volume);
190
191     if (!(u->sink_input = pa_sink_input_new(sink->core, &data, 0)))
192         goto fail;
193
194     u->sink_input->peek = sink_input_peek;
195     u->sink_input->drop = sink_input_drop;
196     u->sink_input->kill = sink_input_kill;
197     u->sink_input->userdata = u;
198
199     pa_sink_notify(u->sink_input->sink);
200
201     return 0;
202
203 fail:
204     if (u)
205         free_userdata(u);
206
207     return -1;
208 }