]> code.delx.au - pulseaudio/blob - src/pulsecore/sound-file.c
69b543ab175f0b706b5200b02672f15e3b250329
[pulseaudio] / src / pulsecore / sound-file.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 <string.h>
29 #include <assert.h>
30
31 #include <sndfile.h>
32
33 #include <pulse/sample.h>
34 #include <pulsecore/log.h>
35
36 #include "sound-file.h"
37 #include "core-scache.h"
38
39 int pa_sound_file_load(pa_mempool *pool, const char *fname, pa_sample_spec *ss, pa_channel_map *map, pa_memchunk *chunk) {
40 SNDFILE*sf = NULL;
41 SF_INFO sfinfo;
42 int ret = -1;
43 size_t l;
44 sf_count_t (*readf_function)(SNDFILE *sndfile, void *ptr, sf_count_t frames) = NULL;
45 assert(fname && ss && chunk);
46
47 chunk->memblock = NULL;
48 chunk->index = chunk->length = 0;
49
50 memset(&sfinfo, 0, sizeof(sfinfo));
51
52 if (!(sf = sf_open(fname, SFM_READ, &sfinfo))) {
53 pa_log("Failed to open file %s", fname);
54 goto finish;
55 }
56
57 switch (sfinfo.format & SF_FORMAT_SUBMASK) {
58 case SF_FORMAT_PCM_16:
59 case SF_FORMAT_PCM_U8:
60 case SF_FORMAT_PCM_S8:
61 ss->format = PA_SAMPLE_S16NE;
62 readf_function = (sf_count_t (*)(SNDFILE *sndfile, void *ptr, sf_count_t frames)) sf_readf_short;
63 break;
64
65 case SF_FORMAT_ULAW:
66 ss->format = PA_SAMPLE_ULAW;
67 break;
68
69 case SF_FORMAT_ALAW:
70 ss->format = PA_SAMPLE_ALAW;
71 break;
72
73 case SF_FORMAT_FLOAT:
74 case SF_FORMAT_DOUBLE:
75 default:
76 ss->format = PA_SAMPLE_FLOAT32NE;
77 readf_function = (sf_count_t (*)(SNDFILE *sndfile, void *ptr, sf_count_t frames)) sf_readf_float;
78 break;
79 }
80
81 ss->rate = sfinfo.samplerate;
82 ss->channels = sfinfo.channels;
83
84 if (!pa_sample_spec_valid(ss)) {
85 pa_log("Unsupported sample format in file %s", fname);
86 goto finish;
87 }
88
89 if (map)
90 pa_channel_map_init_auto(map, ss->channels, PA_CHANNEL_MAP_DEFAULT);
91
92 if ((l = pa_frame_size(ss)*sfinfo.frames) > PA_SCACHE_ENTRY_SIZE_MAX) {
93 pa_log("File too large");
94 goto finish;
95 }
96
97 chunk->memblock = pa_memblock_new(pool, l);
98 assert(chunk->memblock);
99 chunk->index = 0;
100 chunk->length = l;
101
102 if ((readf_function && readf_function(sf, chunk->memblock->data, sfinfo.frames) != sfinfo.frames) ||
103 (!readf_function && sf_read_raw(sf, chunk->memblock->data, l) != l)) {
104 pa_log("Premature file end");
105 goto finish;
106 }
107
108 ret = 0;
109
110 finish:
111
112 if (sf)
113 sf_close(sf);
114
115 if (ret != 0 && chunk->memblock)
116 pa_memblock_unref(chunk->memblock);
117
118 return ret;
119
120 }
121
122 int pa_sound_file_too_big_to_cache(const char *fname) {
123 SNDFILE*sf = NULL;
124 SF_INFO sfinfo;
125 pa_sample_spec ss;
126
127 if (!(sf = sf_open(fname, SFM_READ, &sfinfo))) {
128 pa_log("Failed to open file %s", fname);
129 return 0;
130 }
131
132 sf_close(sf);
133
134 switch (sfinfo.format & SF_FORMAT_SUBMASK) {
135 case SF_FORMAT_PCM_16:
136 case SF_FORMAT_PCM_U8:
137 case SF_FORMAT_PCM_S8:
138 ss.format = PA_SAMPLE_S16NE;
139 break;
140
141 case SF_FORMAT_ULAW:
142 ss.format = PA_SAMPLE_ULAW;
143 break;
144
145 case SF_FORMAT_ALAW:
146 ss.format = PA_SAMPLE_ALAW;
147 break;
148
149 case SF_FORMAT_DOUBLE:
150 case SF_FORMAT_FLOAT:
151 default:
152 ss.format = PA_SAMPLE_FLOAT32NE;
153 break;
154 }
155
156 ss.rate = sfinfo.samplerate;
157 ss.channels = sfinfo.channels;
158
159 if ((pa_frame_size(&ss) * sfinfo.frames) > PA_SCACHE_ENTRY_SIZE_MAX) {
160 pa_log("File too large %s", fname);
161 return 1;
162 }
163
164 return 0;
165 }