YARP
Yet Another Robot Platform
 
Loading...
Searching...
No Matches
AudioFromFileDevice.cpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2023-2023 Istituto Italiano di Tecnologia (IIT)
3 * SPDX-License-Identifier: BSD-3-Clause
4 */
5
7
8#include <yarp/os/Thread.h>
9#include <yarp/os/Time.h>
10#include <yarp/os/Stamp.h>
12#include <yarp/os/LogStream.h>
13
14#include <mutex>
15#include <string>
16
17
18using namespace yarp::os;
19using namespace yarp::dev;
20using namespace yarp::sig;
21
22constexpr double c_DEFAULT_PERIOD=0.01; //s
23
24namespace {
25YARP_LOG_COMPONENT(AUDIOFROMFILE, "yarp.device.audioFromFileDevice")
26}
27
28typedef unsigned short int audio_sample_16t;
29
34
38
40{
41 yCInfo(AUDIOFROMFILE) << "Not yet implemented";
42 return ReturnValue::return_code::return_value_error_not_implemented_by_device;
43}
44
46{
47 bool b = false;
48 b = parseParams(config);
49 if (!b) { return false; }
50
51 b = configureRecorderAudioDevice(config.findGroup("AUDIO_BASE"), "audioFromFileDevice");
52 if (!b) { return false; }
53
54 //sets the thread period
56
57 //opens the file
58 bool ret = yarp::sig::file::read(m_audioFile, m_file_name.c_str());
59 if (ret == false)
60 {
61 yCError(AUDIOFROMFILE) << "Unable to open file" << m_file_name.c_str();
62 return false;
63 }
64 yCInfo(AUDIOFROMFILE) << "Loaded file has the following properties: samples:" << m_audioFile.getSamples() << " channels:"<< m_audioFile.getChannels() << " bytes per samples:" << m_audioFile.getBytesPerSample();
65
67 {
68 this->m_audiorecorder_cfg.numChannels = m_audioFile.getChannels();
69 this->m_audiorecorder_cfg.frequency = m_audioFile.getFrequency();
71 this->m_audiorecorder_cfg.numSamples = m_audioFile.getSamples();
72 }
73 else
74 {
75 if (m_audioFile.getChannels() != this->m_audiorecorder_cfg.numChannels)
76 {
77 yCInfo(AUDIOFROMFILE) << "Number of channels mismatch!";
78 return false;
79 }
80 yCError(AUDIOFROMFILE) << "Not Yet implemented! I should replace this fatal error with a configuration conversion here!";
81 return false;
82 }
83
84
85 //sets the audio configuration equal to the audio file
86 //constexpr size_t c_EXTRA_SPACE = 2;
87 //AudioBufferSize buffer_size(m_audiorecorder_cfg.numSamples* c_EXTRA_SPACE, m_audiorecorder_cfg.numChannels, m_audiorecorder_cfg.bytesPerSample);
88 //m_inputBuffer = new yarp::dev::CircularAudioBuffer_16t("fake_mic_buffer", buffer_size);
89
90 m_datap = m_audioFile.getInterleavedAudioRawData();
91
92 //start the capture thread
93 start();
94 return true;
95}
96
98{
100 return true;
101}
102
104{
106 if (m_reset_on_stop)
107 {
108 m_bpnt=0;
109 }
110 return b;
111}
112
113bool AudioFromFileDevice::threadInit()
114{
115 return true;
116}
117
118
119void AudioFromFileDevice::run()
120{
121 // when not recording, do nothing
123 {
124 return;
125 }
126
127 // Just acquire raw data and put them in the buffer
128 //each iteration, which occurs every xxx ms (thread period), I copy a fixed amount of
129 //samples (m_driver_frame_size) in the buffer. This operation cannot be interrupted by stopping the device
130 //with m_recording_enabled=false. When the pointer reaches the end of the sound (audioFile),
131 //just restart from the beginning in an endless loop
132 size_t chan_num = m_audioFile.getChannels();
133 size_t fsize_in_samples = m_audioFile.getSamples();
134 for (size_t i = 0; i < m_driver_frame_size; i++)
135 {
136 if (m_bpnt >= fsize_in_samples)
137 {
138 m_bpnt = 0;
139 }
140 for (size_t c=0; c< chan_num; c++)
141 {
142 m_inputBuffer->write((unsigned short)(m_datap.at(m_bpnt* chan_num +c).get()));
143 }
144 m_bpnt++;
145 }
146
148 {
149 yCDebug(AUDIOFROMFILE) << "b_pnt" << m_bpnt << "/" << fsize_in_samples << " samples";
150 }
151}
constexpr double c_DEFAULT_PERIOD
unsigned short int audio_sample_16t
bool ret
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)
bool open(yarp::os::Searchable &config) override
Open the DeviceDriver.
bool close() override
Close the DeviceDriver.
yarp::dev::ReturnValue stopRecording() override
Stop the recording.
yarp::dev::CircularAudioBuffer_16t * m_inputBuffer
virtual yarp::dev::ReturnValue stopRecording() override
Stop the recording.
bool configureRecorderAudioDevice(yarp::os::Searchable &config, std::string device_name)
AudioDeviceDriverSettings m_audiorecorder_cfg
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 Bottle & findGroup(const std::string &key) const =0
Gets a list corresponding to a given keyword.
size_t getBytesPerSample() const
Get the number of bytes per sample.
Definition Sound.cpp:593
std::vector< std::reference_wrapper< audio_sample > > getInterleavedAudioRawData() const
Returns a serialized version of the sound, in interleaved format, e.g.
Definition Sound.cpp:539
size_t getChannels() const
Get the number of channels of the sound.
Definition Sound.cpp:603
int getFrequency() const
Get the frequency of the sound (i.e.
Definition Sound.cpp:356
size_t getSamples() const
Get the number of samples contained in the sound.
Definition Sound.cpp:598
#define yCInfo(component,...)
#define yCError(component,...)
#define yCDebug(component,...)
#define YARP_LOG_COMPONENT(name,...)
For streams capable of holding different kinds of content, check what they actually have.
Definition jointData.cpp:13
An interface to the operating system, including Port based communication.
bool read(ImageOf< PixelRgb > &dest, const std::string &src, image_fileformat format=FORMAT_ANY)