YARP
Yet Another Robot Platform
 
Loading...
Searching...
No Matches
SoundUtils.cpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2026-2026 Istituto Italiano di Tecnologia (IIT)
3 * SPDX-License-Identifier: BSD-3-Clause
4 */
5
7#include <cstring>
8#include <cmath>
9#include <algorithm>
10#include <yarp/os/Log.h>
11#include <yarp/os/LogStream.h>
12
13using namespace yarp::sig;
14
15#ifndef M_PI
16#define M_PI 3.14159265358979323846
17#endif
18
19bool prepareSound(Sound& outSound,double duration_s, size_t channels, size_t sampleRate, double frequency, size_t amplitude)
20{
21 if (duration_s <= 0 || channels == 0 || sampleRate == 0 || frequency == 0 || amplitude == 0)
22 {
23 yError() << "yarp::sig::utils::makeTone: invalid parameters";
24 return false;
25 }
26
27 // Sound parameters
28 size_t totalSamples = sampleRate * duration_s;
29
30 // Create the sound object
31 outSound = yarp::sig::Sound();
32 outSound.resize(totalSamples, channels);
33 outSound.setFrequency(sampleRate);
34
35 return true;
36}
37
38bool utils::makeSawTooth(Sound& outSound,double duration_s, size_t channels, size_t sampleRate, double frequency, size_t amplitude)
39{
40 if (!prepareSound(outSound, duration_s, channels, sampleRate, frequency, amplitude)) { return false; }
41
42 size_t totalSamples = outSound.getSamples();
43
44 for (size_t i = 0; i < totalSamples; i++)
45 {
46 double t = static_cast<double>(i) / sampleRate;
47 double value = 2.0 * (t * frequency - floor(t * frequency + 0.5));
48 short sample = static_cast<short>(amplitude * value);
49 for (size_t ch = 0; ch < channels; ch++)
50 {
51 outSound.set(sample, i, ch);
52 }
53 }
54
55 return true;
56}
57
58bool utils::makeSquareWave(Sound& outSound,double duration_s, size_t channels, size_t sampleRate, double frequency, size_t amplitude)
59{
60 if (!prepareSound(outSound, duration_s, channels, sampleRate, frequency, amplitude)) { return false; }
61
62 double twoPiF = 2.0 * M_PI * frequency;
63
64 for (size_t i = 0; i < outSound.getSamples(); i++)
65 {
66 double t = static_cast<double>(i) / sampleRate;
67
68 double s = sin(twoPiF * t);
69 short sample = (s >= 0) ? amplitude : -amplitude;
70
71 for (size_t ch = 0; ch < channels; ch++)
72 {
73 outSound.set(sample, i, ch);
74 }
75 }
76
77 return true;
78}
79
80bool utils::makeTone(Sound& outSound,double duration_s, size_t channels, size_t sampleRate, double frequency, size_t amplitude)
81{
82 if (!prepareSound(outSound, duration_s, channels, sampleRate, frequency, amplitude)) { return false; }
83
84 // Fill the sound buffer with a sine wave
85 double twoPiF = 2.0 * M_PI * frequency;
86 for (size_t i = 0; i < outSound.getSamples(); i++)
87 {
88 double t = static_cast<double>(i) / sampleRate;
89 short sample = static_cast<short>((double)(amplitude) * sin(twoPiF * t));
90 for (size_t ch = 0; ch < channels; ch++)
91 {
92 outSound.set(sample, i, ch);
93 }
94 }
95
96 return true;
97}
98
99Sound utils::mix(const Sound& A, const Sound& B, double percentage)
100{
101 Sound result;
102
103 // Ensure percentage is clamped between 0 and 1
104 if (percentage < 0.0) percentage = 0.0;
105 if (percentage > 1.0) percentage = 1.0;
106
107 // Check for matching sample rates and number of channels
108 if (A.getFrequency() != B.getFrequency() || A.getChannels() != B.getChannels())
109 {
110 yError() << "Cannot mix sounds with different sample rates or channel counts.";
111 return result;
112 }
113
114 // Determine the length of the resulting sound
115 size_t maxSamples = std::max(A.getSamples(), B.getSamples());
116 size_t channels = A.getChannels();
117 double alpha = percentage;
118 double beta = 1.0 - percentage;
119
120 // Create the resulting sound
121 result.resize(maxSamples, channels);
122 result.setFrequency(A.getFrequency());
123
124 // Mix samples
125 for (size_t ch = 0; ch < channels; ++ch)
126 {
127 for (size_t i = 0; i < maxSamples; ++i)
128 {
129 double sampleA = (i < A.getSamples()) ? A.getSafe(i, ch) : 0.0;
130 double sampleB = (i < B.getSamples()) ? B.getSafe(i, ch) : 0.0;
131 yarp::sig::Sound::audio_sample sampleO = static_cast<yarp::sig::Sound::audio_sample>(alpha * sampleA + beta * sampleB);
132 result.setSafe(sampleO, i, ch);
133 }
134 }
135
136 return result;
137}
#define yError(...)
Definition Log.h:361
bool prepareSound(Sound &outSound, double duration_s, size_t channels, size_t sampleRate, double frequency, size_t amplitude)
#define M_PI
A mini-server for performing network communication in the background.
Class for storing sounds See Audio in YARP for additional documentation on YARP audio.
Definition Sound.h:42
void setSafe(audio_sample value, size_t sample, size_t channel=0)
Definition Sound.cpp:507
void setFrequency(int freq)
Set the frequency of the sound (i.e.
Definition Sound.cpp:369
size_t getChannels() const
Get the number of channels of the sound.
Definition Sound.cpp:669
void resize(size_t samples, size_t channels=1)
Set the sound size.
Definition Sound.cpp:267
int getFrequency() const
Get the frequency of the sound (i.e.
Definition Sound.cpp:364
void set(audio_sample value, size_t sample, size_t channel=0)
Definition Sound.cpp:342
size_t getSamples() const
Get the number of samples contained in the sound.
Definition Sound.cpp:664
audio_sample getSafe(size_t sample, size_t channel=0) const
Definition Sound.h:110
bool makeSawTooth(Sound &outSound, double duration_s=1.0, size_t channels=1, size_t sampleRate=16000, double frequency=1, size_t amplitude=30000)
Generate a sound containing a sawtooth signal.
Sound mix(const Sound &A, const Sound &B, double percentage)
Mixes two sounds together into a single sound.
bool makeTone(Sound &outSound, double duration_s=1.0, size_t channels=1, size_t sampleRate=16000, double frequency=440, size_t amplitude=30000)
Generate a sound containing a sine wave tone.
bool makeSquareWave(Sound &outSound, double duration_s=1.0, size_t channels=1, size_t sampleRate=16000, double frequency=1, size_t amplitude=30000)
Generate a sound containing a square wave signal.