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