-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathac_infile_audio.cpp
133 lines (121 loc) · 3.36 KB
/
ac_infile_audio.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/* ac_infile_audio.cpp - Definition of the audiofile input class
* Copyright (C) 2021 Jeanette C. <jeanette@juliencoder.de>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 3
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include "ac_infile_audio.hpp"
using std::string;
using std::to_string;
namespace JBS {
Ac_infile_audio::Ac_infile_audio(const string& filename):
Ac_infile_base(filename)
{
its_file = NULL;
}
Ac_infile_audio::Ac_infile_audio(const char *filename):
Ac_infile_base(filename)
{
its_file = NULL;
}
Ac_infile_audio::~Ac_infile_audio()
{
if (its_open == true)
{
close();
}
}
// Open the connected file
bool Ac_infile_audio::open()
{
bool file_state = false;
if (its_open == true)
{ // Do nothing
file_state = true;
}
else // Open the file
{
// Open the file and get information
its_file = sf_open(its_filename.c_str(), SFM_READ, &its_info);
if (its_file == NULL)
{ // open error
its_error_msg = string(sf_strerror(its_file));
its_good = false;
}
else // File was opened successfully
{
file_state = true;
its_open = true;
its_good = true;
// Only allow mono input files
if (its_info.channels != 1)
{
its_error_msg = its_filename + string(" has more than one audio channel.");
its_good = false;
sf_close(its_file);
its_open = false;
its_file = NULL;
}
// Only allow up to 65536 samples
if (its_info.frames > 65536)
{
its_error_msg = its_filename + string(" has too many samples.");
its_good = false;
sf_close(its_file);
its_file = NULL;
its_open = false;
}
} // else file was opened successfully
} // if(its_open == true)
return file_state;
}
void Ac_infile_audio::close()
{
if (its_open == true)
{
sf_close(its_file);
its_file = NULL;
its_open = false;
}
}
// Read the data and process it
bool Ac_infile_audio::process()
{
bool process_state = false;
// Check that the file is open and ready for reading
if (its_open == true)
{
// Check that the file status is good
if (its_good == true)
{
// The whole file is one waveform
// So check the length to allocate output data
sf_count_t num_values = its_info.frames;
its_data = new double[num_values];
sf_count_t read_frames = 0;
read_frames = sf_readf_double(its_file,its_data,num_values);
if (read_frames != num_values)
{
its_error_msg = string("Only read ") + to_string(read_frames) + string(" of ") + to_string(num_values) + string(" samples.");
}
else // all was well, set process_state to true
{
its_size = num_values;
process_state = true;
}
} // if (iots_good == true)
} // if (its_open == rue)
return process_state;
}
} // End of namespace JBS