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 snd.clear();
34 return false;
35 }
36#ifndef MULTICHANS_TESTED
37 if (snd.getChannels() != 1)
38 {
39 yCError(SOUNDFILTERS) << "only 1 channel is currently supported";
40 snd.clear();
41 return false;
42 }
43#endif
44 if (snd.getSamples() == 0)
45 {
46 yCError(SOUNDFILTERS) << "empty sound received?!";
47 snd.clear();
48 return false;
49 }
50 if (static_cast<unsigned>(snd.getFrequency()) == frequency)
51 {
52 yCWarning(SOUNDFILTERS) << "no resampling needed";
53 return true;
54 }
55
56#if !defined (YARP_HAS_SOXR)
57 yCError(SOUNDFILTERS) << "libsoxr not available";
58 snd.clear();
59 return false;
60#else
61 //configuration
63 tit.scale = 1.0;
64
65 double irate = snd.getFrequency();
66 double orate = double(frequency);
67 size_t ismp = snd.getSamples();
68#ifdef MULTICHANS_TESTED
69 size_t ichans = snd.getChannels();
70#else
71 size_t ichans = 1;
72#endif
73 size_t ilen = ismp*ichans;
75
76 //copy from sound to buffer
77 for (size_t t = 0; t < ismp; t++)
78 {
79 for (size_t c = 0; c < ichans; c++)
80 {
81 arri[t] = snd.getSafe(t, c);
82 }
83 }
84
85 size_t osmp = (size_t)(ilen * orate / irate + .5);
86 size_t ochans = ichans;
87 size_t olen = osmp*ochans;
89
90 //resample
91 size_t odone;
92 soxr_error_t error = soxr_oneshot(irate, orate, ichans, // Rates and # of channels
93 arri, ilen, NULL, // Input
94 arro, olen, &odone, // Output
95 &tit, NULL, NULL); // Configuration
96 if (error != 0)
97 {
98 yCError(SOUNDFILTERS) << "libsoxr resample failed";
99 delete[]arri;
100 delete[]arro;
101 return false;
102 }
103
104 //copy from buffer to sound
105 snd.setFrequency(size_t(orate));
106 snd.resize(olen);
107 for (size_t t = 0; t < osmp; t++)
108 {
109 for (size_t c = 0; c < ochans; c++)
110 {
111 snd.setSafe(arro[t], t, c);
112 }
113 }
114
115 //clear buffer
116 delete[]arri;
117 delete[]arro;
118
119 return true;
120#endif
121}
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
#define yCError(component,...)
#define yCWarning(component,...)
#define YARP_LOG_COMPONENT(name,...)
An interface to the operating system, including Port based communication.
Definition OSTestMsg.cpp:13
bool resample(yarp::sig::Sound &snd, size_t frequency)
Resample a sound.