Sequencer: Fix casting/sign related bug which could cause a hang during SMF writing.

pull/119/merge
Jonathan Moore Liles 2020-09-16 18:19:07 -07:00
parent 1a07dda5d9
commit 8109ff7bc0
2 changed files with 23 additions and 18 deletions

View File

@ -20,6 +20,7 @@
#include "smf.H" #include "smf.H"
#include "phrase.H" #include "phrase.H"
#include "pattern.H" #include "pattern.H"
#include <math.h>
using namespace MIDI; using namespace MIDI;
@ -143,11 +144,11 @@ smf::read_byte ( void )
} }
void void
smf::write_var ( long var ) smf::write_var ( unsigned long var )
{ {
long buffer; unsigned long buffer = var & 0x7F;
buffer = var & 0x7F; byte_t buf[4];
/* we shift it right 7, if there is /* we shift it right 7, if there is
still set bits, encode into buffer still set bits, encode into buffer
in reverse order */ in reverse order */
@ -157,15 +158,19 @@ smf::write_var ( long var )
buffer |= ( var & 0x7F ) | 0x80; buffer |= ( var & 0x7F ) | 0x80;
} }
for ( ;; ) int i = 0;
while ( i < 4 )
{ {
write_byte( buffer ); buf[i++] = buffer;
if ( buffer & 0x80 ) if ( buffer & 0x80 )
buffer >>= 8; buffer >>= 8;
else else
break; break;
} }
write_bytes( buf, i );
} }
@ -182,15 +187,6 @@ smf::write_long ( unsigned long x )
write_bytes( buf, 4 ); write_bytes( buf, 4 );
} }
void
smf::write_ascii ( const char *buf )
{
if ( strlen( buf ) != 4 )
ASSERTION( "invalid MIDI value" );
write_bytes( (void *)buf, 4 );
}
void void
smf::write_short ( unsigned short x ) smf::write_short ( unsigned short x )
{ {
@ -208,6 +204,15 @@ smf::write_byte ( byte_t b )
write_bytes( &b, 1 ); write_bytes( &b, 1 );
} }
void
smf::write_ascii ( const char *buf )
{
if ( strlen( buf ) != 4 )
ASSERTION( "invalid MIDI value" );
write_bytes( (void *)buf, 4 );
}
void void
smf::write_bytes ( const void *p, size_t l ) smf::write_bytes ( const void *p, size_t l )
@ -231,7 +236,7 @@ smf::write_event ( const midievent *e )
tick_t delta = ts - _time; tick_t delta = ts - _time;
_time = ts; _time = ts;
write_var( delta ); write_var( floor(delta) );
if ( _cue && (e->is_note_off() || e->is_note_on() ) ) if ( _cue && (e->is_note_off() || e->is_note_on() ) )
{ {

View File

@ -64,7 +64,7 @@ public:
void read_bytes ( void *p, int l ); void read_bytes ( void *p, int l );
byte_t read_byte ( void ); byte_t read_byte ( void );
void write_var ( long var ); void write_var ( unsigned long var );
void write_long ( unsigned long x ); void write_long ( unsigned long x );
void write_ascii ( const char *buf ); void write_ascii ( const char *buf );
void write_short ( unsigned short x ); void write_short ( unsigned short x );