2004-08-17 19:47:42 +00:00
|
|
|
/* $Id$ */
|
|
|
|
|
|
|
|
|
|
/***
|
2006-06-19 21:53:48 +00:00
|
|
|
This file is part of PulseAudio.
|
2004-08-17 19:47:42 +00:00
|
|
|
|
2006-06-19 21:53:48 +00:00
|
|
|
PulseAudio is free software; you can redistribute it and/or modify
|
2004-11-14 14:58:54 +00:00
|
|
|
it under the terms of the GNU Lesser General Public License as published
|
2004-08-17 19:47:42 +00:00
|
|
|
by the Free Software Foundation; either version 2 of the License,
|
|
|
|
|
or (at your option) any later version.
|
|
|
|
|
|
2006-06-19 21:53:48 +00:00
|
|
|
PulseAudio is distributed in the hope that it will be useful, but
|
2004-08-17 19:47:42 +00:00
|
|
|
WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
|
General Public License for more details.
|
|
|
|
|
|
2004-11-14 14:58:54 +00:00
|
|
|
You should have received a copy of the GNU Lesser General Public License
|
2006-06-19 21:53:48 +00:00
|
|
|
along with PulseAudio; if not, write to the Free Software
|
2004-08-17 19:47:42 +00:00
|
|
|
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
|
|
|
|
|
USA.
|
|
|
|
|
***/
|
|
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
|
#include <config.h>
|
|
|
|
|
#endif
|
|
|
|
|
|
2004-08-04 16:42:37 +00:00
|
|
|
#include <string.h>
|
|
|
|
|
#include <assert.h>
|
|
|
|
|
|
|
|
|
|
#include <sndfile.h>
|
|
|
|
|
|
2006-06-19 21:53:48 +00:00
|
|
|
#include <pulse/sample.h>
|
|
|
|
|
#include <pulsecore/log.h>
|
2006-02-17 12:10:58 +00:00
|
|
|
|
|
|
|
|
#include "sound-file.h"
|
2006-04-23 20:59:09 +00:00
|
|
|
#include "core-scache.h"
|
2004-08-04 16:42:37 +00:00
|
|
|
|
2006-08-18 19:55:18 +00:00
|
|
|
int pa_sound_file_load(pa_mempool *pool, const char *fname, pa_sample_spec *ss, pa_channel_map *map, pa_memchunk *chunk) {
|
2004-08-04 16:42:37 +00:00
|
|
|
SNDFILE*sf = NULL;
|
|
|
|
|
SF_INFO sfinfo;
|
|
|
|
|
int ret = -1;
|
|
|
|
|
size_t l;
|
2006-05-17 14:55:17 +00:00
|
|
|
sf_count_t (*readf_function)(SNDFILE *sndfile, void *ptr, sf_count_t frames) = NULL;
|
2004-08-04 16:42:37 +00:00
|
|
|
assert(fname && ss && chunk);
|
|
|
|
|
|
|
|
|
|
chunk->memblock = NULL;
|
|
|
|
|
chunk->index = chunk->length = 0;
|
2004-09-27 21:05:55 +00:00
|
|
|
|
|
|
|
|
memset(&sfinfo, 0, sizeof(sfinfo));
|
|
|
|
|
|
2004-08-04 16:42:37 +00:00
|
|
|
if (!(sf = sf_open(fname, SFM_READ, &sfinfo))) {
|
2006-02-23 02:27:19 +00:00
|
|
|
pa_log(__FILE__": Failed to open file %s", fname);
|
2004-08-04 16:42:37 +00:00
|
|
|
goto finish;
|
|
|
|
|
}
|
|
|
|
|
|
2005-09-16 00:03:19 +00:00
|
|
|
switch (sfinfo.format & SF_FORMAT_SUBMASK) {
|
2006-05-17 14:55:17 +00:00
|
|
|
case SF_FORMAT_PCM_16:
|
|
|
|
|
case SF_FORMAT_PCM_U8:
|
|
|
|
|
case SF_FORMAT_PCM_S8:
|
|
|
|
|
ss->format = PA_SAMPLE_S16NE;
|
|
|
|
|
readf_function = (sf_count_t (*)(SNDFILE *sndfile, void *ptr, sf_count_t frames)) sf_readf_short;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case SF_FORMAT_ULAW:
|
|
|
|
|
ss->format = PA_SAMPLE_ULAW;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case SF_FORMAT_ALAW:
|
|
|
|
|
ss->format = PA_SAMPLE_ALAW;
|
|
|
|
|
break;
|
|
|
|
|
|
2004-09-27 21:05:55 +00:00
|
|
|
case SF_FORMAT_FLOAT:
|
2005-09-16 00:03:19 +00:00
|
|
|
case SF_FORMAT_DOUBLE:
|
2006-05-17 14:55:17 +00:00
|
|
|
default:
|
2004-09-27 21:05:55 +00:00
|
|
|
ss->format = PA_SAMPLE_FLOAT32NE;
|
|
|
|
|
readf_function = (sf_count_t (*)(SNDFILE *sndfile, void *ptr, sf_count_t frames)) sf_readf_float;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2004-08-04 16:42:37 +00:00
|
|
|
ss->rate = sfinfo.samplerate;
|
|
|
|
|
ss->channels = sfinfo.channels;
|
|
|
|
|
|
|
|
|
|
if (!pa_sample_spec_valid(ss)) {
|
2006-02-23 02:27:19 +00:00
|
|
|
pa_log(__FILE__": Unsupported sample format in file %s", fname);
|
2004-08-04 16:42:37 +00:00
|
|
|
goto finish;
|
|
|
|
|
}
|
2006-04-22 21:50:15 +00:00
|
|
|
|
|
|
|
|
if (map)
|
2006-05-16 23:47:38 +00:00
|
|
|
pa_channel_map_init_auto(map, ss->channels, PA_CHANNEL_MAP_DEFAULT);
|
2004-08-04 16:42:37 +00:00
|
|
|
|
2006-04-23 20:59:09 +00:00
|
|
|
if ((l = pa_frame_size(ss)*sfinfo.frames) > PA_SCACHE_ENTRY_SIZE_MAX) {
|
2006-02-23 02:27:19 +00:00
|
|
|
pa_log(__FILE__": File too large");
|
2004-08-04 16:42:37 +00:00
|
|
|
goto finish;
|
|
|
|
|
}
|
|
|
|
|
|
2006-08-18 19:55:18 +00:00
|
|
|
chunk->memblock = pa_memblock_new(pool, l);
|
2004-08-04 16:42:37 +00:00
|
|
|
assert(chunk->memblock);
|
|
|
|
|
chunk->index = 0;
|
|
|
|
|
chunk->length = l;
|
|
|
|
|
|
2006-05-17 14:55:17 +00:00
|
|
|
if ((readf_function && readf_function(sf, chunk->memblock->data, sfinfo.frames) != sfinfo.frames) ||
|
|
|
|
|
(!readf_function && sf_read_raw(sf, chunk->memblock->data, l) != l)) {
|
2006-02-23 02:27:19 +00:00
|
|
|
pa_log(__FILE__": Premature file end");
|
2004-08-04 16:42:37 +00:00
|
|
|
goto finish;
|
|
|
|
|
}
|
2006-05-17 14:55:17 +00:00
|
|
|
|
2004-08-04 16:42:37 +00:00
|
|
|
ret = 0;
|
|
|
|
|
|
|
|
|
|
finish:
|
|
|
|
|
|
|
|
|
|
if (sf)
|
|
|
|
|
sf_close(sf);
|
|
|
|
|
|
|
|
|
|
if (ret != 0 && chunk->memblock)
|
|
|
|
|
pa_memblock_unref(chunk->memblock);
|
|
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
|
|
}
|
2005-09-16 00:04:29 +00:00
|
|
|
|
|
|
|
|
int pa_sound_file_too_big_to_cache(const char *fname) {
|
|
|
|
|
SNDFILE*sf = NULL;
|
|
|
|
|
SF_INFO sfinfo;
|
2006-01-11 01:17:39 +00:00
|
|
|
pa_sample_spec ss;
|
2005-09-16 00:04:29 +00:00
|
|
|
|
|
|
|
|
if (!(sf = sf_open(fname, SFM_READ, &sfinfo))) {
|
2006-02-23 02:27:19 +00:00
|
|
|
pa_log(__FILE__": Failed to open file %s", fname);
|
2005-09-16 00:04:29 +00:00
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sf_close(sf);
|
|
|
|
|
|
|
|
|
|
switch (sfinfo.format & SF_FORMAT_SUBMASK) {
|
2006-05-17 14:55:17 +00:00
|
|
|
case SF_FORMAT_PCM_16:
|
|
|
|
|
case SF_FORMAT_PCM_U8:
|
|
|
|
|
case SF_FORMAT_PCM_S8:
|
|
|
|
|
ss.format = PA_SAMPLE_S16NE;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case SF_FORMAT_ULAW:
|
|
|
|
|
ss.format = PA_SAMPLE_ULAW;
|
2005-09-16 00:04:29 +00:00
|
|
|
break;
|
2006-05-17 14:55:17 +00:00
|
|
|
|
|
|
|
|
case SF_FORMAT_ALAW:
|
|
|
|
|
ss.format = PA_SAMPLE_ALAW;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case SF_FORMAT_DOUBLE:
|
|
|
|
|
case SF_FORMAT_FLOAT:
|
2005-09-16 00:04:29 +00:00
|
|
|
default:
|
2006-05-17 14:55:17 +00:00
|
|
|
ss.format = PA_SAMPLE_FLOAT32NE;
|
2005-09-16 00:04:29 +00:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ss.rate = sfinfo.samplerate;
|
|
|
|
|
ss.channels = sfinfo.channels;
|
|
|
|
|
|
2006-04-23 20:59:09 +00:00
|
|
|
if ((pa_frame_size(&ss) * sfinfo.frames) > PA_SCACHE_ENTRY_SIZE_MAX) {
|
2006-02-23 02:27:19 +00:00
|
|
|
pa_log(__FILE__": File too large %s", fname);
|
2005-09-16 00:04:29 +00:00
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|