forked from robotology/yarp
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added soundfilters::resample() method
- Loading branch information
Showing
3 changed files
with
120 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
/* | ||
* SPDX-FileCopyrightText: 2021 Istituto Italiano di Tecnologia (IIT) | ||
* SPDX-License-Identifier: BSD-3-Clause | ||
*/ | ||
|
||
#include <yarp/sig/SoundFilters.h> | ||
|
||
#include <yarp/os/Log.h> | ||
#include <yarp/os/LogStream.h> | ||
|
||
using namespace yarp::os; | ||
using namespace yarp::sig; | ||
using namespace yarp::sig::soundfilters; | ||
|
||
namespace | ||
{ | ||
YARP_LOG_COMPONENT(SOUNDFILTERS, "yarp.sig.SoundFilters") | ||
} | ||
|
||
#if defined (YARP_HAS_SOXR) | ||
#include <soxr.h> | ||
#endif | ||
|
||
//####################################################################################################### | ||
|
||
bool yarp::sig::soundfilters::resample(yarp::sig::Sound& snd, size_t frequency) | ||
{ | ||
#if !defined (YARP_HAS_SOXR) | ||
|
||
yCError(SOUNDFILTERS) << "libsoxr not available"; | ||
return false; | ||
#else | ||
//configuration | ||
soxr_io_spec_t tit; | ||
tit.itype = SOXR_INT16_I; | ||
tit.otype = SOXR_INT16_I; | ||
tit.scale = 1.0; | ||
|
||
double irate = snd.getFrequency(); | ||
double orate = double(frequency); | ||
size_t ilen = snd.getSamples(); | ||
yarp::sig::Sound::audio_sample* arri = new yarp::sig::Sound::audio_sample[ilen]; | ||
|
||
//copy from sound to buffer | ||
for (size_t t = 0; t < ilen; t++) | ||
{ | ||
for (size_t c = 0; c < 1; c++) | ||
{ | ||
arri[t] = snd.getSafe(t, c); | ||
} | ||
} | ||
|
||
size_t olen = (size_t)(ilen * orate / irate + .5); | ||
yarp::sig::Sound::audio_sample* arro = new yarp::sig::Sound::audio_sample[olen]; | ||
|
||
//resample | ||
size_t odone; | ||
soxr_error_t error = soxr_oneshot(irate, orate, 1, // Rates and # of channels | ||
arri, ilen, NULL, // Input | ||
arro, olen, &odone, // Output | ||
&tit, NULL, NULL); // Configuration | ||
if (error != 0) | ||
{ | ||
yCError(SOUNDFILTERS) << "libsoxr resample failed"; | ||
delete[]arri; | ||
delete[]arro; | ||
} | ||
|
||
//copy from buffer to sound | ||
snd.setFrequency(size_t(orate)); | ||
snd.resize(olen); | ||
for (size_t t = 0; t < olen; t++) | ||
{ | ||
for (size_t c = 0; c < 1; c++) | ||
{ | ||
snd.setSafe(arro[t], t, c); | ||
} | ||
} | ||
|
||
//clear buffer | ||
delete[]arri; | ||
delete[]arro; | ||
|
||
return true; | ||
#endif | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/* | ||
* SPDX-FileCopyrightText: 2021 Istituto Italiano di Tecnologia (IIT) | ||
* SPDX-License-Identifier: BSD-3-Clause | ||
*/ | ||
|
||
#ifndef YARP_SIG_SOUNDFILTERS_H | ||
#define YARP_SIG_SOUNDFILTERS_H | ||
|
||
#include <yarp/sig/Sound.h> | ||
|
||
namespace yarp { | ||
namespace sig{ | ||
namespace soundfilters { | ||
/** | ||
* Resample a sound | ||
* @param snd the sound to resample | ||
* @param frequency the output frequency | ||
* @return true on success | ||
*/ | ||
bool YARP_sig_API resample(yarp::sig::Sound& snd, size_t frequency); | ||
} | ||
} | ||
} | ||
|
||
#endif // YARP_SIG_SOUNDFILTERS_H |