2008-03-25 08:08:13 +01:00
|
|
|
|
|
|
|
/*******************************************************************************/
|
|
|
|
/* Copyright (C) 2008 Jonathan Moore Liles */
|
|
|
|
/* */
|
|
|
|
/* 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; see the file COPYING. If not,write to the Free Software */
|
|
|
|
/* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
|
|
|
|
/*******************************************************************************/
|
|
|
|
|
2008-03-25 22:17:24 +01:00
|
|
|
#include "Peak_Server.H"
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
|
2008-03-25 08:08:13 +01:00
|
|
|
/* Peak Server
|
|
|
|
|
2008-03-25 23:50:12 +01:00
|
|
|
The Peak Server streams peak data to any Timeline Editors or other
|
|
|
|
clients that ask for it.
|
2008-03-25 08:08:13 +01:00
|
|
|
|
2008-03-25 23:50:12 +01:00
|
|
|
Peak request looks like (in ASCII)
|
2008-03-25 08:08:13 +01:00
|
|
|
|
|
|
|
> read_peaks "foo.wav" fpp start end
|
|
|
|
|
2008-03-25 23:50:12 +01:00
|
|
|
Where "foo.wav" is the base name. (actual filenames may differ if
|
|
|
|
the channels of the source are 'broken out')
|
2008-03-25 08:08:13 +01:00
|
|
|
|
2008-03-25 23:50:12 +01:00
|
|
|
Response looks like (in binary)
|
2008-03-25 08:08:13 +01:00
|
|
|
|
2008-03-25 23:50:12 +01:00
|
|
|
> (int)channels (int)length (float)min max min max min max
|
|
|
|
> length ...
|
|
|
|
|
|
|
|
Were length specifies the number of Peaks (min/max pairs). The
|
|
|
|
first channel is transmitted first, and any others follow.
|
2008-03-25 08:08:13 +01:00
|
|
|
*/
|
2008-03-25 22:17:24 +01:00
|
|
|
|
|
|
|
#include "Audio_File.H"
|
|
|
|
#include "Peaks.H"
|
|
|
|
|
|
|
|
typedef unsigned long tick_t;
|
|
|
|
|
|
|
|
#define PEAK_PORT 6100
|
|
|
|
|
|
|
|
void
|
|
|
|
Peak_Server::handle_new ( int s )
|
|
|
|
{
|
|
|
|
printf( "new connection\n" );
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Peak_Server::handle_hang_up ( int s )
|
|
|
|
{
|
|
|
|
printf( "hangup\n" );
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Peak_Server::handle_request ( int s, const char *buf, int l )
|
|
|
|
{
|
|
|
|
printf( "request: %s", buf );
|
|
|
|
|
|
|
|
char source[512];
|
|
|
|
float fpp;
|
|
|
|
tick_t start, end;
|
|
|
|
|
|
|
|
if ( 4 != sscanf( buf, "read_peaks \"%[^\"]\" %f %lu %lu", source, &fpp, &start, &end ) )
|
2008-03-25 23:50:12 +01:00
|
|
|
{
|
|
|
|
const char *err = "error: malformed request\n";
|
|
|
|
fprintf( stderr, err );
|
|
|
|
send( s, err, strlen( err ), 0 );
|
|
|
|
return;
|
|
|
|
}
|
2008-03-25 22:17:24 +01:00
|
|
|
|
|
|
|
Audio_File *af = Audio_File::from_file( source );
|
|
|
|
|
2008-03-25 23:50:12 +01:00
|
|
|
if ( ! af )
|
|
|
|
{
|
|
|
|
const char *err = "error: could not open source\n";
|
|
|
|
send( s, err, strlen( err ), 0 );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-03-25 22:17:24 +01:00
|
|
|
int channels = af->channels();
|
|
|
|
|
|
|
|
send( s, &channels, sizeof( int ), 0 );
|
|
|
|
|
|
|
|
for ( int i = 0; i < af->channels(); ++i )
|
|
|
|
{
|
|
|
|
const Peaks *pk = af->peaks( i );
|
|
|
|
|
|
|
|
int peaks = pk->fill_buffer( fpp, start, end );
|
|
|
|
|
|
|
|
send( s, &peaks, sizeof( int ), 0 );
|
|
|
|
|
|
|
|
send( s, pk->peakbuf(), peaks * sizeof( Peak ), 0 );
|
|
|
|
}
|
|
|
|
|
2008-03-25 23:50:12 +01:00
|
|
|
// delete af;
|
2008-03-25 22:17:24 +01:00
|
|
|
}
|