YARP
Yet Another Robot Platform
 
Loading...
Searching...
No Matches
SoundFilters.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
7
8#include <yarp/os/Log.h>
9#include <yarp/os/LogStream.h>
10
11using namespace yarp::os;
12using namespace yarp::sig;
13using namespace yarp::sig::soundfilters;
14
15namespace
16{
17 YARP_LOG_COMPONENT(SOUNDFILTERS, "yarp.sig.SoundFilters")
18}
19
20#if defined (YARP_HAS_SOXR)
21#include <soxr.h>
22#endif
23
24//#define MULTICHANS_TESTED
25
26//#######################################################################################################
27
29{
30 if (frequency == 0)
31 {
32 yCError(SOUNDFILTERS) << "invalid frequency value = 0";
33 return false;
34 }
35#ifndef MULTICHANS_TESTED
36 if (snd.getChannels() != 1)
37 {
38 yCError(SOUNDFILTERS) << "only 1 channel is currently supported";
39 return false;
40 }
41#endif
42 if (snd.getSamples() == 0)
43 {
44 yCError(SOUNDFILTERS) << "empty sound received?!";
45 return false;
46 }
47 if (static_cast<unsigned>(snd.getFrequency()) == frequency)
48 {
49 yCWarning(SOUNDFILTERS) << "no resampling needed";
50 return true;
51 }
52
53#if !defined (YARP_HAS_SOXR)
54 yCError(SOUNDFILTERS) << "libsoxr not available";
55 return false;
56#else
57 //configuration
59 tit.itype = SOXR_INT16_I;
60 tit.otype = SOXR_INT16_I;
61 tit.scale = 1.0;
62
63 double irate = snd.getFrequency();
64 double orate = double(frequency);
65 size_t ismp = snd.getSamples();
66#ifdef MULTICHANS_TESTED
67 size_t ichans = snd.getChannels();
68#else
69 size_t ichans = 1;
70#endif
71 size_t ilen = ismp*ichans;
73
74 //copy from sound to buffer
75 for (size_t t = 0; t < ismp; t++)
76 {
77 for (size_t c = 0; c < ichans; c++)
78 {
79 arri[t] = snd.getSafe(t, c);
80 }
81 }
82
83 size_t osmp = (size_t)(ilen * orate / irate + .5);
84 size_t ochans = ichans;
85 size_t olen = osmp*ochans;
87
88 //resample
89 size_t odone;
90 soxr_error_t error = soxr_oneshot(irate, orate, ichans, // Rates and # of channels
91 arri, ilen, NULL, // Input
92 arro, olen, &odone, // Output
93 &tit, NULL, NULL); // Configuration
94 if (error != 0)
95 {
96 yCError(SOUNDFILTERS) << "libsoxr resample failed";
97 delete[]arri;
98 delete[]arro;
99 }
100
101 //copy from buffer to sound
102 snd.setFrequency(size_t(orate));
103 snd.resize(olen);
104 for (size_t t = 0; t < osmp; t++)
105 {
106 for (size_t c = 0; c < ochans; c++)
107 {
108 snd.setSafe(arro[t], t, c);
109 }
110 }
111
112 //clear buffer
113 delete[]arri;
114 delete[]arro;
115
116 return true;
117#endif
118}
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:25
void setSafe(audio_sample value, size_t sample, size_t channel=0)
Definition Sound.cpp:448
void setFrequency(int freq)
Set the frequency of the sound (i.e.
Definition Sound.cpp:361
size_t getChannels() const
Get the number of channels of the sound.
Definition Sound.cpp:603
void resize(size_t samples, size_t channels=1)
Set the sound size.
Definition Sound.cpp:270
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
audio_sample getSafe(size_t sample, size_t channel=0) const
Definition Sound.h:88
#define yCError(component,...)
#define yCWarning(component,...)
#define YARP_LOG_COMPONENT(name,...)
An interface to the operating system, including Port based communication.
bool resample(yarp::sig::Sound &snd, size_t frequency)
Resample a sound.