From a27dfe1feb8d3b5bae957531f1a8e634b3b5b8ef Mon Sep 17 00:00:00 2001 From: Erik Hofman Date: Thu, 26 Mar 2020 09:55:11 +0100 Subject: [PATCH] Move more modern C++ idioms and use std::unique_ptr for moving data ownership around --- src/Sound/VoiceSynthesizer.cxx | 6 +++++- src/Sound/beacon.cxx | 32 ++++++++++++++++++++------------ src/Sound/morse.cxx | 9 ++++++--- 3 files changed, 31 insertions(+), 16 deletions(-) diff --git a/src/Sound/VoiceSynthesizer.cxx b/src/Sound/VoiceSynthesizer.cxx index befb0adbf..7ae061214 100644 --- a/src/Sound/VoiceSynthesizer.cxx +++ b/src/Sound/VoiceSynthesizer.cxx @@ -123,7 +123,11 @@ SGSoundSample * FLITEVoiceSynthesizer::synthesize(const std::string & text, doub ALsizei rate, count; 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{ + reinterpret_cast( data ), + free + }; + return new SGSoundSample(buf, count * sizeof(short), rate, SG_SAMPLE_MONO16); diff --git a/src/Sound/beacon.cxx b/src/Sound/beacon.cxx index 46f0013a4..90dcf0074 100644 --- a/src/Sound/beacon.cxx +++ b/src/Sound/beacon.cxx @@ -43,24 +43,32 @@ bool FGBeacon::init() { unsigned char *ptr; size_t i, len; - const unsigned char* inner_buf = (const unsigned char*)malloc( INNER_SIZE ); - const unsigned char* middle_buf = (const unsigned char*)malloc(MIDDLE_SIZE); - const unsigned char* outer_buf = (const unsigned char*)malloc( OUTER_SIZE ); + auto inner_buf = std::unique_ptr{ + reinterpret_cast( malloc( INNER_SIZE ) ), + free + }; + auto middle_buf = std::unique_ptr{ + reinterpret_cast( malloc( MIDDLE_SIZE ) ), + free + }; + auto outer_buf = std::unique_ptr{ + reinterpret_cast( malloc( OUTER_SIZE ) ), + free + }; // Make inner marker beacon sound len= (int)(INNER_DIT_LEN / 2.0 ); unsigned char inner_dit[INNER_DIT_LEN]; - make_tone( inner_dit, INNER_FREQ, len, INNER_DIT_LEN, - TRANSITION_BYTES ); + make_tone( inner_dit, INNER_FREQ, len, INNER_DIT_LEN, TRANSITION_BYTES ); - ptr = (unsigned char*)inner_buf; + ptr = inner_buf.get(); for ( i = 0; i < 6; ++i ) { memcpy( ptr, inner_dit, INNER_DIT_LEN ); ptr += INNER_DIT_LEN; } 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_max_dist( 20.0 ); @@ -75,12 +83,12 @@ bool FGBeacon::init() { make_tone( middle_dah, MIDDLE_FREQ, len, MIDDLE_DAH_LEN, TRANSITION_BYTES ); - ptr = (unsigned char*)middle_buf; + ptr = (unsigned char*)middle_buf.get(); memcpy( ptr, middle_dit, MIDDLE_DIT_LEN ); ptr += MIDDLE_DIT_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_max_dist( 20.0 ); @@ -89,13 +97,13 @@ bool FGBeacon::init() { unsigned char outer_dah[OUTER_DAH_LEN]; make_tone( outer_dah, OUTER_FREQ, len, OUTER_DAH_LEN, TRANSITION_BYTES ); - - ptr = (unsigned char*)outer_buf; + + ptr = (unsigned char*)outer_buf.get(); memcpy( ptr, outer_dah, OUTER_DAH_LEN ); ptr += 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_max_dist( 20.0 ); } catch ( sg_io_exception &e ) { diff --git a/src/Sound/morse.cxx b/src/Sound/morse.cxx index 9e4d5eced..ffef13c59 100644 --- a/src/Sound/morse.cxx +++ b/src/Sound/morse.cxx @@ -190,10 +190,13 @@ SGSoundSample *FGMorse::make_ident( const std::string& id, const int freq ) { length += 2 * SPACE_SIZE; // 2. Allocate space for the message - const unsigned char* buffer = (const unsigned char *)malloc(length); + auto buffer = std::unique_ptr{ + reinterpret_cast( malloc( length ) ), + free + }; // 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 ) { 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; // 4. create the simple sound and return - SGSoundSample *sample = new SGSoundSample( &buffer, length, + SGSoundSample *sample = new SGSoundSample( buffer, length, BYTES_PER_SECOND ); sample->set_reference_dist( 10.0 );