16#define M_PI 3.14159265358979323846
19bool prepareSound(
Sound& outSound,
double duration_s,
size_t channels,
size_t sampleRate,
double frequency,
size_t amplitude)
21 if (duration_s <= 0 || channels == 0 || sampleRate == 0 || frequency == 0 || amplitude == 0)
23 yError() <<
"yarp::sig::utils::makeTone: invalid parameters";
28 size_t totalSamples = sampleRate * duration_s;
32 outSound.
resize(totalSamples, channels);
38bool utils::makeSawTooth(
Sound& outSound,
double duration_s,
size_t channels,
size_t sampleRate,
double frequency,
size_t amplitude)
40 if (!
prepareSound(outSound, duration_s, channels, sampleRate, frequency, amplitude)) {
return false; }
44 for (
size_t i = 0; i < totalSamples; i++)
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++)
51 outSound.
set(sample, i, ch);
60 if (!
prepareSound(outSound, duration_s, channels, sampleRate, frequency, amplitude)) {
return false; }
62 double twoPiF = 2.0 *
M_PI * frequency;
64 for (
size_t i = 0; i < outSound.
getSamples(); i++)
66 double t =
static_cast<double>(i) / sampleRate;
68 double s = sin(twoPiF * t);
69 short sample = (s >= 0) ? amplitude : -amplitude;
71 for (
size_t ch = 0; ch < channels; ch++)
73 outSound.
set(sample, i, ch);
80bool utils::makeTone(
Sound& outSound,
double duration_s,
size_t channels,
size_t sampleRate,
double frequency,
size_t amplitude)
82 if (!
prepareSound(outSound, duration_s, channels, sampleRate, frequency, amplitude)) {
return false; }
85 double twoPiF = 2.0 *
M_PI * frequency;
86 for (
size_t i = 0; i < outSound.
getSamples(); i++)
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++)
92 outSound.
set(sample, i, ch);
104 if (percentage < 0.0) percentage = 0.0;
105 if (percentage > 1.0) percentage = 1.0;
110 yError() <<
"Cannot mix sounds with different sample rates or channel counts.";
117 double alpha = percentage;
118 double beta = 1.0 - percentage;
121 result.
resize(maxSamples, channels);
125 for (
size_t ch = 0; ch < channels; ++ch)
127 for (
size_t i = 0; i < maxSamples; ++i)
132 result.
setSafe(sampleO, i, ch);
bool prepareSound(Sound &outSound, double duration_s, size_t channels, size_t sampleRate, double frequency, size_t amplitude)
A mini-server for performing network communication in the background.
Class for storing sounds See Audio in YARP for additional documentation on YARP audio.
void setSafe(audio_sample value, size_t sample, size_t channel=0)
void setFrequency(int freq)
Set the frequency of the sound (i.e.
size_t getChannels() const
Get the number of channels of the sound.
void resize(size_t samples, size_t channels=1)
Set the sound size.
int getFrequency() const
Get the frequency of the sound (i.e.
void set(audio_sample value, size_t sample, size_t channel=0)
size_t getSamples() const
Get the number of samples contained in the sound.
audio_sample getSafe(size_t sample, size_t channel=0) const
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.