YARP
Yet Another Robot Platform
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 
7 #include <yarp/sig/SoundFile.h>
10 
11 #include <yarp/conf/system.h>
12 
13 #include <yarp/os/NetInt16.h>
14 #include <yarp/os/NetInt32.h>
15 #include <yarp/os/ManagedBytes.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 
24 using namespace yarp::os;
25 using namespace yarp::sig;
26 using namespace yarp::sig::file;
27 
28 namespace
29 {
30  YARP_LOG_COMPONENT(SOUNDFILE, "yarp.sig.SoundFile")
31 }
32 
33 //#######################################################################################################
34 
35 bool 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 
56 bool 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  {
60  return read_wav_bytestream(data, bytestream);
61  }
62  else if (strcmp(format.c_str(), ".mp3") == 0)
63  {
64  return read_mp3_bytestream(data, bytestream, streamsize);
65  }
66  yCError(SOUNDFILE) << "Unknown file format";
67  return false;
68 }
69 
70 bool 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 
92 //#######################################################################################################
93 
94 bool yarp::sig::file::soundStreamReader::open(const char *filename)
95 {
96  bool b = yarp::sig::file::read(m_sound_data,filename);
97  if (!b)
98  {
99  yCError(SOUNDFILE, "Unable to read data from file %s", filename);
100  return false;
101  }
102 
103  m_index=0;
104  m_totsize = m_sound_data.getSamples();
105  return true;
106 }
107 
109 {
110  m_sound_data.resize(0,0);
111  m_index = 0;
112  m_totsize = 0;
113  return true;
114 }
115 
116 size_t yarp::sig::file::soundStreamReader::readBlock(Sound& dest, size_t block_size)
117 {
118  if (m_totsize == 0)
119  {
120  yCError(SOUNDFILE, "File is not open yet");
121  return false;
122  }
123 
124  if (m_index+block_size>m_totsize) {block_size= m_totsize-m_index;}
125  dest = m_sound_data.subSound(m_index,m_index+block_size);
126  m_index += block_size;
127  return block_size;
128 }
129 
131 {
132  if (m_totsize == 0)
133  {
134  yCError(SOUNDFILE, "File is not open yet");
135  return false;
136  }
137  m_index=sample_offset;
138  return true;
139 }
140 
142 {
143  if (m_totsize == 0)
144  {
145  yCError(SOUNDFILE, "File is not open yet");
146  return false;
147  }
148  return m_index;
149 }
Class for storing sounds See Audio in YARP for additional documentation on YARP audio.
Definition: Sound.h:26
Sound subSound(size_t first_sample, size_t last_sample)
Returns a subpart of the sound.
Definition: Sound.cpp:102
bool open(const char *filename)
Definition: SoundFile.cpp:94
bool rewind(size_t sample_offset=0)
Definition: SoundFile.cpp:130
size_t readBlock(Sound &dest, size_t block_size)
Definition: SoundFile.cpp:116
#define yCError(component,...)
Definition: LogComponent.h:154
#define YARP_LOG_COMPONENT(name,...)
Definition: LogComponent.h:77
An interface to the operating system, including Port based communication.
Image file operations.
Definition: ImageFile.h:21
bool read(ImageOf< PixelRgb > &dest, const std::string &src, image_fileformat format=FORMAT_ANY)
Definition: ImageFile.cpp:923
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_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)
Definition: ImageFile.cpp:1099
bool read_bytestream(Sound &data, const char *filename, size_t streamsize, std::string format)
Read a sound from a byte array.
Definition: SoundFile.cpp:56
bool write_wav_file(const Sound &data, const char *filename)
Write a sound to a .wav file.
bool read_wav_bytestream(Sound &data, const char *bytestream)
Read a sound from a byte array.
Signal processing.
Definition: Image.h:22