forked from vreemdelabs/Scoreview-Base
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaudiodata.cpp
More file actions
217 lines (193 loc) · 4.91 KB
/
audiodata.cpp
File metadata and controls
217 lines (193 loc) · 4.91 KB
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/*
Scoreview (R)
Copyright (C) 2015 Patrick Areny
All Rights Reserved.
Scoreview 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, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <string.h>
#include <iterator>
#include <list>
#include "audiodata.h"
CaudioChunk::CaudioChunk():
m_size(0),
m_used(0),
m_pdata(NULL)
{
try
{
m_pdata = new float[AUDIO_CHUNK_SIZE];
m_size = AUDIO_CHUNK_SIZE;
}
catch (int e)
{
printf("Error in audio chunk allocation\n");
}
}
CaudioChunk::~CaudioChunk()
{
delete[] m_pdata;
}
// Adds data to the current buffer and returns how many samples did not fit in it
int CaudioChunk::add_data(float *pdata, int size)
{
int rsize;
rsize = m_used + size > AUDIO_CHUNK_SIZE ? AUDIO_CHUNK_SIZE - m_used : size;
memcpy(m_pdata + m_used, pdata, rsize * sizeof(float));
m_used += rsize;
return (size - rsize);
}
bool CaudioChunk::spaceavailable()
{
return (m_used < m_size);
}
Caudiodata::Caudiodata()
{
m_CaudioChunk_list.clear();
}
Caudiodata::~Caudiodata()
{
for (m_iter = m_CaudioChunk_list.begin(); m_iter != m_CaudioChunk_list.end(); m_iter++)
delete *m_iter;
}
int Caudiodata::add_data(float *pdata, int size)
{
int local_size;
CaudioChunk* pchunk;
std::list<CaudioChunk*>::iterator iter;
local_size = size;
// Look for space in the last chunk
if (m_CaudioChunk_list.begin() != m_CaudioChunk_list.end())
{
iter = m_CaudioChunk_list.end();
iter--;
pchunk = *iter;
// Sapce left
if (pchunk->spaceavailable())
{
local_size = pchunk->add_data(pdata, local_size);
}
}
if (local_size > 0)
{
pchunk = new CaudioChunk();
local_size = pchunk->add_data(pdata, local_size);
m_CaudioChunk_list.push_back(pchunk);
while (local_size > 0)
{
pchunk = new CaudioChunk();
local_size = pchunk->add_data(pdata + size - local_size, local_size);
m_CaudioChunk_list.push_back(pchunk);
}
}
return 0;
}
CaudioChunk *Caudiodata::get_first()
{
m_iter = m_CaudioChunk_list.begin();
if (m_iter == m_CaudioChunk_list.end())
return (NULL);
return (*m_iter);
}
CaudioChunk *Caudiodata::get_next()
{
m_iter++;
if (m_iter == m_CaudioChunk_list.end())
return (NULL);
return (*m_iter);
}
// Returns the number of samples stored in the class
long Caudiodata::get_samplenum()
{
CaudioChunk *pchunk = get_first();
long samplenum = 0;
while (pchunk != NULL)
{
samplenum += pchunk->m_used;
pchunk = get_next();
}
return samplenum;
}
// Copies size samples from the position samplepos to pdataout, returns the number of samples copied
t_ret_samples Caudiodata::get_data(int samplepos, float *pdataout, int size)
{
int currentsample = 0;
CaudioChunk *pchunk;
int inchunkindex;
int copysize;
int progress;
t_ret_samples r;
r.obtained = 0;
r.missing_left = 0;
r.missing_right = size;
if (samplepos < 0)
{
r.missing_left = -samplepos;
r.missing_right -= r.missing_left;
size += samplepos;
samplepos = 0;
if (size < 0)
return r;
}
pchunk = get_first();
if (pchunk == NULL)
{
printf("Sample not available at this offset\n");
return (r);
}
while (currentsample + pchunk->m_used <= samplepos)
{
currentsample += pchunk->m_used;
pchunk = get_next();
if (pchunk == NULL)
{
printf("Sample not available at this offset\n");
return (r);
}
}
inchunkindex = samplepos - currentsample;
// Copy the samples from the consecutive chunks of data
progress = 0;
while (progress < size)
{
copysize = (pchunk->m_used - inchunkindex) > size - progress? size - progress : (pchunk->m_used - inchunkindex);
memcpy(pdataout + progress, pchunk->m_pdata + inchunkindex, copysize * sizeof(float));
progress += copysize;
inchunkindex = 0;
pchunk = get_next();
if (pchunk == NULL)
break;
}
r.obtained = progress;
r.missing_right = size - progress;
return(r);
}
// Returns the sample n
float Caudiodata::get_sample(int n)
{
CaudioChunk *pchunk = get_first();
long samplenum = 0;
long prevsamplenum;
if (pchunk->m_used > n)
return (pchunk->m_pdata[n]);
while (pchunk != NULL)
{
prevsamplenum = samplenum;
samplenum += pchunk->m_used;
if (samplenum > n)
{
return (pchunk->m_pdata[n - prevsamplenum]);
}
pchunk = get_next();
}
return 0;
}