1
0
Fork 0

Move more modern C++ idioms and use std::unique_ptr for moving data ownership around

This commit is contained in:
Erik Hofman 2020-03-26 09:55:11 +01:00
parent d4b1e42b83
commit a27dfe1feb
3 changed files with 31 additions and 16 deletions

View file

@ -123,7 +123,11 @@ SGSoundSample * FLITEVoiceSynthesizer::synthesize(const std::string & text, doub
ALsizei rate, count; ALsizei rate, count;
if ( FALSE == Flite_HTS_Engine_synthesize_samples_mono16(_engine, text.c_str(), &data, &count, &rate)) return NULL; if ( FALSE == Flite_HTS_Engine_synthesize_samples_mono16(_engine, text.c_str(), &data, &count, &rate)) return NULL;
return new SGSoundSample(&data, auto buf = std::unique_ptr<unsigned char, decltype(free)*>{
reinterpret_cast<unsigned char*>( data ),
free
};
return new SGSoundSample(buf,
count * sizeof(short), count * sizeof(short),
rate, rate,
SG_SAMPLE_MONO16); SG_SAMPLE_MONO16);

View file

@ -43,24 +43,32 @@ bool FGBeacon::init() {
unsigned char *ptr; unsigned char *ptr;
size_t i, len; size_t i, len;
const unsigned char* inner_buf = (const unsigned char*)malloc( INNER_SIZE ); auto inner_buf = std::unique_ptr<unsigned char, decltype(free)*>{
const unsigned char* middle_buf = (const unsigned char*)malloc(MIDDLE_SIZE); reinterpret_cast<unsigned char*>( malloc( INNER_SIZE ) ),
const unsigned char* outer_buf = (const unsigned char*)malloc( OUTER_SIZE ); free
};
auto middle_buf = std::unique_ptr<unsigned char, decltype(free)*>{
reinterpret_cast<unsigned char*>( malloc( MIDDLE_SIZE ) ),
free
};
auto outer_buf = std::unique_ptr<unsigned char, decltype(free)*>{
reinterpret_cast<unsigned char*>( malloc( OUTER_SIZE ) ),
free
};
// Make inner marker beacon sound // Make inner marker beacon sound
len= (int)(INNER_DIT_LEN / 2.0 ); len= (int)(INNER_DIT_LEN / 2.0 );
unsigned char inner_dit[INNER_DIT_LEN]; unsigned char inner_dit[INNER_DIT_LEN];
make_tone( inner_dit, INNER_FREQ, len, INNER_DIT_LEN, make_tone( inner_dit, INNER_FREQ, len, INNER_DIT_LEN, TRANSITION_BYTES );
TRANSITION_BYTES );
ptr = (unsigned char*)inner_buf; ptr = inner_buf.get();
for ( i = 0; i < 6; ++i ) { for ( i = 0; i < 6; ++i ) {
memcpy( ptr, inner_dit, INNER_DIT_LEN ); memcpy( ptr, inner_dit, INNER_DIT_LEN );
ptr += INNER_DIT_LEN; ptr += INNER_DIT_LEN;
} }
try { try {
inner = new SGSoundSample( &inner_buf, INNER_SIZE, BYTES_PER_SECOND ); inner = new SGSoundSample( inner_buf, INNER_SIZE, BYTES_PER_SECOND );
inner->set_reference_dist( 10.0 ); inner->set_reference_dist( 10.0 );
inner->set_max_dist( 20.0 ); inner->set_max_dist( 20.0 );
@ -75,12 +83,12 @@ bool FGBeacon::init() {
make_tone( middle_dah, MIDDLE_FREQ, len, MIDDLE_DAH_LEN, make_tone( middle_dah, MIDDLE_FREQ, len, MIDDLE_DAH_LEN,
TRANSITION_BYTES ); TRANSITION_BYTES );
ptr = (unsigned char*)middle_buf; ptr = (unsigned char*)middle_buf.get();
memcpy( ptr, middle_dit, MIDDLE_DIT_LEN ); memcpy( ptr, middle_dit, MIDDLE_DIT_LEN );
ptr += MIDDLE_DIT_LEN; ptr += MIDDLE_DIT_LEN;
memcpy( ptr, middle_dah, MIDDLE_DAH_LEN ); memcpy( ptr, middle_dah, MIDDLE_DAH_LEN );
middle = new SGSoundSample( &middle_buf, MIDDLE_SIZE, BYTES_PER_SECOND); middle = new SGSoundSample( middle_buf, MIDDLE_SIZE, BYTES_PER_SECOND);
middle->set_reference_dist( 10.0 ); middle->set_reference_dist( 10.0 );
middle->set_max_dist( 20.0 ); middle->set_max_dist( 20.0 );
@ -89,13 +97,13 @@ bool FGBeacon::init() {
unsigned char outer_dah[OUTER_DAH_LEN]; unsigned char outer_dah[OUTER_DAH_LEN];
make_tone( outer_dah, OUTER_FREQ, len, OUTER_DAH_LEN, make_tone( outer_dah, OUTER_FREQ, len, OUTER_DAH_LEN,
TRANSITION_BYTES ); TRANSITION_BYTES );
ptr = (unsigned char*)outer_buf; ptr = (unsigned char*)outer_buf.get();
memcpy( ptr, outer_dah, OUTER_DAH_LEN ); memcpy( ptr, outer_dah, OUTER_DAH_LEN );
ptr += OUTER_DAH_LEN; ptr += OUTER_DAH_LEN;
memcpy( ptr, outer_dah, OUTER_DAH_LEN ); memcpy( ptr, outer_dah, OUTER_DAH_LEN );
outer = new SGSoundSample( &outer_buf, OUTER_SIZE, BYTES_PER_SECOND ); outer = new SGSoundSample( outer_buf, OUTER_SIZE, BYTES_PER_SECOND );
outer->set_reference_dist( 10.0 ); outer->set_reference_dist( 10.0 );
outer->set_max_dist( 20.0 ); outer->set_max_dist( 20.0 );
} catch ( sg_io_exception &e ) { } catch ( sg_io_exception &e ) {

View file

@ -190,10 +190,13 @@ SGSoundSample *FGMorse::make_ident( const std::string& id, const int freq ) {
length += 2 * SPACE_SIZE; length += 2 * SPACE_SIZE;
// 2. Allocate space for the message // 2. Allocate space for the message
const unsigned char* buffer = (const unsigned char *)malloc(length); auto buffer = std::unique_ptr<unsigned char, decltype(free)*>{
reinterpret_cast<unsigned char*>( malloc( length ) ),
free
};
// 3. Assemble the message; // 3. Assemble the message;
unsigned char *buf_ptr = (unsigned char*)buffer; unsigned char *buf_ptr = buffer.get();
for ( i = 0; i < (int)id.length(); ++i ) { for ( i = 0; i < (int)id.length(); ++i ) {
if ( idptr[i] >= 'A' && idptr[i] <= 'Z' ) { if ( idptr[i] >= 'A' && idptr[i] <= 'Z' ) {
@ -232,7 +235,7 @@ SGSoundSample *FGMorse::make_ident( const std::string& id, const int freq ) {
buf_ptr += SPACE_SIZE; buf_ptr += SPACE_SIZE;
// 4. create the simple sound and return // 4. create the simple sound and return
SGSoundSample *sample = new SGSoundSample( &buffer, length, SGSoundSample *sample = new SGSoundSample( buffer, length,
BYTES_PER_SECOND ); BYTES_PER_SECOND );
sample->set_reference_dist( 10.0 ); sample->set_reference_dist( 10.0 );