NETSIM  1.0.0
Hardware-in-the-loopacousticnetworksimulator
processor.h
Go to the documentation of this file.
1 // Copyright 2020:
2 // GobySoft, LLC (2017-)
3 // Massachusetts Institute of Technology (2017-)
4 // File authors:
5 // Toby Schneider <toby@gobysoft.org>
6 //
7 //
8 // This file is part of the NETSIM Libraries.
9 //
10 // The NETSIM Libraries are free software: you can redistribute them and/or modify
11 // them under the terms of the GNU Lesser General Public License as published by
12 // the Free Software Foundation, either version 2.1 of the License, or
13 // (at your option) any later version.
14 //
15 // The NETSIM Libraries are distributed in the hope that they will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public License
21 // along with NETSIM. If not, see <http://www.gnu.org/licenses/>.
22 
23 #ifndef PROCESSOR20170816H
24 #define PROCESSOR20170816H
25 
26 #include "goby/middleware/marshalling/protobuf.h"
27 
28 #include "goby/zeromq/application/multi_thread.h"
29 
30 #include "netsim/core/common.h"
31 #include "netsim/messages/core_config.pb.h"
32 #include "netsim/messages/groups.h"
33 #include <jack/types.h>
34 
35 using ThreadBase = goby::middleware::SimpleThread<netsim::protobuf::NetSimCoreConfig>;
36 
37 namespace netsim
38 {
39 extern std::atomic<int> processor_ready;
40 
41 template <int to_index> class ProcessorThreadBase : public ThreadBase
42 {
43  public:
44  ProcessorThreadBase(const netsim::protobuf::NetSimCoreConfig& config,
45  boost::units::quantity<boost::units::si::frequency> loop_freq)
46  : ThreadBase(config, loop_freq)
47  {
48  // update buffer (audio block) size
49  interthread().template subscribe<netsim::groups::buffer_size_change, jack_nframes_t>(
50  [this](const jack_nframes_t& buffer_size) { this->update_buffer_size(buffer_size); });
51 
52  // subscribe to all the detectors except our own id, since we ignore our transmissions
53  for (int i = 0, n = cfg().number_of_modems(); i < n; ++i)
54  {
55  audio_out_groups_.push_back(goby::middleware::DynamicGroup("aout", i + to_index));
56 
57  // don't subscribe to our own audio
58  if (i != to_index)
59  {
60  auto detector_audio_callback =
61  [this, i](std::shared_ptr<const netsim::TaggedAudioBuffer> buffer) {
62  this->detector_audio(buffer, i);
63  };
64  switch (i)
65  {
66 #define NETSIM_PROCESSOR_SUBSCRIBE_DETECTOR_AUDIO(z, n, _) \
67  case n: \
68  interthread() \
69  .template subscribe<netsim::groups::DetectorAudio<n>::group, \
70  netsim::TaggedAudioBuffer>(detector_audio_callback); \
71  break;
73  nil)
74  }
75  }
76  }
77  }
78 
79  protected:
80  virtual void detector_audio(std::shared_ptr<const netsim::TaggedAudioBuffer> buffer,
81  int modem_index) = 0;
82 
83  virtual void update_buffer_size(const jack_nframes_t& buffer_size) = 0;
84 
85  void publish_audio_buffer(std::shared_ptr<const netsim::TaggedAudioBuffer> buffer,
86  int tx_modem_id)
87  {
88  switch (tx_modem_id)
89  {
90 #define NETSIM_PROCESSOR_PUBLISH_AUDIO_BUFFER(z, n, _) \
91  case n: \
92  this->interthread().template publish<netsim::groups::AudioOut<n, to_index>::group>( \
93  buffer); \
94  break;
96  }
97  }
98 
99  private:
100  // indexed on tx modem id
101  std::vector<goby::middleware::DynamicGroup> audio_out_groups_;
102 };
103 } // namespace netsim
104 
105 #endif
netsim
Definition: environment.h:30
netsim::ProcessorThreadBase
Definition: processor.h:41
NETSIM_PROCESSOR_PUBLISH_AUDIO_BUFFER
#define NETSIM_PROCESSOR_PUBLISH_AUDIO_BUFFER(z, n, _)
netsim::ProcessorThreadBase::ProcessorThreadBase
ProcessorThreadBase(const netsim::protobuf::NetSimCoreConfig &config, boost::units::quantity< boost::units::si::frequency > loop_freq)
Definition: processor.h:44
netsim::ProcessorThreadBase::detector_audio
virtual void detector_audio(std::shared_ptr< const netsim::TaggedAudioBuffer > buffer, int modem_index)=0
NETSIM_PROCESSOR_SUBSCRIBE_DETECTOR_AUDIO
#define NETSIM_PROCESSOR_SUBSCRIBE_DETECTOR_AUDIO(z, n, _)
common.h
netsim::ProcessorThreadBase::publish_audio_buffer
void publish_audio_buffer(std::shared_ptr< const netsim::TaggedAudioBuffer > buffer, int tx_modem_id)
Definition: processor.h:85
netsim::processor_ready
std::atomic< int > processor_ready
groups.h
ThreadBase
goby::middleware::SimpleThread< netsim::protobuf::NetSimCoreConfig > ThreadBase
Definition: processor.h:35
NETSIM_MAX_MODEMS
#define NETSIM_MAX_MODEMS
Definition: common.h:34
netsim::ProcessorThreadBase::update_buffer_size
virtual void update_buffer_size(const jack_nframes_t &buffer_size)=0