YARP
Yet Another Robot Platform
 
Loading...
Searching...
No Matches
SoundFile.cpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2006-2021 Istituto Italiano di Tecnologia (IIT)
3 * SPDX-FileCopyrightText: 2006-2010 RobotCub Consortium
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
10
11#include <yarp/conf/system.h>
12
13#include <yarp/os/NetInt16.h>
14#include <yarp/os/NetInt32.h>
16#include <yarp/os/Vocab.h>
17
18#include <yarp/os/Log.h>
19#include <yarp/os/LogStream.h>
20
21#include <cstdio>
22#include <cstring>
23
24using namespace yarp::os;
25using namespace yarp::sig;
26using namespace yarp::sig::file;
27
28namespace
29{
30 YARP_LOG_COMPONENT(SOUNDFILE, "yarp.sig.SoundFile")
31}
32
33//#######################################################################################################
34
35bool yarp::sig::file::read(Sound& data, const char* filename)
36{
37 const char* file_ext = strrchr(filename, '.');
38 if (file_ext == nullptr)
39 {
40 yCError(SOUNDFILE) << "cannot find file extension in file name";
41 return false;
42 }
43
44 if (strcmp(file_ext, ".wav") == 0)
45 {
46 return read_wav_file(data, filename);
47 }
48 else if (strcmp(file_ext, ".mp3") == 0)
49 {
50 return read_mp3_file(data, filename);
51 }
52 yCError(SOUNDFILE) << "Unknown file format";
53 return false;
54}
55
56bool yarp::sig::file::read_bytestream(Sound& data, const char* bytestream, size_t streamsize, std::string format)
57{
58 if (strcmp(format.c_str(), ".wav") == 0)
59 {
61 }
62 else if (strcmp(format.c_str(), ".mp3") == 0)
63 {
65 }
66 yCError(SOUNDFILE) << "Unknown file format";
67 return false;
68}
69
70bool yarp::sig::file::write(const Sound& sound_data, const char* filename)
71{
72 const char* file_ext = strrchr(filename, '.');
73 if (file_ext == nullptr)
74 {
75 yCError(SOUNDFILE) << "cannot find file extension in file name";
76 return false;
77 }
78
79 if (strcmp(file_ext, ".wav") == 0)
80 {
81 return write_wav_file(sound_data, filename);
82 }
83 else if (strcmp(file_ext, ".mp3") == 0)
84 {
85 return write_mp3_file(sound_data, filename);
86 }
87
88 yCError(SOUNDFILE) << "Unknown file format";
89 return false;
90}
91
92bool yarp::sig::file::write_bytestream(const Sound& data, char* bytestream, size_t& streamsize, std::string format)
93{
94 if (strcmp(format.c_str(), ".wav") == 0)
95 {
97 }
98 else if (strcmp(format.c_str(), ".mp3") == 0)
99 {
101 }
102 yCError(SOUNDFILE) << "Unknown file format";
103 return false;
104}
A mini-server for performing network communication in the background.
Class for storing sounds See Audio in YARP for additional documentation on YARP audio.
Definition Sound.h:39
#define yCError(component,...)
#define YARP_LOG_COMPONENT(name,...)
An interface to the operating system, including Port based communication.
bool read(ImageOf< PixelRgb > &dest, const std::string &src, image_fileformat format=FORMAT_ANY)
bool write_mp3_file(const Sound &data, const char *filename, size_t bitrate=64000)
Write a sound to a mp3 file.
bool read_wav_file(Sound &data, const char *filename)
Read a sound from a .wav audio file.
bool read_wav_bytestream(Sound &data, const char *bytestream, size_t streamsize)
Read a sound from a byte array.
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 write_wav_bytestream(const Sound &data, char *bytestream, size_t &streamsize)
Write a sound to a byte array.
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
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.
bool write(const ImageOf< PixelRgb > &src, const std::string &dest, image_fileformat format=FORMAT_PPM)
bool write_wav_file(const Sound &data, const char *filename)
Write a sound to a .wav file.