23#if defined (YARP_HAS_FFMPEG)
26 #include <libavutil/opt.h>
27 #include <libavcodec/avcodec.h>
28 #include <libavcodec/version.h>
29 #include <libavutil/channel_layout.h>
30 #include <libavutil/common.h>
31 #include <libavutil/imgutils.h>
32 #include <libavutil/mathematics.h>
33 #include <libavutil/samplefmt.h>
47#if defined (YARP_HAS_FFMPEG)
48#define AUDIO_INBUF_SIZE 20480
49#define AUDIO_REFILL_THRESH 4096
53#if defined (YARP_HAS_FFMPEG)
68#if LIBAVCODEC_VERSION_MAJOR >= 61
103 for (i = 0; i < frame->nb_samples; i++)
107 short int val = *((
short int*)frame->data[ch] + i);
120#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(61, 13, 100)
121#define YARP_FFMPEG_HAS_GET_SUPPORTED_CONFIG
125 const void* out =
nullptr;
129 return static_cast<const T*
>(out);
135#ifdef YARP_FFMPEG_HAS_GET_SUPPORTED_CONFIG
138 return codec->sample_fmts;
144#ifdef YARP_FFMPEG_HAS_GET_SUPPORTED_CONFIG
147 return codec->supported_samplerates;
215 os.write((
const char*)(
pkt->data),
pkt->size);
221#if LIBAVCODEC_VERSION_MAJOR >= 61
229#ifdef YARP_FFMPEG_HAS_GET_SUPPORTED_CONFIG
232 p = codec->ch_layouts;
240 while (p->nb_channels)
268 if (!codec->channel_layouts) {
272 p = codec->channel_layouts;
292#if !defined (YARP_HAS_FFMPEG)
297 const AVCodec * codec =
nullptr;
305#if LIBAVCODEC_VERSION_MAJOR < 58
339#if LIBAVCODEC_VERSION_MAJOR >= 61
359 fos.
open(filename, std::fstream::out | std::fstream::binary);
360 if (
fos.is_open()==
false)
384 frame->nb_samples =
c->frame_size;
385 frame->format =
c->sample_fmt;
387#if LIBAVCODEC_VERSION_MAJOR >= 61
397 frame->channel_layout =
c->channel_layout;
415 for (
size_t i = 0; i <
nframes; i++)
426#if LIBAVCODEC_VERSION_MAJOR >= 61
432 samples = (
uint16_t*)frame->data[0];
433 for (
int j = 0; j <
c->frame_size; j++)
439 if (encode(
c, frame,
pkt,
fos) ==
false)
457#if LIBAVCODEC_VERSION_MAJOR >= 61
463 samples = (
uint16_t*)frame->data[0];
481 if (encode(
c, frame,
pkt,
fos) ==
false)
505#if !defined (YARP_HAS_FFMPEG)
515 std::streampos
start_pos = istream.tellg();
551 const AVCodec* codec =
nullptr;
563#if LIBAVCODEC_VERSION_MAJOR < 58
637 len = istream.gcount();
664 offset +=
it->getSamples();
700 fis.
open(filename, std::fstream::in | std::fstream::binary);
701 if (
fis.is_open() ==
false)
720#if !defined (YARP_HAS_FFMPEG)
724 const AVCodec * codec =
nullptr;
729 std::ostringstream oss(std::ios::binary);
732#if LIBAVCODEC_VERSION_MAJOR < 58
766#if LIBAVCODEC_VERSION_MAJOR >= 61
814 frame->nb_samples =
c->frame_size;
815 frame->format =
c->sample_fmt;
817#if LIBAVCODEC_VERSION_MAJOR >= 61
828 frame->channel_layout =
c->channel_layout;
864 os.write((
const char*)(p->data), p->size);
876 for (
size_t i = 0; i <
nframes; i++)
886#if LIBAVCODEC_VERSION_MAJOR >= 61
892 samples = (
uint16_t*)frame->data[0];
893 for (
int j = 0; j <
c->frame_size; j++)
920#if LIBAVCODEC_VERSION_MAJOR >= 61
926 samples = (
uint16_t*)frame->data[0];
bool read_mp3_istream(Sound &sound_data, std::istream &istream)
A mini-server for performing network communication in the background.
void close() override
Stop port activity.
bool open(const std::string &name) override
Start port operation, with a specific name, with automatically-chosen network parameters.
T * read(bool shouldWait=true) override
Read an available object from the port.
void write(bool forceStrict=false)
Write the current object being returned by BufferedPort::prepare.
Class for storing sounds See Audio in YARP for additional documentation on YARP audio.
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 overwrite(const Sound &alt, size_t offset=0, size_t len=0)
It overwrites the sound with another sound, starting from a specified offset.
audio_sample get(size_t sample, size_t channel=0) const
size_t getSamples() const
Get the number of samples contained in the sound.
#define yCError(component,...)
#define yCDebug(component,...)
#define YARP_LOG_COMPONENT(name,...)
NetInt32 encode(const std::string &str)
Convert a string into a vocabulary identifier.
std::string decode(NetInt32 code)
Convert a vocabulary identifier into a string.
An interface to the operating system, including Port based communication.
bool write_mp3_file(const Sound &data, const char *filename, size_t bitrate=64000)
Write a sound to a mp3 file.
bool write_mp3_bytestream(const Sound &data, char *bytestream, size_t &streamsize)
write a sound from a byte array.
bool read_mp3_file(Sound &data, const char *filename)
Read a sound from a .mp3 audio file.
bool read_mp3_bytestream(Sound &data, const char *bytestream, size_t streamsize)
Read a sound from a byte array.