2011-06-08 23:00:22 +02:00
|
|
|
/*
|
|
|
|
* simpleaudio-sndfile.c
|
|
|
|
*
|
|
|
|
* Copyright (C) 2011 Kamal Mostafa <kamal@whence.com>
|
|
|
|
*
|
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
|
|
|
*/
|
|
|
|
|
2011-05-30 07:20:28 +02:00
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <malloc.h>
|
|
|
|
#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
|
2011-05-30 07:20:28 +02:00
|
|
|
sa_sndfile_read( simpleaudio *sa, float *buf, size_t nframes )
|
|
|
|
{
|
|
|
|
SNDFILE *s = (SNDFILE *)sa->backend_handle;
|
|
|
|
int n;
|
|
|
|
if ((n = sf_readf_float(s, buf, nframes)) < 0) {
|
|
|
|
fprintf(stderr, "sf_read_float: ");
|
|
|
|
sf_perror(s);
|
|
|
|
return -1;
|
|
|
|
}
|
2011-06-19 18:40:49 +02:00
|
|
|
//fprintf(stderr, "sf_readf_float: nframes=%ld n=%d\n", nframes, n);
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static ssize_t
|
|
|
|
sa_sndfile_write( simpleaudio *sa, float *buf, size_t nframes )
|
|
|
|
{
|
|
|
|
//fprintf(stderr, "sf_writef_float: nframes=%ld\n", nframes);
|
|
|
|
SNDFILE *s = (SNDFILE *)sa->backend_handle;
|
|
|
|
int n;
|
|
|
|
if ((n = sf_writef_float(s, buf, nframes)) < 0) {
|
|
|
|
fprintf(stderr, "sf_read_float: ");
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static const struct simpleaudio_backend simpleaudio_backend_pulse = {
|
|
|
|
sa_sndfile_read,
|
2011-06-19 18:40:49 +02:00
|
|
|
sa_sndfile_write,
|
2011-05-30 07:20:28 +02:00
|
|
|
sa_sndfile_close,
|
|
|
|
};
|
|
|
|
|
|
|
|
simpleaudio *
|
2011-06-19 18:40:49 +02:00
|
|
|
simpleaudio_open_stream_sndfile(
|
|
|
|
int sa_stream_direction,
|
|
|
|
char *path )
|
2011-05-30 07:20:28 +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 = {
|
2011-06-19 18:40:49 +02:00
|
|
|
.format = SF_FORMAT_FLAC | SF_FORMAT_PCM_16, // FIXME - hardcoded
|
|
|
|
.samplerate = 48000,
|
|
|
|
.channels = 1,
|
2011-05-30 07:20:28 +02:00
|
|
|
};
|
|
|
|
|
2011-06-19 18:40:49 +02:00
|
|
|
if ( sa_stream_direction == SA_STREAM_RECORD )
|
|
|
|
sfinfo.format = 0;
|
|
|
|
|
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);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
simpleaudio *sa = malloc(sizeof(simpleaudio));
|
|
|
|
if ( !sa ) {
|
|
|
|
perror("malloc");
|
|
|
|
sf_close(s);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
sa->rate = sfinfo.samplerate;
|
|
|
|
sa->channels = sfinfo.channels;
|
|
|
|
sa->backend = &simpleaudio_backend_pulse;
|
|
|
|
sa->backend_handle = s;
|
|
|
|
sa->backend_framesize = sa->channels * sizeof(float);
|
|
|
|
|
|
|
|
return sa;
|
|
|
|
}
|
|
|
|
|