From 9cef27f5ca7486230d1158748f994cba0ceea673 Mon Sep 17 00:00:00 2001 From: Torsten Dreyer Date: Tue, 13 Sep 2011 20:06:31 +0200 Subject: [PATCH] Refactor morse and beacon as singleton --- projects/VC90/FlightGear/FlightGear.vcproj | 8 +++ src/Instrumentation/adf.cxx | 6 +- src/Instrumentation/adf.hxx | 2 - src/Instrumentation/kr_87.cxx | 4 +- src/Instrumentation/kr_87.hxx | 3 - src/Instrumentation/marker_beacon.cxx | 9 ++- src/Instrumentation/marker_beacon.hxx | 6 -- src/Instrumentation/navradio.cxx | 7 +-- src/Instrumentation/navradio.hxx | 3 - src/Sound/CMakeLists.txt | 4 +- src/Sound/Makefile.am | 1 + src/Sound/beacon.cxx | 13 ++++- src/Sound/beacon.hxx | 43 ++++++-------- src/Sound/morse.cxx | 50 ++++------------ src/Sound/morse.hxx | 29 +++------- src/Sound/soundgenerator.cxx | 64 +++++++++++++++++++++ src/Sound/soundgenerator.hxx | 67 ++++++++++++++++++++++ 17 files changed, 202 insertions(+), 117 deletions(-) create mode 100644 src/Sound/soundgenerator.cxx create mode 100644 src/Sound/soundgenerator.hxx diff --git a/projects/VC90/FlightGear/FlightGear.vcproj b/projects/VC90/FlightGear/FlightGear.vcproj index c9f3558c8..77d845292 100644 --- a/projects/VC90/FlightGear/FlightGear.vcproj +++ b/projects/VC90/FlightGear/FlightGear.vcproj @@ -3229,6 +3229,14 @@ RelativePath="..\..\..\src\Sound\sample_queue.hxx" > + + + + diff --git a/src/Instrumentation/adf.cxx b/src/Instrumentation/adf.cxx index 41ee7700e..e3fbac053 100644 --- a/src/Instrumentation/adf.cxx +++ b/src/Instrumentation/adf.cxx @@ -17,6 +17,8 @@ #include #include "adf.hxx" +#include + #include #include @@ -110,8 +112,6 @@ ADF::init () _sgr = smgr->find("avionics", true); _sgr->tie_to_listener(); - morse.init(); - std::ostringstream temp; temp << _name << _num; _adf_ident = temp.str(); @@ -252,7 +252,7 @@ ADF::search (double frequency_khz, double longitude_rad, } SGSoundSample *sound; - sound = morse.make_ident( ident, LO_FREQUENCY ); + sound = FGMorse::instance()->make_ident( ident, LO_FREQUENCY ); sound->set_volume(_last_volume = 0); _sgr->add( sound, _adf_ident ); diff --git a/src/Instrumentation/adf.hxx b/src/Instrumentation/adf.hxx index 88817a8ee..5b1b1667d 100644 --- a/src/Instrumentation/adf.hxx +++ b/src/Instrumentation/adf.hxx @@ -16,7 +16,6 @@ #include #include -#include using std::string; @@ -93,7 +92,6 @@ private: SGVec3d _transmitter_cart; double _transmitter_range_nm; - FGMorse morse; int _ident_count; time_t _last_ident_time; float _last_volume; diff --git a/src/Instrumentation/kr_87.cxx b/src/Instrumentation/kr_87.cxx index 30c0cbe66..9b91babea 100644 --- a/src/Instrumentation/kr_87.cxx +++ b/src/Instrumentation/kr_87.cxx @@ -36,6 +36,7 @@ #include "kr_87.hxx" +#include #include using std::string; @@ -119,7 +120,6 @@ void FGKR_87::init () { SGSoundMgr *smgr = globals->get_soundmgr(); _sgr = smgr->find("avionics", true); _sgr->tie_to_listener(); - morse.init(); } @@ -534,7 +534,7 @@ void FGKR_87::search() { _sgr->remove( "adf-ident" ); } SGSoundSample *sound; - sound = morse.make_ident( trans_ident, LO_FREQUENCY ); + sound = FGMorse::instance()->make_ident( trans_ident, LO_FREQUENCY ); sound->set_volume( 0.3 ); _sgr->add( sound, "adf-ident" ); diff --git a/src/Instrumentation/kr_87.hxx b/src/Instrumentation/kr_87.hxx index e59beba7b..14accba9f 100644 --- a/src/Instrumentation/kr_87.hxx +++ b/src/Instrumentation/kr_87.hxx @@ -32,14 +32,11 @@ #include #include -#include class SGSampleGroup; class FGKR_87 : public SGSubsystem { - FGMorse morse; - SGPropertyNode_ptr lon_node; SGPropertyNode_ptr lat_node; SGPropertyNode_ptr alt_node; diff --git a/src/Instrumentation/marker_beacon.cxx b/src/Instrumentation/marker_beacon.cxx index f7945722d..5fa56fade 100644 --- a/src/Instrumentation/marker_beacon.cxx +++ b/src/Instrumentation/marker_beacon.cxx @@ -34,6 +34,7 @@ #include #include "marker_beacon.hxx" +#include #include using std::string; @@ -120,8 +121,6 @@ FGMarkerBeacon::init () _sgr = smgr->find("avionics", true); _sgr->tie_to_listener(); - morse.init(); - beacon.init(); blink.stamp(); outer_marker = middle_marker = inner_marker = false; @@ -317,7 +316,7 @@ void FGMarkerBeacon::search() // cout << "OUTER MARKER" << endl; if ( last_beacon != OUTER ) { if ( ! _sgr->exists( current_sound_name ) ) { - SGSoundSample *sound = beacon.get_outer(); + SGSoundSample *sound = FGBeacon::instance()->get_outer(); if ( sound ) { _sgr->add( sound, current_sound_name ); } @@ -336,7 +335,7 @@ void FGMarkerBeacon::search() // cout << "MIDDLE MARKER" << endl; if ( last_beacon != MIDDLE ) { if ( ! _sgr->exists( current_sound_name ) ) { - SGSoundSample *sound = beacon.get_middle(); + SGSoundSample *sound = FGBeacon::instance()->get_middle(); if ( sound ) { _sgr->add( sound, current_sound_name ); } @@ -355,7 +354,7 @@ void FGMarkerBeacon::search() // cout << "INNER MARKER" << endl; if ( last_beacon != INNER ) { if ( ! _sgr->exists( current_sound_name ) ) { - SGSoundSample *sound = beacon.get_inner(); + SGSoundSample *sound = FGBeacon::instance()->get_inner(); if ( sound ) { _sgr->add( sound, current_sound_name ); } diff --git a/src/Instrumentation/marker_beacon.hxx b/src/Instrumentation/marker_beacon.hxx index 25e435fea..52259b6a2 100644 --- a/src/Instrumentation/marker_beacon.hxx +++ b/src/Instrumentation/marker_beacon.hxx @@ -32,16 +32,10 @@ #include #include -#include -#include - class SGSampleGroup; class FGMarkerBeacon : public SGSubsystem { - FGBeacon beacon; - FGMorse morse; - SGInterpTable *term_tbl; SGInterpTable *low_tbl; SGInterpTable *high_tbl; diff --git a/src/Instrumentation/navradio.cxx b/src/Instrumentation/navradio.cxx index 780094c42..886ad1f37 100644 --- a/src/Instrumentation/navradio.cxx +++ b/src/Instrumentation/navradio.cxx @@ -44,6 +44,7 @@ #include #include #include
+#include using std::string; @@ -175,8 +176,6 @@ FGNavRadio::init () _sgr = smgr->find("avionics", true); _sgr->tie_to_listener(); - morse.init(); - SGPropertyNode* node = _radio_node.get(); bus_power_node = fgGetNode(("/systems/electrical/outputs/" + _name).c_str(), true); @@ -1014,7 +1013,7 @@ void FGNavRadio::audioNavidChanged() try { string trans_ident(_navaid->get_trans_ident()); - SGSoundSample* sound = morse.make_ident(trans_ident, LO_FREQUENCY); + SGSoundSample* sound = FGMorse::instance()->make_ident(trans_ident, LO_FREQUENCY); sound->set_volume( 0.3 ); if (!_sgr->add( sound, nav_fx_name )) { SG_LOG(SG_COCKPIT, SG_WARN, "Failed to add v1-vor-ident sound"); @@ -1024,7 +1023,7 @@ void FGNavRadio::audioNavidChanged() _sgr->remove( dme_fx_name ); } - sound = morse.make_ident( trans_ident, HI_FREQUENCY ); + sound = FGMorse::instance()->make_ident( trans_ident, HI_FREQUENCY ); sound->set_volume( 0.3 ); _sgr->add( sound, dme_fx_name ); diff --git a/src/Instrumentation/navradio.hxx b/src/Instrumentation/navradio.hxx index 96621b6d0..4b2e16918 100644 --- a/src/Instrumentation/navradio.hxx +++ b/src/Instrumentation/navradio.hxx @@ -26,7 +26,6 @@ #include
-#include "Sound/morse.hxx" #include #include @@ -41,8 +40,6 @@ typedef SGSharedPtr FGNavRecordPtr; class FGNavRadio : public SGSubsystem, public SGPropertyChangeListener { - FGMorse morse; - SGInterpTable *term_tbl; SGInterpTable *low_tbl; SGInterpTable *high_tbl; diff --git a/src/Sound/CMakeLists.txt b/src/Sound/CMakeLists.txt index 29a1d4854..28cc5bf8c 100644 --- a/src/Sound/CMakeLists.txt +++ b/src/Sound/CMakeLists.txt @@ -1,6 +1,7 @@ include(FlightGearComponent) set(SOURCES + soundgenerator.cxx beacon.cxx fg_fx.cxx morse.cxx @@ -10,6 +11,7 @@ set(SOURCES ) set(HEADERS + soundgenerator.hxx beacon.hxx fg_fx.hxx morse.hxx @@ -18,4 +20,4 @@ set(HEADERS voiceplayer.hxx ) -flightgear_component(Sound "${SOURCES}" "${HEADERS}") \ No newline at end of file +flightgear_component(Sound "${SOURCES}" "${HEADERS}") diff --git a/src/Sound/Makefile.am b/src/Sound/Makefile.am index e6799c233..893b776d2 100644 --- a/src/Sound/Makefile.am +++ b/src/Sound/Makefile.am @@ -1,6 +1,7 @@ noinst_LIBRARIES = libSound.a libSound_a_SOURCES = \ + soundgenerator.cxx soundgenerator.hxx \ beacon.cxx beacon.hxx \ fg_fx.cxx fg_fx.hxx \ morse.cxx morse.hxx \ diff --git a/src/Sound/beacon.cxx b/src/Sound/beacon.cxx index de9a98915..9d7a1a184 100644 --- a/src/Sound/beacon.cxx +++ b/src/Sound/beacon.cxx @@ -18,8 +18,6 @@ // along with this program; if not, write to the Free Software // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. // -// $Id$ - #include #include @@ -104,3 +102,14 @@ bool FGBeacon::init() { return true; } + +FGBeacon * FGBeacon::_instance = NULL; + +FGBeacon * FGBeacon::instance() +{ + if( _instance == NULL ) { + _instance = new FGBeacon(); + _instance->init(); + } + return _instance; +} diff --git a/src/Sound/beacon.hxx b/src/Sound/beacon.hxx index d0c37d697..c60f48225 100644 --- a/src/Sound/beacon.hxx +++ b/src/Sound/beacon.hxx @@ -20,24 +20,17 @@ // along with this program; if not, write to the Free Software // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. // -// $Id$ - #ifndef _BEACON_HXX #define _BEACON_HXX -#ifdef HAVE_CONFIG_H -# include -#endif +#include "soundgenerator.hxx" #include #include #include #include -#include "morse.hxx" - - // Quoting from http://www.smartregs.com/data/sa326.htm // Smart REGS Glossary - marker beacon // @@ -77,35 +70,35 @@ // (See instrument landing system) (Refer to AIM.) -static const int INNER_FREQ = 3000; -static const int MIDDLE_FREQ = 1300; -static const int OUTER_FREQ = 400; - -static const int INNER_SIZE = BYTES_PER_SECOND; -static const int MIDDLE_SIZE = (int)(BYTES_PER_SECOND * 60 / 95 ); -static const int OUTER_SIZE = BYTES_PER_SECOND; - -static const int INNER_DIT_LEN = (int)(BYTES_PER_SECOND / 6.0); -static const int MIDDLE_DIT_LEN = (int)(MIDDLE_SIZE / 3.0); -static const int MIDDLE_DAH_LEN = (int)(MIDDLE_SIZE * 2 / 3.0); -static const int OUTER_DAH_LEN = (int)(BYTES_PER_SECOND / 2.0); - // manages everything we need to know for an individual sound sample -class FGBeacon { +class FGBeacon : public FGSoundGenerator { private: + static const int INNER_FREQ = 3000; + static const int MIDDLE_FREQ = 1300; + static const int OUTER_FREQ = 400; + + static const int INNER_SIZE = BYTES_PER_SECOND; + static const int MIDDLE_SIZE = (int)(BYTES_PER_SECOND * 60 / 95 ); + static const int OUTER_SIZE = BYTES_PER_SECOND; + + static const int INNER_DIT_LEN = (int)(BYTES_PER_SECOND / 6.0); + static const int MIDDLE_DIT_LEN = (int)(MIDDLE_SIZE / 3.0); + static const int MIDDLE_DAH_LEN = (int)(MIDDLE_SIZE * 2 / 3.0); + static const int OUTER_DAH_LEN = (int)(BYTES_PER_SECOND / 2.0); SGSharedPtr inner; SGSharedPtr middle; SGSharedPtr outer; + // allocate and initialize sound samples + bool init(); + static FGBeacon * _instance; public: FGBeacon(); ~FGBeacon(); - - // allocate and initialize sound samples - bool init(); + static FGBeacon * instance(); SGSoundSample *get_inner() { return inner; } SGSoundSample *get_middle() { return middle; } diff --git a/src/Sound/morse.cxx b/src/Sound/morse.cxx index 7ae7cac98..9ff603322 100644 --- a/src/Sound/morse.cxx +++ b/src/Sound/morse.cxx @@ -18,7 +18,6 @@ // along with this program; if not, write to the Free Software // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. // -// $Id$ #include @@ -79,44 +78,6 @@ FGMorse::~FGMorse() { } -// Make a tone of specified freq and total_len with trans_len ramp in -// and out and only the first len bytes with sound, the rest with -// silence -void make_tone( unsigned char *buf, int freq, - int len, int total_len, int trans_len ) -{ - int i, j; - - for ( i = 0; i < trans_len; ++i ) { - float level = ( sin( (double) i * SGD_2PI / (BYTES_PER_SECOND / freq) ) ) - * ((double)i / trans_len) / 2.0 + 0.5; - - /* Convert to unsigned byte */ - buf[ i ] = (unsigned char) ( level * 255.0 ) ; - } - - for ( i = trans_len; i < len - trans_len; ++i ) { - float level = ( sin( (double) i * SGD_2PI / (BYTES_PER_SECOND / freq) ) ) - / 2.0 + 0.5; - - /* Convert to unsigned byte */ - buf[ i ] = (unsigned char) ( level * 255.0 ) ; - } - j = trans_len; - for ( i = len - trans_len; i < len; ++i ) { - float level = ( sin( (double) i * SGD_2PI / (BYTES_PER_SECOND / freq) ) ) - * ((double)j / trans_len) / 2.0 + 0.5; - --j; - - /* Convert to unsigned byte */ - buf[ i ] = (unsigned char) ( level * 255.0 ) ; - } - for ( i = len; i < total_len; ++i ) { - buf[ i ] = (unsigned char) ( 0.5 * 255.0 ) ; - } -} - - // allocate and initialize sound samples bool FGMorse::init() { // Make Low DIT @@ -270,3 +231,14 @@ SGSoundSample *FGMorse::make_ident( const string& id, const int freq ) { return sample; } + +FGMorse * FGMorse::_instance = NULL; + +FGMorse * FGMorse::instance() +{ + if( _instance == NULL ) { + _instance = new FGMorse(); + _instance->init(); + } + return _instance; +} diff --git a/src/Sound/morse.hxx b/src/Sound/morse.hxx index c6ed5a43b..3d8b9dfbb 100644 --- a/src/Sound/morse.hxx +++ b/src/Sound/morse.hxx @@ -18,16 +18,12 @@ // along with this program; if not, write to the Free Software // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. // -// $Id$ #ifndef _MORSE_HXX #define _MORSE_HXX -#ifdef HAVE_CONFIG_H -# include -#endif - +#include "soundgenerator.hxx" #include #include @@ -97,7 +93,7 @@ static const int LO_FREQUENCY = 1020; // AIM 1-1-7 (f) specified in Hz static const int HI_FREQUENCY = 1350; // AIM 1-1-7 (f) specified in Hz // manages everything we need to know for an individual sound sample -class FGMorse { +class FGMorse : public FGSoundGenerator { private: @@ -110,15 +106,18 @@ private: unsigned char cust_dit[ DIT_SIZE ] ; unsigned char cust_dah[ DAH_SIZE ] ; + static FGMorse * _instance; + bool cust_init( const int freq ); + // allocate and initialize sound samples + bool init(); public: FGMorse(); ~FGMorse(); - // allocate and initialize sound samples - bool init(); + static FGMorse * instance(); // make a SimpleSound morse code transmission for the specified string SGSoundSample *make_ident( const string& id, @@ -126,20 +125,6 @@ public: }; -/** - * \relates FGMorse - * Make a tone of specified freq and total_len with trans_len ramp in - * and out and only the first len bytes with sound, the rest with - * silence. - * @param buf unsigned char pointer to sound buffer - * @param freq desired frequency of tone - * @param len length of tone within sound - * @param total_len total length of sound (anything more than len is padded - * with silence. - * @param trans_len length of ramp up and ramp down to avoid audio "pop" - */ -void make_tone( unsigned char *buf, int freq, - int len, int total_len, int trans_len ); #endif // _MORSE_HXX diff --git a/src/Sound/soundgenerator.cxx b/src/Sound/soundgenerator.cxx new file mode 100644 index 000000000..0f5743ee2 --- /dev/null +++ b/src/Sound/soundgenerator.cxx @@ -0,0 +1,64 @@ +// soundgenerator.cxx -- simple sound generation +// +// Written by Curtis Olson, started March 2001. +// +// Copyright (C) 2001 Curtis L. Olson - http://www.flightgear.org/~curt +// +// 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; if not, write to the Free Software +// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +// + +#include "soundgenerator.hxx" +#include + +FGSoundGenerator::~FGSoundGenerator() +{ +} + +// Make a tone of specified freq and total_len with trans_len ramp in +// and out and only the first len bytes with sound, the rest with +// silence +void FGSoundGenerator::make_tone( unsigned char *buf, int freq, + int len, int total_len, int trans_len ) +{ + int i, j; + + for ( i = 0; i < trans_len; ++i ) { + float level = ( sin( (double) i * SGD_2PI / (BYTES_PER_SECOND / freq) ) ) + * ((double)i / trans_len) / 2.0 + 0.5; + + /* Convert to unsigned byte */ + buf[ i ] = (unsigned char) ( level * 255.0 ) ; + } + + for ( i = trans_len; i < len - trans_len; ++i ) { + float level = ( sin( (double) i * SGD_2PI / (BYTES_PER_SECOND / freq) ) ) + / 2.0 + 0.5; + + /* Convert to unsigned byte */ + buf[ i ] = (unsigned char) ( level * 255.0 ) ; + } + j = trans_len; + for ( i = len - trans_len; i < len; ++i ) { + float level = ( sin( (double) i * SGD_2PI / (BYTES_PER_SECOND / freq) ) ) + * ((double)j / trans_len) / 2.0 + 0.5; + --j; + + /* Convert to unsigned byte */ + buf[ i ] = (unsigned char) ( level * 255.0 ) ; + } + for ( i = len; i < total_len; ++i ) { + buf[ i ] = (unsigned char) ( 0.5 * 255.0 ) ; + } +} diff --git a/src/Sound/soundgenerator.hxx b/src/Sound/soundgenerator.hxx new file mode 100644 index 000000000..fa600f347 --- /dev/null +++ b/src/Sound/soundgenerator.hxx @@ -0,0 +1,67 @@ +// soundgenerator.hxx -- simple sound generation +// +// Written by Curtis Olson, started March 2001. +// +// Copyright (C) 2001 Curtis L. Olson - http://www.flightgear.org/~curt +// +// 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; if not, write to the Free Software +// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +// + + +#ifndef _FGSOUNDGENERATOR_HXX +#define _FGSOUNDGENERATOR_HXX + +#ifdef HAVE_CONFIG_H +# include +#endif + +class FGSoundGenerator { + +public: + static const int BYTES_PER_SECOND = 22050; + // static const int BEAT_LENGTH = 240; // milleseconds (5 wpm) + static const int BEAT_LENGTH = 92; // milleseconds (13 wpm) + static const int TRANSITION_BYTES = (int)(0.005 * BYTES_PER_SECOND); + static const int COUNT_SIZE = BYTES_PER_SECOND * BEAT_LENGTH / 1000; + static const int DIT_SIZE = 2 * COUNT_SIZE; // 2 counts + static const int DAH_SIZE = 4 * COUNT_SIZE; // 4 counts + static const int SPACE_SIZE = 3 * COUNT_SIZE; // 3 counts + static const int LO_FREQUENCY = 1020; // AIM 1-1-7 (f) specified in Hz + static const int HI_FREQUENCY = 1350; // AIM 1-1-7 (f) specified in Hz + +protected: + /** + * \relates FGMorse + * Make a tone of specified freq and total_len with trans_len ramp in + * and out and only the first len bytes with sound, the rest with + * silence. + * @param buf unsigned char pointer to sound buffer + * @param freq desired frequency of tone + * @param len length of tone within sound + * @param total_len total length of sound (anything more than len is padded + * with silence. + * @param trans_len length of ramp up and ramp down to avoid audio "pop" + */ + static void make_tone( unsigned char *buf, int freq, + int len, int total_len, int trans_len ); + +public: + + virtual ~FGSoundGenerator(); +}; + + + +#endif // _FGSOUNDGENERATOR_HXX