]> code.delx.au - pulseaudio/blob - polyp/sound-file-stream.c
implement proper logging
[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 };
45
46 static void free_userdata(struct userdata *u) {
47 assert(u);
48 if (u->sink_input)
49 pa_sink_input_free(u->sink_input);
50 if (u->memchunk.memblock)
51 pa_memblock_unref(u->memchunk.memblock);
52 if (u->sndfile)
53 sf_close(u->sndfile);
54
55 pa_xfree(u);
56 }
57
58 static void sink_input_kill(struct pa_sink_input *i) {
59 assert(i && i->userdata);
60 free_userdata(i->userdata);
61 }
62
63 static void si_kill(struct pa_mainloop_api *m, void *i) {
64 sink_input_kill(i);
65 }
66
67 static int sink_input_peek(struct pa_sink_input *i, struct pa_memchunk *chunk) {
68 struct userdata *u;
69 assert(i && chunk && i->userdata);
70 u = i->userdata;
71
72 if (!u->memchunk.memblock) {
73 uint32_t fs = pa_frame_size(&i->sample_spec);
74 sf_count_t samples = BUF_SIZE/fs;
75
76 u->memchunk.memblock = pa_memblock_new(BUF_SIZE, i->sink->core->memblock_stat);
77 u->memchunk.index = 0;
78 samples = sf_readf_float(u->sndfile, u->memchunk.memblock->data, samples);
79 u->memchunk.length = samples*fs;
80
81 if (!u->memchunk.length) {
82 pa_memblock_unref(u->memchunk.memblock);
83 u->memchunk.memblock = NULL;
84 u->memchunk.index = u->memchunk.length = 0;
85 pa_mainloop_api_once(i->sink->core->mainloop, si_kill, i);
86 return -1;
87 }
88 }
89
90 *chunk = u->memchunk;
91 pa_memblock_ref(chunk->memblock);
92 assert(chunk->length);
93 return 0;
94 }
95
96 static void sink_input_drop(struct pa_sink_input *i, const struct pa_memchunk*chunk, size_t length) {
97 struct userdata *u;
98 assert(i && chunk && length && i->userdata);
99 u = i->userdata;
100
101 assert(!memcmp(chunk, &u->memchunk, sizeof(chunk)));
102 assert(length <= u->memchunk.length);
103
104 u->memchunk.index += length;
105 u->memchunk.length -= length;
106
107 if (u->memchunk.length <= 0) {
108 pa_memblock_unref(u->memchunk.memblock);
109 u->memchunk.memblock = NULL;
110 u->memchunk.index = u->memchunk.length = 0;
111 }
112 }
113
114 int pa_play_file(struct pa_sink *sink, const char *fname, pa_volume_t volume) {
115 struct userdata *u = NULL;
116 SF_INFO sfinfo;
117 struct pa_sample_spec ss;
118 assert(sink && fname);
119
120 if (volume <= 0)
121 goto fail;
122
123 u = pa_xmalloc(sizeof(struct userdata));
124 u->sink_input = NULL;
125 u->memchunk.memblock = NULL;
126 u->memchunk.index = u->memchunk.length = 0;
127 u->sndfile = NULL;
128
129 memset(&sfinfo, 0, sizeof(sfinfo));
130
131 if (!(u->sndfile = sf_open(fname, SFM_READ, &sfinfo))) {
132 pa_log(__FILE__": Failed to open file %s\n", fname);
133 goto fail;
134 }
135
136 ss.format = PA_SAMPLE_FLOAT32;
137 ss.rate = sfinfo.samplerate;
138 ss.channels = sfinfo.channels;
139
140 if (!pa_sample_spec_valid(&ss)) {
141 pa_log(__FILE__": Unsupported sample format in file %s\n", fname);
142 goto fail;
143 }
144
145 if (!(u->sink_input = pa_sink_input_new(sink, fname, &ss)))
146 goto fail;
147
148 u->sink_input->volume = volume;
149 u->sink_input->peek = sink_input_peek;
150 u->sink_input->drop = sink_input_drop;
151 u->sink_input->kill = sink_input_kill;
152 u->sink_input->userdata = u;
153
154 pa_sink_notify(sink);
155
156 return 0;
157
158 fail:
159 if (u)
160 free_userdata(u);
161
162 return -1;
163 }