| 1 | /* Sound_and_MixingMatrix.cpp |
| 2 | * |
| 3 | * Copyright (C) 2010-2019 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 "NUM2.h" |
| 21 | #include "Sound_and_MixingMatrix.h" |
| 22 | |
| 23 | void Sound_MixingMatrix_playPart (Sound me, MixingMatrix thee, double fromTime, double toTime, Sound_PlayCallback playCallback, Thing playBoss) { |
| 24 | try { |
| 25 | autoSound mix = Sound_MixingMatrix_mixPart (me, thee, fromTime, toTime); |
| 26 | Sound_playPart (mix.get(), fromTime, toTime, playCallback, playBoss); |
| 27 | } catch (MelderError) { |
| 28 | Melder_throw (me, U": not played."); |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | void Sound_MixingMatrix_play (Sound me, MixingMatrix thee, Sound_PlayCallback callback, Thing boss) { |
| 33 | Sound_MixingMatrix_playPart (me, thee, my xmin, my xmax, callback, boss); |
| 34 | } |
| 35 | |
| 36 | autoSound Sound_MixingMatrix_mix (Sound me, MixingMatrix thee) { |
| 37 | return Sound_MixingMatrix_mixPart (me, thee, my xmin, my xmax); |
| 38 | } |
| 39 | |
| 40 | autoSound Sound_MixingMatrix_mixPart (Sound me, MixingMatrix thee, double fromTime, double toTime) { |
| 41 | try { |
| 42 | Melder_require (my ny == thy numberOfColumns, |
| 43 | U"The number of inputs in the MixingMatrix and the number of channels in the Sound should be equal."); |
| 44 | |
| 45 | if (fromTime == toTime) { |
| 46 | fromTime = my xmin; |
| 47 | toTime = my xmax; |
| 48 | } |
| 49 | /* |
| 50 | Determine index range. |
| 51 | We use all the real or virtual samples that fit within [fromTime..toTime]. |
| 52 | */ |
| 53 | const integer ix1 = 1 + Melder_iceiling ((fromTime - my x1) / my dx); |
| 54 | const integer ix2 = 1 + Melder_ifloor ((toTime - my x1) / my dx); |
| 55 | Melder_require (ix1 <= ix2, |
| 56 | U"Mixed Sound would contain no samples."); |
| 57 | |
| 58 | autoSound him = Sound_create (thy numberOfRows, fromTime, toTime, ix2 - ix1 + 1, my dx, my x1 + (ix1 - 1) * my dx); |
| 59 | /* |
| 60 | 1 nx 1 nx |
| 61 | |..........| |..........| (me) |
| 62 | |-----------|---| |-----|----------| (index in me) |
| 63 | ix1 ix2 ix2 ix1 ix2 ix2 |
| 64 | 1 'nx' 'nx' |
| 65 | Example: (1) (2) (3) (4) |
| 66 | New sound: him_nx = ix2 - ix1 + 1 |
| 67 | Example: nx = 12 |
| 68 | (1) copy from [1, ix2] to [2-ix1, 1 - ix1 + ix2] |
| 69 | ix1=-3, ix2=8 [1,8] -> [5,12] |
| 70 | (2) copy from [1, nx] to [2-ix1, 1 - ix1 + nx] |
| 71 | ix1=-3, ix2=13 [1,12] -> [5,16] |
| 72 | (3) copy from [ix1,ix2] to [1 , ix2 - ix1 + 1] |
| 73 | ix1=4, ix2=10 [4,10] -> [1,7] |
| 74 | (4) copy from [ix1, nx] to [1 , nx - ix1 + 1] |
| 75 | ix1=4, ix2=21 [4,12] -> [1,9] |
| 76 | */ |
| 77 | if (! (toTime < my xmin || fromTime > my xmax)) { |
| 78 | integer off1 = 0, off2 = 0; |
| 79 | integer to_i1 = 1, to_i2 = his nx; |
| 80 | if (ix1 < 1) { // (1) + (2) |
| 81 | off1 = 1 - ix1; |
| 82 | to_i1 = 1 - ix1; |
| 83 | to_i2 = to_i1 + my nx; // (2) |
| 84 | if (ix2 < my nx) // (1) |
| 85 | to_i2 = 1 - ix1 + ix2; |
| 86 | } else { // (3) + (4) |
| 87 | off2 = ix1 - 1; |
| 88 | to_i2 = to_i1 + ix2 - ix1; // (3) |
| 89 | if (ix2 > my nx) // (4) |
| 90 | to_i2 = his nx; |
| 91 | } |
| 92 | for (integer i = 1; i <= thy numberOfRows; i ++) { |
| 93 | for (integer ichan = 1; ichan <= my ny; ichan ++) { |
| 94 | const double mixingCoeffient = thy data [i] [ichan]; |
| 95 | if (mixingCoeffient != 0.0) { |
| 96 | for (integer j = 1; j <= to_i2 - to_i1 + 1; j ++) { |
| 97 | //to [j] += mixingCoeffient * from [j]; |
| 98 | his z [i] [j + off1] += mixingCoeffient * my z [ichan] [j + off2]; |
| 99 | } |
| 100 | } |
| 101 | } |
| 102 | } |
| 103 | } |
| 104 | return him; |
| 105 | } catch (MelderError) { |
| 106 | Melder_throw (me, U": not mixed."); |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | autoSound Sound_MixingMatrix_unmix (Sound me, MixingMatrix thee) { |
| 111 | try { |
| 112 | Melder_require (my ny == thy numberOfColumns, |
| 113 | U"The number of inputs in the MixingMatrix and the number of channels in the Sound should be equal."); |
| 114 | |
| 115 | autoMAT minv = newMATpseudoInverse (thy data.get(), 0.0); |
| 116 | autoSound him = Sound_create (thy numberOfColumns, my xmin, my xmax, my nx, my dx, my x1); |
| 117 | mul_MAT_out (his z.get(), minv.get(), my z.get()); |
| 118 | return him; |
| 119 | } catch (MelderError) { |
| 120 | Melder_throw (me, U": not unmixed."); |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | /* End of file Sound_and_MixingMatrix.cpp */ |