]> code.delx.au - pulseaudio/blob - polyp/sound-file-stream.c
* fix include file names in installed header files
[pulseaudio] / polyp / sound-file-stream.c
1 /* $Id$ */
2
3 /***
4 This file is part of polypaudio.
5
6 polypaudio 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 polypaudio 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 polypaudio; 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 "sound-file-stream.h"
34 #include "sink-input.h"
35 #include "xmalloc.h"
36 #include "log.h"
37
38 #define BUF_SIZE (1024*10)
39 #define PA_TYPEID_SOUND_FILE PA_TYPEID_MAKE('S', 'N', 'D', 'F')
40
41 struct userdata {
42 SNDFILE *sndfile;
43 struct pa_sink_input *sink_input;
44 struct pa_memchunk memchunk;
45 sf_count_t (*readf_function)(SNDFILE *sndfile, void *ptr, sf_count_t frames);
46 };
47
48 static void free_userdata(struct userdata *u) {
49 assert(u);
50 if (u->sink_input) {
51 pa_sink_input_disconnect(u->sink_input);
52 pa_sink_input_unref(u->sink_input);
53 }
54
55 if (u->memchunk.memblock)
56 pa_memblock_unref(u->memchunk.memblock);
57 if (u->sndfile)
58 sf_close(u->sndfile);
59
60 pa_xfree(u);
61 }
62
63 static void sink_input_kill(struct pa_sink_input *i) {
64 assert(i && i->userdata);
65 free_userdata(i->userdata);
66 }
67
68 static void si_kill(struct pa_mainloop_api *m, void *i) {
69 sink_input_kill(i);
70 }
71
72 static int sink_input_peek(struct pa_sink_input *i, struct pa_memchunk *chunk) {
73 struct userdata *u;
74 assert(i && chunk && i->userdata);
75 u = i->userdata;
76
77 if (!u->memchunk.memblock) {
78 uint32_t fs = pa_frame_size(&i->sample_spec);
79 sf_count_t samples = BUF_SIZE/fs;
80
81 u->memchunk.memblock = pa_memblock_new(BUF_SIZE, i->sink->core->memblock_stat);
82 u->memchunk.index = 0;
83 samples = u->readf_function(u->sndfile, u->memchunk.memblock->data, samples);
84 u->memchunk.length = samples*fs;
85
86 if (!u->memchunk.length) {
87 pa_memblock_unref(u->memchunk.memblock);
88 u->memchunk.memblock = NULL;
89 u->memchunk.index = u->memchunk.length = 0;
90 pa_mainloop_api_once(i->sink->core->mainloop, si_kill, i);
91 return -1;
92 }
93 }
94
95 *chunk = u->memchunk;
96 pa_memblock_ref(chunk->memblock);
97 assert(chunk->length);
98 return 0;
99 }
100
101 static void sink_input_drop(struct pa_sink_input *i, const struct pa_memchunk*chunk, size_t length) {
102 struct userdata *u;
103 assert(i && chunk && length && i->userdata);
104 u = i->userdata;
105
106 assert(!memcmp(chunk, &u->memchunk, sizeof(chunk)));
107 assert(length <= u->memchunk.length);
108
109 u->memchunk.index += length;
110 u->memchunk.length -= length;
111
112 if (u->memchunk.length <= 0) {
113 pa_memblock_unref(u->memchunk.memblock);
114 u->memchunk.memblock = NULL;
115 u->memchunk.index = u->memchunk.length = 0;
116 }
117 }
118
119 int pa_play_file(struct pa_sink *sink, const char *fname, pa_volume_t volume) {
120 struct userdata *u = NULL;
121 SF_INFO sfinfo;
122 struct pa_sample_spec ss;
123 assert(sink && fname);
124
125 if (volume <= 0)
126 goto fail;
127
128 u = pa_xmalloc(sizeof(struct userdata));
129 u->sink_input = NULL;
130 u->memchunk.memblock = NULL;
131 u->memchunk.index = u->memchunk.length = 0;
132 u->sndfile = NULL;
133
134 memset(&sfinfo, 0, sizeof(sfinfo));
135
136 if (!(u->sndfile = sf_open(fname, SFM_READ, &sfinfo))) {
137 pa_log(__FILE__": Failed to open file %s\n", fname);
138 goto fail;
139 }
140
141 switch (sfinfo.format & 0xFF) {
142 case SF_FORMAT_PCM_16:
143 case SF_FORMAT_PCM_U8:
144 case SF_FORMAT_ULAW:
145 case SF_FORMAT_ALAW:
146 ss.format = PA_SAMPLE_S16NE;
147 u->readf_function = (sf_count_t (*)(SNDFILE *sndfile, void *ptr, sf_count_t frames)) sf_readf_short;
148 break;
149 case SF_FORMAT_FLOAT:
150 default:
151 ss.format = PA_SAMPLE_FLOAT32NE;
152 u->readf_function = (sf_count_t (*)(SNDFILE *sndfile, void *ptr, sf_count_t frames)) sf_readf_float;
153 break;
154 }
155
156 ss.rate = sfinfo.samplerate;
157 ss.channels = sfinfo.channels;
158
159 if (!pa_sample_spec_valid(&ss)) {
160 pa_log(__FILE__": Unsupported sample format in file %s\n", fname);
161 goto fail;
162 }
163
164 if (!(u->sink_input = pa_sink_input_new(sink, PA_TYPEID_SOUND_FILE, fname, &ss, 0, -1)))
165 goto fail;
166
167 u->sink_input->volume = volume;
168 u->sink_input->peek = sink_input_peek;
169 u->sink_input->drop = sink_input_drop;
170 u->sink_input->kill = sink_input_kill;
171 u->sink_input->userdata = u;
172
173 pa_sink_notify(sink);
174
175 return 0;
176
177 fail:
178 if (u)
179 free_userdata(u);
180
181 return -1;
182 }