-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChannelBuffersHandler.cpp
More file actions
97 lines (82 loc) · 3.61 KB
/
Copy pathChannelBuffersHandler.cpp
File metadata and controls
97 lines (82 loc) · 3.61 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
/*
* Copyright (c) 2010-2016 Stephane Poirier
*
* stephane.poirier@oifii.org
*
* Stephane Poirier
* 3532 rue Ste-Famille, #3
* Montreal, QC, H2X 2L1
*
* 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, see <http://www.gnu.org/licenses/>.
*
*/
#include "ChannelBuffersHandler.h"
ChannelBuffersHandler :: ChannelBuffersHandler(long singleChanBufferLengthIn, int numberOfChannelsIn)
{
singleChanBufferLength = singleChanBufferLengthIn;
numberOfChannels = numberOfChannelsIn;
//Initialize buffers array
singleChanBuffers = new SAMPLE*[numberOfChannels];
for(int i = 0; i < numberOfChannels; i++)
singleChanBuffers[i] = new SAMPLE[singleChanBufferLength];
}
ChannelBuffersHandler :: ~ChannelBuffersHandler()
{
for(int i = 0; i < numberOfChannels; i++)
delete []singleChanBuffers[i];
delete []singleChanBuffers;
}
//splits the interleaved inBuffer into individual channels and stores a local copy of them
void ChannelBuffersHandler :: acquireAndSeparateNewBuffer(SAMPLE* inBuffer)
{
int sampleNum = 0;
for(int i = 0; i < singleChanBufferLength; i++)
for(int j = 0; j < numberOfChannels; j++)
singleChanBuffers[j][i] = inBuffer[sampleNum++];
}
//copies channel buffers from another ChannelBuffersHandler
void ChannelBuffersHandler :: copyInBuffersFromHandler(ChannelBuffersHandler* inBuffersHandler)
{
//Make sure this handler has the same length buffers and number of channels as the one to copy from
assert(inBuffersHandler->getSingleChanBufferLength() == singleChanBufferLength);
assert(inBuffersHandler->getNumberOfChannels() == numberOfChannels);
//copy each of the other handler's buffers into this handler's single channel buffers, in order
for( int currentChannel = 0; currentChannel < numberOfChannels; currentChannel++ )
inBuffersHandler->copyOutChannelBuffer( singleChanBuffers[currentChannel], currentChannel );
}
//copies the specified channel buffer into the given destination
void ChannelBuffersHandler :: copyOutChannelBuffer(SAMPLE* copyDestination, int channelNum)
{
memcpy(copyDestination, singleChanBuffers[channelNum], singleChanBufferLength * sizeof(SAMPLE));
}
//copies a mix of the channel buffer contents into the copyDestination array specified by the coefficients in the channelMixCoeffs array
void ChannelBuffersHandler :: copyOutMixOfChannelBuffers(SAMPLE* copyDestination, SAMPLE* channelMixCoeffs)
{
//Skip through to the first non-zero weight. We assume weights are positive.
int firstChannel = 0;
while( channelMixCoeffs[firstChannel] <= 0.0000000001 )
firstChannel++;
//initialize with first non-zero weighted mix component
for(int i = 0; i < singleChanBufferLength; i++)
copyDestination[i] = channelMixCoeffs[firstChannel] * singleChanBuffers[firstChannel][i];
//sum in the rest of the non-zero weighted mix components
for(int currentChannel = firstChannel + 1; currentChannel < numberOfChannels; currentChannel++)
{
if( ! channelMixCoeffs[currentChannel] <= 0.0000000001 )
{
for(int i = 0; i < singleChanBufferLength; i++)
copyDestination[i] += channelMixCoeffs[currentChannel] * singleChanBuffers[currentChannel][i];
}
}
}