Show readfreebob.c syntax highlighted
/***************************************************************************
Copyright (C) 2005 by Pieter Palmers *
*
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 2 of the License, or *
(at your option) any later version. *
*
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, write to the *
Free Software Foundation, Inc., *
59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
/**
* Test application for the per-stream decode API
*
* Modified by L Girod
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sndfile.h>
#include <signal.h>
#include <libmisc/misc.h>
#include "libfreebob/freebob_streaming.h"
int run;
static void sighandler (int sig)
{
run = 0;
}
#define DIRECT
int main(int argc, char *argv[])
{
#define PERIOD_SIZE 1024
int samplesread=0;
int nb_in_channels=0, nb_out_channels=0;
int retval=0;
int i=0;
int nb_periods=0;
freebob_sample_t **audiobuffer;
run=1;
printf("Freebob->multichannel .wav\n");
signal (SIGINT, sighandler);
signal (SIGPIPE, sighandler);
freebob_device_info_t device_info;
freebob_options_t dev_options = {
sample_rate: 48000,
period_size: PERIOD_SIZE,
nb_buffers: 3,
port: 0,
node_id: -1,
packetizer_priority: 60,
directions: FREEBOB_IGNORE_PLAYBACK
};
char *output_filename;
if (misc_parse_out_switch(&argc, argv, "help", 'h')) {
fprintf(stderr,
"Usage:\n"
" -s <sample rate> (48000 default)\n"
" -p <firewire port> (0 default)\n"
" -n <node id> (-1 default)\n"
" -f <filename> (/tmp/outfile.wav default)\n"
);
exit(1);
}
misc_parse_option_as_int(&argc, argv, "sample_rate", 's', &(dev_options.sample_rate));
misc_parse_option_as_int(&argc, argv, "port", 'p', &(dev_options.port));
misc_parse_option_as_int(&argc, argv, "node_id", 'n', &(dev_options.node_id));
output_filename = misc_parse_out_option(&argc, argv, "filename", 'f');
if (output_filename == NULL)
output_filename = "/tmp/outfile.wav";
freebob_device_t *dev=freebob_streaming_init(&device_info, dev_options);
if (!dev) {
fprintf(stderr,"Could not init Freebob Streaming layer\n");
exit(-1);
}
printf("Reading from port %d, node %d, rate=%d, output to file '%s'\n",
dev_options.port, dev_options.node_id, dev_options.sample_rate,
output_filename);
nb_in_channels=freebob_streaming_get_nb_capture_streams(dev);
nb_out_channels=freebob_streaming_get_nb_playback_streams(dev);
int input_channel_count = 0;
#if 0
for (i=0;i<nb_in_channels;i++) {
freebob_streaming_get_capture_stream_name(dev,i,name,sizeof(name));
//fprintf(fid_in[i], "#Channel name: %s\n",name);
switch (freebob_streaming_get_capture_stream_type(dev,i)) {
case freebob_stream_type_audio:
break;
default:
case freebob_stream_type_midi:
case freebob_stream_type_unknown:
case freebob_stream_type_invalid:
break;
}
}
#else
input_channel_count = 8;
#endif
/* open output file */
SF_INFO sfinfo = {
samplerate: dev_options.sample_rate,
format: SF_FORMAT_WAV | SF_FORMAT_PCM_16,
channels: input_channel_count
};
SNDFILE *output_file = sf_open(output_filename, SFM_WRITE, &sfinfo);
if (output_file == NULL) {
fprintf(stderr, "Can't open output sound file %s: %m\n", output_filename);
exit(1);
}
/* allocate intermediate buffers */
audiobuffer=calloc(nb_in_channels,sizeof(freebob_sample_t *));
for (i=0;i<nb_in_channels;i++) {
audiobuffer[i]=calloc(PERIOD_SIZE+1,sizeof(freebob_sample_t));
#ifdef DIRECT
int status = freebob_streaming_set_capture_stream_buffer
(dev, i, (char*)audiobuffer[i], freebob_buffer_type_uint24);
if (status < 0) {
fprintf(stderr, "unable to install capture buffer! %m\n");
exit(1);
}
#endif
}
/* interleaved temp buffer */
int16_t interleaved[PERIOD_SIZE+1][input_channel_count];
freebob_streaming_start(dev);
fprintf(stderr,"Entering receive loop (%d,%d)\n",nb_in_channels,nb_out_channels);
while(run) {
retval = freebob_streaming_wait(dev);
if (retval < 0) {
fprintf(stderr,"Xrun\n");
freebob_streaming_reset(dev);
fprintf(stderr,"closing file and aborting\n");
sf_close(output_file);
exit(1);
}
freebob_streaming_transfer_buffers(dev);
nb_periods++;
if((nb_periods % 32)==0) {
fprintf(stderr,"\r%05d periods",nb_periods);
}
for(i=0;i<nb_in_channels;i++) {
switch (freebob_streaming_get_capture_stream_type(dev,i)) {
case freebob_stream_type_audio:
#ifdef DIRECT
/* it's already written in.. */
#else
samplesread=freebob_streaming_read(dev, i, audiobuffer[i], PERIOD_SIZE);
#endif
break;
case freebob_stream_type_midi:
samplesread=freebob_streaming_read(dev, i, audiobuffer[i], PERIOD_SIZE);
break;
default:
break;
}
}
int j;
samplesread=PERIOD_SIZE;
for (j=0; j<samplesread; j++) {
for (i=0; i<input_channel_count; i++) {
int x = ((freebob_sample_t *)audiobuffer[i])[j];
if (x & (1 << 23)) x -= 0xFFFFFF;
int16_t si = x >> 8;
interleaved[j][i] = si;
}
}
int sampleswritten = sf_writef_short(output_file, (int16_t*)interleaved,
samplesread);
if (sampleswritten != samplesread) {
printf("*** write error condition? %m aborting\n");
sf_close(output_file);
exit(1);
}
}
fprintf(stderr,"\n");
fprintf(stderr,"Exiting receive loop\n");
freebob_streaming_stop(dev);
freebob_streaming_finish(dev);
sf_close(output_file);
return 0;
}
See more files for this project here