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);
135 if (!codec->supported_samplerates) {
139 p = codec->supported_samplerates;
177 os.write((
const char*)(
pkt->data),
pkt->size);
183#if LIBAVCODEC_VERSION_MAJOR >= 61
191 if (!codec->ch_layouts)
197 p = codec->ch_layouts;
198 while (p->nb_channels)
226 if (!codec->channel_layouts) {
230 p = codec->channel_layouts;
250#if !defined (YARP_HAS_FFMPEG)
255 const AVCodec * codec =
nullptr;
263#if LIBAVCODEC_VERSION_MAJOR < 58
297#if LIBAVCODEC_VERSION_MAJOR >= 61
317 fos.
open(filename, std::fstream::out | std::fstream::binary);
318 if (
fos.is_open()==
false)
342 frame->nb_samples =
c->frame_size;
343 frame->format =
c->sample_fmt;
345#if LIBAVCODEC_VERSION_MAJOR >= 61
355 frame->channel_layout =
c->channel_layout;
373 for (
size_t i = 0; i <
nframes; i++)
384#if LIBAVCODEC_VERSION_MAJOR >= 61
390 samples = (
uint16_t*)frame->data[0];
391 for (
int j = 0; j <
c->frame_size; j++)
397 if (encode(
c, frame,
pkt,
fos) ==
false)
415#if LIBAVCODEC_VERSION_MAJOR >= 61
421 samples = (
uint16_t*)frame->data[0];
439 if (encode(
c, frame,
pkt,
fos) ==
false)
463#if !defined (YARP_HAS_FFMPEG)
473 std::streampos
start_pos = istream.tellg();
509 const AVCodec* codec =
nullptr;
521#if LIBAVCODEC_VERSION_MAJOR < 58
595 len = istream.gcount();
622 offset +=
it->getSamples();
658 fis.
open(filename, std::fstream::in | std::fstream::binary);
659 if (
fis.is_open() ==
false)
678#if !defined (YARP_HAS_FFMPEG)
682 const AVCodec * codec =
nullptr;
687 std::ostringstream oss(std::ios::binary);
690#if LIBAVCODEC_VERSION_MAJOR < 58
724#if LIBAVCODEC_VERSION_MAJOR >= 61
772 frame->nb_samples =
c->frame_size;
773 frame->format =
c->sample_fmt;
775#if LIBAVCODEC_VERSION_MAJOR >= 61
786 frame->channel_layout =
c->channel_layout;
822 os.write((
const char*)(p->data), p->size);
834 for (
size_t i = 0; i <
nframes; i++)
844#if LIBAVCODEC_VERSION_MAJOR >= 61
850 samples = (
uint16_t*)frame->data[0];
851 for (
int j = 0; j <
c->frame_size; j++)
878#if LIBAVCODEC_VERSION_MAJOR >= 61
884 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.