2011-06-08 23:00:22 +02:00
|
|
|
/*
|
|
|
|
* simpleaudio-sndfile.c
|
|
|
|
*
|
2012-08-14 06:51:53 +02:00
|
|
|
* Copyright (C) 2011-2012 Kamal Mostafa <kamal@whence.com>
|
2011-06-08 23:00:22 +02:00
|
|
|
*
|
2011-06-23 02:10:50 +02:00
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
2011-06-08 23:00:22 +02:00
|
|
|
*
|
2011-06-23 02:10:50 +02:00
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but 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.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2011-06-08 23:00:22 +02:00
|
|
|
*/
|
|
|
|
|
2012-08-06 22:14:26 +02:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if USE_SNDFILE
|
2011-05-30 07:20:28 +02:00
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <malloc.h>
|
2011-06-24 08:46:18 +02:00
|
|
|
#include <string.h>
|
2011-05-30 07:20:28 +02:00
|
|
|
#include <assert.h>
|
|
|
|
|
|
|
|
#include <sndfile.h>
|
|
|
|
|
|
|
|
#include "simpleaudio.h"
|
|
|
|
#include "simpleaudio_internal.h"
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* sndfile backend for simpleaudio
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2011-06-08 01:31:26 +02:00
|
|
|
static ssize_t
|
2012-08-12 23:39:23 +02:00
|
|
|
sa_sndfile_read( simpleaudio *sa, void *buf, size_t nframes )
|
2011-05-30 07:20:28 +02:00
|
|
|
{
|
|
|
|
SNDFILE *s = (SNDFILE *)sa->backend_handle;
|
|
|
|
int n;
|
2012-08-13 00:39:04 +02:00
|
|
|
switch ( sa->format ) {
|
|
|
|
case SA_SAMPLE_FORMAT_FLOAT:
|
|
|
|
n = sf_readf_float(s, buf, nframes);
|
|
|
|
break;
|
|
|
|
case SA_SAMPLE_FORMAT_S16:
|
|
|
|
n = sf_readf_short(s, buf, nframes);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
assert(0);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if ( n < 0 ) {
|
|
|
|
fprintf(stderr, "sf_read: ");
|
2011-05-30 07:20:28 +02:00
|
|
|
sf_perror(s);
|
|
|
|
return -1;
|
|
|
|
}
|
2012-08-13 00:39:04 +02:00
|
|
|
// fprintf(stderr, "sf_read: nframes=%ld n=%d\n", nframes, n);
|
2011-06-19 18:40:49 +02:00
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static ssize_t
|
2012-08-12 23:39:23 +02:00
|
|
|
sa_sndfile_write( simpleaudio *sa, void *buf, size_t nframes )
|
2011-06-19 18:40:49 +02:00
|
|
|
{
|
2012-08-13 00:39:04 +02:00
|
|
|
// fprintf(stderr, "sf_write: nframes=%ld\n", nframes);
|
2011-06-19 18:40:49 +02:00
|
|
|
SNDFILE *s = (SNDFILE *)sa->backend_handle;
|
|
|
|
int n;
|
2012-08-13 00:39:04 +02:00
|
|
|
switch ( sa->format ) {
|
|
|
|
case SA_SAMPLE_FORMAT_FLOAT:
|
|
|
|
n = sf_writef_float(s, buf, nframes);
|
|
|
|
break;
|
|
|
|
case SA_SAMPLE_FORMAT_S16:
|
|
|
|
n = sf_writef_short(s, buf, nframes);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
assert(0);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if ( n < 0 ) {
|
|
|
|
fprintf(stderr, "sf_write: ");
|
2011-06-19 18:40:49 +02:00
|
|
|
sf_perror(s);
|
|
|
|
return -1;
|
|
|
|
}
|
2011-05-30 07:20:28 +02:00
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
sa_sndfile_close( simpleaudio *sa )
|
|
|
|
{
|
|
|
|
sf_close(sa->backend_handle);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-06-24 08:46:18 +02:00
|
|
|
/* (Why) doesn't libsndfile provide an API for this?... */
|
|
|
|
static const struct sndfile_format {
|
|
|
|
unsigned int major_format;
|
|
|
|
char *str;
|
|
|
|
} sndfile_formats[] = {
|
|
|
|
{ SF_FORMAT_WAV, "WAV" },
|
|
|
|
{ SF_FORMAT_AIFF, "AIFF" },
|
|
|
|
{ SF_FORMAT_AU, "AU" },
|
|
|
|
{ SF_FORMAT_RAW, "RAW" },
|
|
|
|
{ SF_FORMAT_PAF, "PAF" },
|
|
|
|
{ SF_FORMAT_SVX, "SVX" },
|
|
|
|
{ SF_FORMAT_NIST, "NIST" },
|
|
|
|
{ SF_FORMAT_VOC, "VOC" },
|
|
|
|
{ SF_FORMAT_IRCAM, "IRCAM" },
|
|
|
|
{ SF_FORMAT_W64, "W64" },
|
|
|
|
{ SF_FORMAT_MAT4, "MAT4" },
|
|
|
|
{ SF_FORMAT_MAT5, "MAT5" },
|
|
|
|
{ SF_FORMAT_PVF, "PVF" },
|
|
|
|
{ SF_FORMAT_XI, "XI" },
|
|
|
|
{ SF_FORMAT_HTK, "HTK" },
|
|
|
|
{ SF_FORMAT_SDS, "SDS" },
|
|
|
|
{ SF_FORMAT_AVR, "AVR" },
|
|
|
|
{ SF_FORMAT_WAVEX, "WAVEX" },
|
|
|
|
{ SF_FORMAT_SD2, "SD2" },
|
|
|
|
{ SF_FORMAT_FLAC, "FLAC" },
|
|
|
|
{ SF_FORMAT_CAF, "CAF" },
|
|
|
|
{ SF_FORMAT_WVE, "WVE" },
|
|
|
|
{ SF_FORMAT_OGG, "OGG" },
|
|
|
|
{ SF_FORMAT_MPC2K, "MPC2K" },
|
|
|
|
{ SF_FORMAT_RF64, "RF64" },
|
|
|
|
{ 0, 0 }
|
|
|
|
};
|
|
|
|
|
|
|
|
static unsigned int
|
|
|
|
sndfile_format_from_path( const char *path )
|
|
|
|
{
|
|
|
|
const char *p = strrchr(path, '.');
|
|
|
|
if ( p )
|
|
|
|
p++;
|
|
|
|
else
|
|
|
|
p = path;
|
|
|
|
|
|
|
|
const struct sndfile_format *sfmt;
|
|
|
|
for ( sfmt=sndfile_formats; sfmt->str; sfmt++ )
|
|
|
|
if ( strcasecmp(sfmt->str,p) == 0 )
|
|
|
|
return sfmt->major_format;
|
|
|
|
return SF_FORMAT_WAV;
|
|
|
|
}
|
|
|
|
|
2012-08-12 23:24:55 +02:00
|
|
|
static int
|
|
|
|
sa_sndfile_open_stream(
|
|
|
|
simpleaudio *sa,
|
|
|
|
sa_direction_t sa_stream_direction,
|
|
|
|
sa_format_t sa_format,
|
2012-08-11 19:07:12 +02:00
|
|
|
unsigned int rate, unsigned int channels,
|
2012-08-12 23:24:55 +02:00
|
|
|
char *app_name, char *stream_name )
|
2011-05-30 07:20:28 +02:00
|
|
|
{
|
2012-08-12 23:24:55 +02:00
|
|
|
const char *path = stream_name;
|
|
|
|
|
2012-08-13 00:39:04 +02:00
|
|
|
int sf_format;
|
|
|
|
switch ( sa->format ) {
|
|
|
|
case SA_SAMPLE_FORMAT_FLOAT:
|
|
|
|
sf_format = SF_FORMAT_FLOAT;
|
|
|
|
break;
|
|
|
|
case SA_SAMPLE_FORMAT_S16:
|
|
|
|
sf_format = SF_FORMAT_PCM_16;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
assert(0);
|
|
|
|
}
|
2012-08-12 06:00:37 +02:00
|
|
|
|
2011-06-19 18:40:49 +02:00
|
|
|
/* setting for SA_STREAM_PLAYBACK (file write) */
|
2011-05-30 07:20:28 +02:00
|
|
|
SF_INFO sfinfo = {
|
2012-08-13 00:39:04 +02:00
|
|
|
.format = sf_format,
|
2012-08-11 19:07:12 +02:00
|
|
|
.samplerate = rate,
|
|
|
|
.channels = channels,
|
2011-05-30 07:20:28 +02:00
|
|
|
};
|
|
|
|
|
2011-06-24 08:46:18 +02:00
|
|
|
if ( sa_stream_direction == SA_STREAM_PLAYBACK )
|
2012-08-13 00:39:04 +02:00
|
|
|
sfinfo.format = sndfile_format_from_path(path) | sf_format;
|
2011-06-19 18:40:49 +02:00
|
|
|
|
2011-05-30 07:20:28 +02:00
|
|
|
/* Create the recording stream */
|
|
|
|
SNDFILE *s;
|
2011-06-19 18:40:49 +02:00
|
|
|
s = sf_open(path,
|
|
|
|
sa_stream_direction == SA_STREAM_RECORD ? SFM_READ : SFM_WRITE,
|
|
|
|
&sfinfo);
|
2011-05-30 07:20:28 +02:00
|
|
|
if ( !s ) {
|
|
|
|
fprintf(stderr, "%s: ", path);
|
|
|
|
sf_perror(s);
|
2012-08-12 23:24:55 +02:00
|
|
|
return 0;
|
2011-05-30 07:20:28 +02:00
|
|
|
}
|
|
|
|
|
2012-08-12 23:24:55 +02:00
|
|
|
/* good or bad to override these? */
|
2011-05-30 07:20:28 +02:00
|
|
|
sa->rate = sfinfo.samplerate;
|
|
|
|
sa->channels = sfinfo.channels;
|
2012-08-12 23:24:55 +02:00
|
|
|
|
2011-05-30 07:20:28 +02:00
|
|
|
sa->backend_handle = s;
|
2012-08-12 05:40:20 +02:00
|
|
|
sa->backend_framesize = sa->channels * sa->samplesize;
|
2011-05-30 07:20:28 +02:00
|
|
|
|
2012-08-12 23:24:55 +02:00
|
|
|
return 1;
|
2011-05-30 07:20:28 +02:00
|
|
|
}
|
|
|
|
|
2012-08-12 23:24:55 +02:00
|
|
|
|
|
|
|
const struct simpleaudio_backend simpleaudio_backend_sndfile = {
|
|
|
|
sa_sndfile_open_stream,
|
|
|
|
sa_sndfile_read,
|
|
|
|
sa_sndfile_write,
|
|
|
|
sa_sndfile_close,
|
|
|
|
};
|
|
|
|
|
2012-08-06 22:14:26 +02:00
|
|
|
#endif /* USE_SNDFILE */
|