YARP
Yet Another Robot Platform
 
Loading...
Searching...
No Matches
FakeMicrophone.cpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2006-2021 Istituto Italiano di Tecnologia (IIT)
3 * SPDX-License-Identifier: BSD-3-Clause
4 */
5
6#include "FakeMicrophone.h"
7
8#include <yarp/os/Thread.h>
9#include <yarp/os/Time.h>
10#include <yarp/os/Semaphore.h>
11#include <yarp/os/Stamp.h>
13#include <yarp/os/LogStream.h>
14
15#include <mutex>
16#define _USE_MATH_DEFINES
17#include <math.h>
18#include <string>
19
20
21using namespace yarp::os;
22using namespace yarp::dev;
23using namespace yarp::sig;
24
25constexpr double c_DEFAULT_PERIOD=0.01; //s
26
27namespace {
28YARP_LOG_COMPONENT(FAKEMICROPHONE, "yarp.device.fakeMicrophone")
29}
30
31typedef unsigned short int audio_sample_16t;
32
37
42
44{
45 if (!this->parseParams(config)) {return false;}
46
47 std::string debug_cfg_string = config.toString();
48 yarp::os::Bottle& bb = config.findGroup("AUDIO_BASE");
49 bool b = configureRecorderAudioDevice(bb, "fakeMicrophone");
50 if (!b) { return false; }
51
53
54 if (m_waveform == "sine") { m_waveform_enum = waveform_t::sine; }
55 else if (m_waveform == "sawtooth") { m_waveform_enum = waveform_t::sawtooth; }
56 else if (m_waveform == "square") { m_waveform_enum = waveform_t::square; }
57 else if (m_waveform == "constant") { m_waveform_enum = waveform_t::constant; }
58 else { yError() << "Unsupported value for waveform parameter"; return false; }
59
60 if (m_waveform_enum == waveform_t::sine) { yCInfo(FAKEMICROPHONE) << "Using sine waveform, signal amplitude=" << m_signal_amplitude << ", signal frequency=" << m_signal_frequency; }
61 else if (m_waveform_enum == waveform_t::sawtooth) { yCInfo(FAKEMICROPHONE) << "Using sawtooth waveform, signal amplitude=" << m_signal_amplitude << ", signal frequency=" << m_signal_frequency; }
62 else if (m_waveform_enum == waveform_t::square) { yCInfo(FAKEMICROPHONE) << "Using square waveform, signal amplitude=" << m_signal_amplitude << ", signal frequency=" << m_signal_frequency; }
63 else if (m_waveform_enum == waveform_t::constant) { yCInfo(FAKEMICROPHONE) << "Using constant waveform, signal amplitude="<< m_signal_amplitude << ", signal frequency=" << m_signal_frequency; }
64
65 //data structure initialization
66 //m_audiorecorder_cfg.numSamples = tmp_freq * 5; //5sec
67 //const size_t EXTRA_SPACE = 2;
68 //AudioBufferSize buffer_size(m_audiorecorder_cfg.numSamples*EXTRA_SPACE, m_audiorecorder_cfg.numChannels, m_audiorecorder_cfg.bytesPerSample);
69 //m_inputBuffer = new yarp::dev::CircularAudioBuffer_16t("fake_mic_buffer", buffer_size);
70
71 m_max_count.resize(m_audiorecorder_cfg.numChannels);
72 m_counter.resize(m_audiorecorder_cfg.numChannels);
73
74 //start the capture thread
75 start();
76 return true;
77}
78
80{
82
83 //wait until the thread is stopped...
84
85 return true;
86}
87
89{
90 std::lock_guard<std::mutex> lock(m_mutex);
91 if (gain > 0)
92 {
93 m_hw_gain = gain;
94 return ReturnValue_ok;
95 }
96 return ReturnValue::return_code::return_value_error_method_failed;
97}
98
99bool FakeMicrophone::threadInit()
100{
101 return true;
102}
103
104
105void FakeMicrophone::run()
106{
107 // when not recording, do nothing
109 {
110 return;
111 }
112
113 //fill the buffer with a generated tone.
114 //each iteration, which occurs every xxx ms (thread period), I copy a fixed amount of
115 //samples (m_driver_frame_size) in the buffer. This operation cannot be interrupted by stopping the device
116 //with m_recording_enabled=false.
117 for (size_t i = 0; i < m_driver_frame_size; i++)
118 {
119 // Default values:
120 // this signal has amplitude (-32000,32000)
121 // the first channel has frequency 440Hz (A4 note)
122 // the second channel has frequency 220Hz etc.
123 // and so on..
124 if (m_waveform_enum == waveform_t::sine)
125 {
126 for (size_t i = 0; i < m_audiorecorder_cfg.numChannels; i++)
127 {
128 m_max_count[i] = double(m_audiorecorder_cfg.frequency) / m_signal_frequency / double(i + 1);
129 double elem1 = double(m_signal_amplitude * sin(double(m_counter[i]) / m_max_count[i] * 2 * M_PI));
131 m_counter[i]++;
132 if (m_counter[i] >= m_max_count[i]) {
133 m_counter[i] = 0;
134 }
135 }
136 }
137 else if(m_waveform_enum == waveform_t::sawtooth)
138 {
139 for (size_t i = 0; i < m_audiorecorder_cfg.numChannels; i++)
140 {
141 m_max_count[i] = double(m_audiorecorder_cfg.frequency) / m_signal_frequency / double(i + 1);
142 double elem1 = m_signal_amplitude * 2.0 * (double(m_counter[i])/ m_max_count[i]) - m_signal_amplitude;
144 m_counter[i]++;
145 if (m_counter[i] >= m_max_count[i]) {
146 m_counter[i] = 0;
147 }
148 }
149 }
150 else if (m_waveform_enum == waveform_t::square)
151 {
152 for (size_t i = 0; i < m_audiorecorder_cfg.numChannels; i++)
153 {
154 m_max_count[i] = double(m_audiorecorder_cfg.frequency) / m_signal_frequency / double(i + 1);
155 double elem1 = m_counter[i] < m_max_count[i]/2 ? m_signal_amplitude : 0;
157 m_counter[i]++;
158 if (m_counter[i] >= m_max_count[i]) {
159 m_counter[i] = 0;
160 }
161 }
162 }
163 else if (m_waveform_enum == waveform_t::constant)
164 {
165 for (size_t i = 0; i < m_audiorecorder_cfg.numChannels; i++)
166 {
168 m_counter[i]++;
169 if (m_counter[i] >= m_max_count[i]) {
170 m_counter[i] = 0;
171 }
172 }
173 }
174 else
175 {
176 yCInfo(FAKEMICROPHONE) << "Not implemented/unreachable code";
177 }
178 }
179}
#define M_PI
constexpr double c_DEFAULT_PERIOD
unsigned short int audio_sample_16t
#define yError(...)
Definition Log.h:361
#define ReturnValue_ok
Definition ReturnValue.h:77
bool parseParams(const yarp::os::Searchable &config) override
Parse the DeviceDriver parameters.
yarp::dev::ReturnValue setHWGain(double gain) override
Sets the hardware gain of the grabbing device (if supported by the hardware)
virtual ~FakeMicrophone() override
bool open(yarp::os::Searchable &config) override
Open the DeviceDriver.
bool close() override
Close the DeviceDriver.
yarp::dev::CircularAudioBuffer_16t * m_inputBuffer
bool configureRecorderAudioDevice(yarp::os::Searchable &config, std::string device_name)
AudioDeviceDriverSettings m_audiorecorder_cfg
A simple collection of objects that can be described and transmitted in a portable way.
Definition Bottle.h:64
A mini-server for performing network communication in the background.
An abstraction for a periodic thread.
bool setPeriod(double period)
Set the (new) period of the thread.
bool start()
Call this to start the thread.
void stop()
Call this to stop the thread, this call blocks until the thread is terminated (and releaseThread() ca...
A base class for nested structures that can be searched.
Definition Searchable.h:31
virtual std::string toString() const =0
Return a standard text representation of the content of the object.
virtual Bottle & findGroup(const std::string &key) const =0
Gets a list corresponding to a given keyword.
#define yCInfo(component,...)
#define YARP_LOG_COMPONENT(name,...)
For streams capable of holding different kinds of content, check what they actually have.
An interface to the operating system, including Port based communication.