| 1 | /* MixingMatrix.cpp |
| 2 | * |
| 3 | * Copyright (C) 2010-2017 David Weenink |
| 4 | * |
| 5 | * This code is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 2 of the License, or (at |
| 8 | * your option) any later version. |
| 9 | * |
| 10 | * This code is distributed in the hope that it will be useful, but |
| 11 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 13 | * See the GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this work. If not, see <http://www.gnu.org/licenses/>. |
| 17 | */ |
| 18 | |
| 19 | #include "Interpreter.h" |
| 20 | #include "MixingMatrix.h" |
| 21 | |
| 22 | Thing_implement (MixingMatrix, TableOfReal, 0); |
| 23 | |
| 24 | autoMixingMatrix MixingMatrix_create (integer numberOfOutputChannels, integer numberOfInputChannels) { |
| 25 | try { |
| 26 | autoMixingMatrix me = Thing_new (MixingMatrix); |
| 27 | TableOfReal_init (me.get(), numberOfOutputChannels, numberOfInputChannels); |
| 28 | return me; |
| 29 | } catch (MelderError) { |
| 30 | Melder_throw (U"MixingMatrix not created."); |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | autoMixingMatrix MixingMatrix_createSimple (integer numberOfOutputChannels, integer numberOfInputChannels, conststring32 elements_string) { |
| 35 | try { |
| 36 | autoSTRVEC elements = splitByWhitespace_STRVEC (elements_string); |
| 37 | |
| 38 | const integer numberOfCells = numberOfInputChannels * numberOfOutputChannels; |
| 39 | |
| 40 | Melder_require (elements.size == numberOfCells, |
| 41 | U"The number of mixing coefficients (", elements.size, U") should equal the number of cells (", numberOfCells, U") in the mixing matrix."); |
| 42 | |
| 43 | autoMixingMatrix me = MixingMatrix_create (numberOfOutputChannels, numberOfInputChannels); |
| 44 | |
| 45 | /* |
| 46 | Construct the full matrix from the elements |
| 47 | */ |
| 48 | for (integer inum = 1; inum <= numberOfCells; inum ++) { |
| 49 | double number; |
| 50 | Interpreter_numericExpression (0, elements [inum].get(), & number); |
| 51 | const integer irow = (inum - 1) / numberOfInputChannels + 1; |
| 52 | const integer icol = (inum - 1) % numberOfInputChannels + 1; |
| 53 | my data [irow] [icol] = number; |
| 54 | } |
| 55 | return me; |
| 56 | } catch (MelderError) { |
| 57 | Melder_throw (U"MixingMatrix not created."); |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | void MixingMatrix_setRandomGauss (MixingMatrix me, double mean, double stdev) { |
| 62 | for (integer i = 1; i <= my numberOfRows; i++) { |
| 63 | for (integer j = 1; j <= my numberOfColumns; j++) { |
| 64 | my data [i] [j] = NUMrandomGauss (mean, stdev); |
| 65 | } |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | void MixingMatrix_multiplyInputChannel (MixingMatrix me, integer inputChannel, double value) { |
| 70 | if (inputChannel >= 1 && inputChannel <= my numberOfColumns) { |
| 71 | for (integer i = 1; i <= my numberOfRows; i ++) { |
| 72 | my data [i] [inputChannel] *= value; |
| 73 | } |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | // https://developer.mozilla.org/en-US/docs/Web/API/AudioNode/channelInterpretation |
| 78 | void MixingMatrix_setStandardChannelInterpretation (MixingMatrix me) { |
| 79 | for (integer i = 1; i <= my numberOfRows; i++) { |
| 80 | for (integer j = 1; j <= my numberOfColumns; j++) { |
| 81 | my data [i] [j] = 0.0; |
| 82 | } |
| 83 | } |
| 84 | bool dimensionsCovered = true; |
| 85 | if (my numberOfColumns == 1) { // mono input |
| 86 | if (my numberOfRows == 2) { // up-mix to stereo |
| 87 | /* |
| 88 | The M input channel is used for both output channels (L and R). |
| 89 | output.L = input.M |
| 90 | output.R = input.M |
| 91 | */ |
| 92 | my data [1] [1] = my data [2] [1] = 1.0; |
| 93 | } else if (my numberOfRows == 4) { // up-mix to quad |
| 94 | /* |
| 95 | The M input channel is used for non-surround output channels (L and R). |
| 96 | Surround output channels (SL and SR) are silent. |
| 97 | output.L = input.M |
| 98 | output.R = input.M |
| 99 | output.SL = 0 |
| 100 | output.SR = 0 |
| 101 | */ |
| 102 | my data [1] [1] = my data [2] [1] = 1.0; |
| 103 | } else if (my numberOfRows == 6) { // up-mix to to 5.1 |
| 104 | /* |
| 105 | The M input channel is used for the center output channel (C). |
| 106 | All the others (L, R, LFE, SL, and SR) are silent. |
| 107 | output.L = 0 |
| 108 | output.R = 0 |
| 109 | output.C = input.M |
| 110 | output.LFE = 0 |
| 111 | output.SL = 0 |
| 112 | output.SR = 0 |
| 113 | */ |
| 114 | my data [3] [1] = 1.0; |
| 115 | } else { |
| 116 | dimensionsCovered = false; |
| 117 | } |
| 118 | } else if (my numberOfColumns == 2) { // stereo input |
| 119 | if (my numberOfRows == 1) { // down-mix to mono |
| 120 | /* |
| 121 | Both input channels (L and R) are equally combined to produce the unique output channel (M). |
| 122 | output.M = 0.5 * (input.L + input.R) |
| 123 | */ |
| 124 | my data [1] [1] = my data [1] [2] = 0.5; |
| 125 | } else if (my numberOfRows == 2) { // to stereo |
| 126 | /* |
| 127 | output.L = input.L |
| 128 | output.R = input.R |
| 129 | */ |
| 130 | my data [1] [1] = my data [2] [2] = 1.0; |
| 131 | } else if (my numberOfRows == 4) { // up-mix to quad |
| 132 | /* |
| 133 | The L and R input channels are used for their non-surround respective output channels (L and R). |
| 134 | Surround output channels (SL and SR) are silent. |
| 135 | output.L = input.L |
| 136 | output.R = input.R |
| 137 | output.SL = 0 |
| 138 | output.SR = 0 |
| 139 | */ |
| 140 | my data [1] [1] = my data [2] [2] = 1.0; |
| 141 | } else if (my numberOfRows == 6) { // up-mix to 5.1 |
| 142 | /* |
| 143 | The L and R input channels are used for their non-surround respective output channels (L and R). |
| 144 | Surround output channels (SL and SR), as well as the center (C) and subwoofer (LFE) channels, are left silent. |
| 145 | output.L = input.L |
| 146 | output.R = input.R |
| 147 | output.C = 0 |
| 148 | output.LFE = 0 |
| 149 | output.SL = 0 |
| 150 | output.SR = 0 |
| 151 | */ |
| 152 | my data [1] [1] = my data [2] [2] = 1.0; |
| 153 | } else { // up-mix |
| 154 | dimensionsCovered = false; |
| 155 | } |
| 156 | } else if (my numberOfColumns == 4) { // quad input |
| 157 | if (my numberOfRows == 1) { // down-mix to mono |
| 158 | /* |
| 159 | All four input channels (L, R, SL, and SR) are equally combined to produce the unique output |
| 160 | channel (M). |
| 161 | output.M = 0.25 * (input.L + input.R + input.SL + input.SR) |
| 162 | */ |
| 163 | my data [1] [1] = my data [1] [2] = my data [1] [3] = my data [1] [4] = 0.25; |
| 164 | } else if (my numberOfRows == 2) { // down-mix to stereo |
| 165 | /* |
| 166 | Both left input channels (L and SL) are equally combined to produce the unique left output channel (L). |
| 167 | And similarly, both right input channels (R and SR) are equally combined to produce the unique right output channel (R). |
| 168 | output.L = 0.5 * (input.L + input.SL) |
| 169 | output.R = 0.5 * (input.R + input.SR) |
| 170 | */ |
| 171 | my data [1] [1] = my data [1] [3] = 0.5; |
| 172 | my data [1] [2] = my data [1] [4] = 0.5; |
| 173 | } else if (my numberOfRows == 6) { // up-mix to 5.1 |
| 174 | /* |
| 175 | The L, R, SL, and SR input channels are used for their respective output channels (L and R). |
| 176 | Center (C) and subwoofer (LFE) channels are left silent. |
| 177 | output.L = input.L |
| 178 | output.R = input.R |
| 179 | output.C = 0 |
| 180 | output.LFE = 0 |
| 181 | output.SL = input.SL |
| 182 | output.SR = input.SR |
| 183 | */ |
| 184 | my data [1] [1] = my data [2] [2] = my data [5] [3] = my data [6] [4] = 1.0; |
| 185 | } else { |
| 186 | dimensionsCovered = false; |
| 187 | } |
| 188 | } else if (my numberOfColumns == 6) { // 5.1 input) |
| 189 | if (my numberOfRows == 1) { // down-mix to mono |
| 190 | /* |
| 191 | The left (L and SL), right (R and SR) and central channels are all mixed together. |
| 192 | The surround channels are slightly attenuated and the regular lateral channels are power-compensated to make them count as a single channel by multiplying by √2/2. |
| 193 | The subwoofer (LFE) channel is lost. |
| 194 | output.M = 0.7071 * (input.L + input.R) + input.C + 0.5 * (input.SL + input.SR) |
| 195 | */ |
| 196 | my data [1] [3] = 1.0; |
| 197 | my data [1] [1] = my data [1] [2] = 0.5; // NUMsqrt1_2 is not safe if the channels are equal; |
| 198 | my data [1] [5] = my data [1] [6] = 0.5; |
| 199 | } else if (my numberOfRows == 2) { // down-mix to stereo |
| 200 | /* |
| 201 | The central channel (C) is summed with each lateral surround channel (SL or SR) and mixed to each lateral channel. |
| 202 | As it is mixed down to two channels, it is mixed at a lower power: in each case it is multiplied by √2/2. |
| 203 | The subwoofer (LFE) channel is lost. |
| 204 | output.L = input.L + 0.7071 * (input.C + input.SL) |
| 205 | output.R = input.R + 0.7071 * (input.C + input.SR) |
| 206 | */ |
| 207 | my data [1] [1] = my data [2] [2] = 1.0; |
| 208 | my data [1] [3] = my data [1] [5] = 0.5; // NUMsqrt1_2; |
| 209 | my data [2] [4] = my data [2] [6] = 0.5; // NUMsqrt1_2; |
| 210 | } else if (my numberOfRows == 4) { // down-mix to quad |
| 211 | /* |
| 212 | The central (C) is mixed with the lateral non-surround channels (L and R). |
| 213 | As it is mixed down to two channels, it is mixed at a lower power: in each case it is multiplied by √2/2. |
| 214 | The surround channels are passed unchanged. The subwoofer (LFE) channel is lost. |
| 215 | output.L = input.L + 0.7071 * input.C |
| 216 | output.R = input.R + 0.7071 * input.C |
| 217 | output.SL = input.SL |
| 218 | output.SR = input.SR |
| 219 | */ |
| 220 | my data [1] [1] = my data [2] [2] = my data [3] [5] = my data [4] [6] = 1.0; |
| 221 | my data [1] [3] = my data [2] [3] = NUMsqrt1_2; |
| 222 | } else { |
| 223 | dimensionsCovered = false; |
| 224 | } |
| 225 | } else { |
| 226 | dimensionsCovered = false; |
| 227 | } |
| 228 | if (! dimensionsCovered) { |
| 229 | /* |
| 230 | Fill each output channel with its input counterpart, that is the input channel with the same index. |
| 231 | Channels with no corresponding input channels are left silent. |
| 232 | */ |
| 233 | const integer lowerDimension = std::min (my numberOfRows, my numberOfColumns); |
| 234 | for (integer i = 1; i <= lowerDimension; i++) |
| 235 | my data [i] [i] = 1.0; |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | void MixingMatrix_muteAndActivateChannels (MixingMatrix me, constBOOLVEC const& muteChannels) { |
| 240 | Melder_assert (muteChannels.size == my numberOfColumns); |
| 241 | integer numberOfMuteChannels = 0; |
| 242 | for (integer ichan = 1; ichan <= muteChannels.size; ichan ++) |
| 243 | if (muteChannels [ichan]) |
| 244 | numberOfMuteChannels ++; |
| 245 | /* |
| 246 | Set all mute channels to 0 and all other channels to 1. |
| 247 | To prevent overflow, scale by the number of channels that are on. |
| 248 | */ |
| 249 | const double coefficient = ( my numberOfColumns > numberOfMuteChannels ? 1.0 / (my numberOfColumns - numberOfMuteChannels) : 0.0 ); |
| 250 | for (integer ichan = 1; ichan <= my numberOfColumns; ichan ++) { |
| 251 | const double channelScaling = ( muteChannels [ichan] ? 0.0 : coefficient ); |
| 252 | for (integer irow = 1; irow <= my numberOfRows; irow ++) |
| 253 | my data [irow] [ichan] = channelScaling; |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | /* End of file MixingMatrix.cpp */ |