Skip to content

Commit

Permalink
Added SoundFilters namespace
Browse files Browse the repository at this point in the history
Added soundfilters::resample() method
  • Loading branch information
randaz81 committed Jul 21, 2021
1 parent 8399e82 commit 0208a4a
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/libYARP_sig/src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ set(YARP_sig_HDRS
yarp/sig/PointCloudUtils-inl.h
yarp/sig/Sound.h
yarp/sig/SoundFile.h
yarp/sig/SoundFilters.h
yarp/sig/SoundFileMp3.h
yarp/sig/SoundFileWav.h
yarp/sig/Vector.h
Expand All @@ -45,6 +46,7 @@ set(YARP_sig_SRCS
yarp/sig/PointCloudBase.cpp
yarp/sig/PointCloudUtils.cpp
yarp/sig/Sound.cpp
yarp/sig/SoundFilters.cpp
yarp/sig/SoundFile.cpp
yarp/sig/SoundFileMp3.cpp
yarp/sig/SoundFileWav.cpp
Expand Down Expand Up @@ -116,6 +118,13 @@ if(YARP_HAS_JPEG)
list(APPEND YARP_sig_PRIVATE_DEPS JPEG)
endif()

if(YARP_HAS_SOXR)
target_include_directories(YARP_sig SYSTEM PRIVATE ${SOXR_INCLUDE_DIR})
target_compile_definitions(YARP_sig PRIVATE YARP_HAS_SOXR)
target_link_libraries(YARP_sig PRIVATE ${SOXR_LIBRARY})
list(APPEND YARP_sig_PRIVATE_DEPS SOXR)
endif()

if(YARP_HAS_ZLIB)
target_include_directories(YARP_sig SYSTEM PRIVATE ${ZLIB_INCLUDE_DIR})
target_compile_definitions(YARP_sig PRIVATE YARP_HAS_ZLIB)
Expand Down
86 changes: 86 additions & 0 deletions src/libYARP_sig/src/yarp/sig/SoundFilters.cpp
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
}
25 changes: 25 additions & 0 deletions src/libYARP_sig/src/yarp/sig/SoundFilters.h
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

0 comments on commit 0208a4a

Please sign in to comment.