YARP
Yet Another Robot Platform
 
Loading...
Searching...
No Matches
Mp3Sound.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
6#include "Mp3Sound.h"
7
8#include <algorithm>
9#include <cmath>
10
12#include <yarp/sig/SoundFile.h>
13
14using namespace yarp::os;
15using namespace yarp::sig;
16
17namespace {
19 "yarp.carrier.portmonitor.sound_compression_mp3",
23 nullptr)
24}
25
27{
28 senderSide = (options.find("sender_side").asBool());
29 return true;
30}
31
35
37{
38 return false;
39}
40
42{
43 return false;
44}
45
47{
48 if (senderSide)
49 {
51 if(snd == nullptr)
52 {
53 yCError(MP3TOSOUND, "Sender: Data cannot be accepted!");
54 return false;
55 }
56 }
57 else
58 {
59 auto* bot = thing.cast_as<Bottle>();
60 if(bot == nullptr)
61 {
62 yCError(MP3TOSOUND, "Receiver: Data cannot be accepted, invalid datatype");
63 return false;
64 }
65 if(bot->size() != 1)
66 {
67 yCError(MP3TOSOUND, "Receiver: Data cannot be accepted, invalid format");
68 return false;
69 }
70 }
71 return true;
72}
73
75{
76 if (senderSide)
77 {
79 if (snd)
80 {
81 size_t lenght=0;
82 bool success = yarp::sig::file::write_bytestream(*snd, nullptr, lenght, ".mp3");
83 if (!success || lenght == 0)
84 {
85 yCError(MP3TOSOUND, "Failed in allocating buffer size!");
86 return m_th;
87 }
88 char* bs = new char[lenght];
90 if (success)
91 {
92 m_bot.clear();
94 m_bot.add(v);
95 }
96 else
97 {
98 yCError(MP3TOSOUND, "Encoding failed!");
99 }
100 delete[] bs;
101
102 m_th.setPortWriter(&m_bot);
103 }
104 else
105 {
106 yCError(MP3TOSOUND, "Conversion failed!");
107 }
108 }
109 else
110 {
111 yarp::os::Bottle* bot = thing.cast_as<Bottle>();
112 if (bot)
113 {
114 const char* binary_data = bot->get(0).asBlob();
115 size_t length = bot->get(0).asBlobLength();
116
117 bool success = yarp::sig::file::read_bytestream(m_snd, binary_data, length, ".mp3");
118 m_th.setPortWriter(&m_snd);
119 }
120 else
121 {
122 yCError(MP3TOSOUND, "Conversion failed!");
123 }
124 }
125
126 return m_th;
127}
bool accept(yarp::os::Things &thing) override
This will be called when the data reach the portmonitor object.
Definition Mp3Sound.cpp:46
bool create(const yarp::os::Property &options) override
This will be called when the dll is properly loaded by the portmonitor carrier.
Definition Mp3Sound.cpp:26
yarp::os::Things & update(yarp::os::Things &thing) override
After data get accpeted in the accept() callback, an instance of that is given to the update function...
Definition Mp3Sound.cpp:74
bool getparam(yarp::os::Property &params) override
This will be called when the portmonitor carrier parameters are requested via YARP admin port.
Definition Mp3Sound.cpp:41
void destroy() override
This will be called when the portmonitor object destroyes.
Definition Mp3Sound.cpp:32
bool setparam(const yarp::os::Property &params) override
This will be called when the portmonitor carrier parameters are set via YARP admin port.
Definition Mp3Sound.cpp:36
A simple collection of objects that can be described and transmitted in a portable way.
Definition Bottle.h:65
void add(const Value &value)
Add a Value to the bottle, at the end of the list.
Definition Bottle.cpp:315
Value & get(size_type index) const
Reads a Value v from a certain part of the list.
Definition Bottle.cpp:252
void clear()
Empties the bottle of any objects it contains.
Definition Bottle.cpp:121
A mini-server for performing network communication in the background.
static LogCallback printCallback()
Get current print callback.
Definition Log.cpp:892
static LogType minimumPrintLevel()
Get current minimum print level.
Definition Log.cpp:852
@ LogTypeReserved
Definition Log.h:98
A class for storing options and configuration information.
Definition Property.h:33
Value & find(const std::string &key) const override
Gets a value corresponding to a given keyword.
Base class for generic things.
Definition Things.h:18
T * cast_as()
Definition Things.h:53
void setPortWriter(yarp::os::PortWriter *writer)
Set the reference to a PortWriter object.
Definition Things.cpp:26
A single value (typically within a Bottle).
Definition Value.h:44
virtual bool asBool() const
Get boolean value.
Definition Value.cpp:192
virtual size_t asBlobLength() const
Get binary data length.
Definition Value.cpp:279
virtual const char * asBlob() const
Get binary data value.
Definition Value.cpp:273
Class for storing sounds See Audio in YARP for additional documentation on YARP audio.
Definition Sound.h:42
#define yCError(component,...)
#define YARP_LOG_COMPONENT(name,...)
An interface to the operating system, including Port based communication.
Definition OSTestMsg.cpp:13
bool write_bytestream(const Sound &data, char *bytestream, size_t &streamsize, std::string format)
Writes a sound to a byte array.
Definition SoundFile.cpp:92
bool read_bytestream(Sound &data, const char *bytestream, size_t streamsize, std::string format)
Read a sound from a byte array.
Definition SoundFile.cpp:56