non/Engine/Peak_Server.C

151 lines
4.2 KiB
C++
Raw Normal View History

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
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
Peak request looks like (in ASCII)
2008-03-25 08:08:13 +01:00
> read_peaks "foo.wav" fpp start end
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
Response looks like (in binary)
2008-03-25 08:08:13 +01:00
> (int)channels (int)length (float)min max min max min max
> min max min max
Were length specifies the number of Peaks (min/max pairs) (for each
channel) 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;
enum { GET_INFO, READ_PEAKS } request;
if ( 1 == sscanf( buf, "get_info \"%[^\"]\"", source ) )
{
request = GET_INFO;
}
else if ( 4 == sscanf( buf, "read_peaks \"%[^\"]\" %f %lu %lu", source, &fpp, &start, &end ) )
{
request = READ_PEAKS;
}
else
{
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 );
if ( ! af )
{
const char *err = "error: could not open source\n";
send( s, err, strlen( err ), 0 );
return;
}
switch ( request )
{
case GET_INFO:
{
char buf[128];
snprintf( buf, sizeof( buf ), "length=%lu channels=%d\n", af->length(), af->channels() );
2008-03-25 22:17:24 +01:00
send( s, buf, strlen( buf ), 0 );
2008-03-25 22:17:24 +01:00
break;
}
case READ_PEAKS:
{
int data[2];
int peaks;
data[0] = af->channels();
data[1] = peaks = (end - start) / fpp;
send( s, &data, sizeof( data ), 0 );
2008-03-27 00:21:11 +01:00
const Peaks *pk = af->peaks();
2008-03-27 00:21:11 +01:00
int npeaks = pk->fill_buffer( fpp, start, end );
2008-03-27 00:21:11 +01:00
int channels = af->channels();
2008-03-25 22:17:24 +01:00
2008-03-27 01:46:38 +01:00
Peak *pbuf = new Peak[ npeaks * channels ];
/* deinterlace */
int k = 0;
2008-03-27 00:21:11 +01:00
for ( int i = 0; i < channels; i++ )
for ( int j = i; j < npeaks * channels; j += channels )
2008-03-27 01:46:38 +01:00
pbuf[ k++ ] = pk->peakbuf()[ j ];
2008-03-25 22:17:24 +01:00
2008-03-27 01:46:38 +01:00
/* transmit */
send( s, pbuf, sizeof( Peak ) * npeaks * channels, 0 );
2008-03-27 00:21:11 +01:00
2008-03-27 01:46:38 +01:00
delete pbuf;
2008-03-25 22:17:24 +01:00
break;
}
2008-03-25 22:17:24 +01:00
}
// delete af;
2008-03-25 22:17:24 +01:00
}