From 638d2e14ae12948e2965e874df6e1f888394b6f5 Mon Sep 17 00:00:00 2001 From: erdgeist <> Date: Tue, 2 Apr 2013 01:47:30 +0000 Subject: have the outside remember, if it is the first frame --- timestretch.c | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/timestretch.c b/timestretch.c index b15bfe7..1f94837 100644 --- a/timestretch.c +++ b/timestretch.c @@ -17,7 +17,9 @@ static size_t g_corr_length; static size_t g_skip; // Per frame skip static size_t g_offset; // Offset into stream, lower bits -static size_t g_firstframe; // If this is set, the caller should provide initial data in g_overlap_buffer + +#define FRAME_FIRST 0x01 +#define FRAME_LAST 0x02 /* some good default for mixing voice, values in micro seconds */ #define OVERLAP 10 // overlapping length (music default = 12 ms) @@ -33,7 +35,6 @@ static size_t calc_convert_values( int sample_rate, float tempo ) { free( g_overlap_buffer ); g_overlap_buffer = malloc( sizeof(short) * g_overlap ); - g_firstframe = 1; g_offset = 0; g_output_length = (sample_rate * OUTPUT_LEN ) / 1000; @@ -47,7 +48,7 @@ static size_t calc_convert_values( int sample_rate, float tempo ) { // boost middle of sequence by top of a flat parabola // slope stolen from soundtouch, precalc table for( i = 0; i < g_corr_length; ++i ) - g_overlap_heuristic[i] = (short)16384.0*(-(double)i*(double)i/((double)g_corr_length*(double)g_corr_length)+(double)i/(double)g_corr_length+0.75f); + g_overlap_heuristic[i] = (short)16384.0*(-(double)(i*i)/((double)(g_corr_length*g_corr_length))+(double)i/(double)g_corr_length+0.75f); g_skip = (size_t)( tempo * (float)g_output_length * 65536.0 ); g_input_length = g_corr_length + g_output_length + g_overlap; @@ -88,16 +89,15 @@ static unsigned int find_corr_max(const short *input, const short *mixbuf) { } // Returns the amount of samples that can be discarded from begin of the input buffer -size_t process_frame( short *input, short *output, short *overlap, int lastframe ) { +size_t process_frame( short *input, short *output, short *overlap, int frame_flag ) { int i, i_ = (int)g_overlap; unsigned int offset = 0; // The first frame needs to be copied verbatim, // we do not have anything to mix, yet. - if( g_firstframe ) { + if( frame_flag & FRAME_FIRST ) sampcpy( output, input, g_output_length ); - g_firstframe = 0; - } else { + else { offset = find_corr_max( input, overlap ); // Cross fade end of last frame with begin of this frame @@ -109,7 +109,7 @@ size_t process_frame( short *input, short *output, short *overlap, int lastframe } // On the last frame help connect the next frame from input seamlessly - if( lastframe ) + if( frame_flag & FRAME_LAST ) return offset + g_output_length; // Remember end of this frame for next frame @@ -126,14 +126,13 @@ int main( int args, char **argv ) { size_t in_fill = 0; short outbuf[ g_output_length ]; short inbuf [ g_input_length ]; + int fd_in = open( "in.raw", O_RDONLY ); + int fd_out = open( "out.raw", O_CREAT | O_WRONLY | O_TRUNC ); + int first_frame = FRAME_FIRST; + (void)args; (void)argv; (void)out_chunk_size; // printf( "DEBUG: OL: %zd SWL: %zd SL: %zd SK: %zd ICM: %zd\n", g_overlap, g_output_length, g_corr_length, g_skip / 65536, g_input_length ); - int fd_in = open( "in.raw", O_RDONLY ); - int fd_out = open( "out.raw", O_CREAT | O_WRONLY | O_TRUNC ); - - (void)args; (void)argv; (void)out_chunk_size; - while( 1 ) { size_t processed; size_t missing = g_input_length - in_fill; @@ -148,7 +147,8 @@ int main( int args, char **argv ) { } // Do one cycle of processing and outputting - processed = process_frame( inbuf, outbuf, g_overlap_buffer, 0 ); + processed = process_frame( inbuf, outbuf, g_overlap_buffer, first_frame ); + first_frame = 0; write( fd_out, outbuf, g_output_length * sizeof(short) ); memmove( inbuf, inbuf + processed, ( in_fill - processed ) * sizeof(short) ); -- cgit v1.2.3