| 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"cxx . . $ `<file>/sys/Interpreter.h`/ |
| 20 | #include "NUM2.h"cxx . . $ `<file>/dwsys/NUM2.h`/ |
| 21 | #include "Sound_and_MixingMatrix.h"cxx . . $ `<file>/dwtools/Sound_and_MixingMatrix.h`/ |
| 22 | |
| 23 | void Sound_MixingMatrix_playPartcxx . . $ Sound_MixingMatrix_playPart(4e5f9b0f4e2af956). (Soundcxx . . $ Sound# me, MixingMatrixcxx . . $ MixingMatrix# thee, double fromTime, double toTime, Sound_PlayCallbackcxx . . $ Sound_PlayCallback# playCallback, Thingcxx . . $ Thing# playBoss) { |
| 24 | try { |
| 25 | autoSoundcxx . . $ autoSound# mix = Sound_MixingMatrix_mixPartcxx . . $ Sound_MixingMatrix_mixPart(eaeb577bb1d712cb). (me, thee, fromTime, toTime); |
| 26 | Sound_playPartcxx . . $ Sound_playPart(4a8b47ed00757136). (mix.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), fromTime, toTime, playCallback, playBoss); |
| 27 | } catch (MelderErrorcxx . . $ MelderError#) { |
| 28 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (me, U": not played."); |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | void Sound_MixingMatrix_playcxx . . $ Sound_MixingMatrix_play(20df8811c3b33ded). (Soundcxx . . $ Sound# me, MixingMatrixcxx . . $ MixingMatrix# thee, Sound_PlayCallbackcxx . . $ Sound_PlayCallback# callback, Thingcxx . . $ Thing# boss) { |
| 33 | Sound_MixingMatrix_playPartcxx . . $ Sound_MixingMatrix_playPart(4e5f9b0f4e2af956). (me, thee, mycxx . . $ `melder/melder.h:44:9`! xmincxx . . $ structFunction#xmin., mycxx . . $ `melder/melder.h:44:9`! xmaxcxx . . $ structFunction#xmax., callback, boss); |
| 34 | } |
| 35 | |
| 36 | autoSoundcxx . . $ autoSound# Sound_MixingMatrix_mixcxx . . $ Sound_MixingMatrix_mix(e0882d588ff3191a). (Soundcxx . . $ Sound# me, MixingMatrixcxx . . $ MixingMatrix# thee) { |
| 37 | return Sound_MixingMatrix_mixPartcxx . . $ Sound_MixingMatrix_mixPart(eaeb577bb1d712cb). (me, thee, mycxx . . $ `melder/melder.h:44:9`! xmincxx . . $ structFunction#xmin., mycxx . . $ `melder/melder.h:44:9`! xmaxcxx . . $ structFunction#xmax.); |
| 38 | } |
| 39 | |
| 40 | autoSoundcxx . . $ autoSound# Sound_MixingMatrix_mixPartcxx . . $ Sound_MixingMatrix_mixPart(eaeb577bb1d712cb). (Soundcxx . . $ Sound# me, MixingMatrixcxx . . $ MixingMatrix# thee, double fromTime, double toTime) { |
| 41 | try { |
| 42 | Melder_requirecxx . . $ `melder/melder_require.h:21:9`! (mycxx . . $ `melder/melder.h:44:9`! ny == thycxx . . $ `melder/melder.h:45:9`! 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 = mycxx . . $ `melder/melder.h:44:9`! xmincxx . . $ structFunction#xmin.; |
| 47 | toTime = mycxx . . $ `melder/melder.h:44:9`! xmaxcxx . . $ structFunction#xmax.; |
| 48 | } |
| 49 | /* |
| 50 | Determine index range. |
| 51 | We use all the real or virtual samples that fit within [fromTime..toTime]. |
| 52 | */ |
| 53 | const integercxx . . $ integer# ix1 = 1 + Melder_iceilingcxx . . $ `melder/melder_ftoi.h:45:9`! ((fromTime - mycxx . . $ `melder/melder.h:44:9`! x1cxx . . $ structSampled#x1.) / mycxx . . $ `melder/melder.h:44:9`! dxcxx . . $ structSampled#dx.); |
| 54 | const integercxx . . $ integer# ix2 = 1 + Melder_ifloorcxx . . $ `melder/melder_ftoi.h:33:9`! ((toTime - mycxx . . $ `melder/melder.h:44:9`! x1cxx . . $ structSampled#x1.) / mycxx . . $ `melder/melder.h:44:9`! dxcxx . . $ structSampled#dx.); |
| 55 | Melder_requirecxx . . $ `melder/melder_require.h:21:9`! (ix1 <= ix2, |
| 56 | U"Mixed Sound would contain no samples."); |
| 57 | |
| 58 | autoSoundcxx . . $ autoSound# him = Sound_createcxx . . $ Sound_create(39ab106d6b9a0135). (thycxx . . $ `melder/melder.h:45:9`! numberOfRowscxx . . $ structTableOfReal#numberOfRows., fromTime, toTime, ix2 - ix1 + 1, mycxx . . $ `melder/melder.h:44:9`! dxcxx . . $ structSampled#dx., mycxx . . $ `melder/melder.h:44:9`! x1cxx . . $ structSampled#x1. + (ix1 - 1) * mycxx . . $ `melder/melder.h:44:9`! dxcxx . . $ structSampled#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 < mycxx . . $ `melder/melder.h:44:9`! xmincxx . . $ structFunction#xmin. || fromTime > mycxx . . $ `melder/melder.h:44:9`! xmaxcxx . . $ structFunction#xmax.)) { |
| 78 | integercxx . . $ integer# off1 = 0, off2 = 0; |
| 79 | integercxx . . $ integer# to_i1 = 1, to_i2 = hiscxx . . $ `melder/melder.h:47:9`! nxcxx . . $ structSampled#nx.; |
| 80 | if (ix1 < 1) { // (1) + (2) |
| 81 | off1 = 1 - ix1; |
| 82 | to_i1 = 1 - ix1; |
| 83 | to_i2 = to_i1 + mycxx . . $ `melder/melder.h:44:9`! nxcxx . . $ structSampled#nx.; // (2) |
| 84 | if (ix2 < mycxx . . $ `melder/melder.h:44:9`! nxcxx . . $ structSampled#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 > mycxx . . $ `melder/melder.h:44:9`! nxcxx . . $ structSampled#nx.) // (4) |
| 90 | to_i2 = hiscxx . . $ `melder/melder.h:47:9`! nxcxx . . $ structSampled#nx.; |
| 91 | } |
| 92 | for (integercxx . . $ integer# i = 1; i <= thycxx . . $ `melder/melder.h:45:9`! numberOfRowscxx . . $ structTableOfReal#numberOfRows.; i ++) { |
| 93 | for (integercxx . . $ integer# ichan = 1; ichan <= mycxx . . $ `melder/melder.h:44:9`! nycxx . . $ structSampledXY#ny.; ichan ++) { |
| 94 | const double mixingCoeffient = thycxx . . $ `melder/melder.h:45:9`! datacxx . . $ structTableOfReal#data. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).i] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).ichan]; |
| 95 | if (mixingCoeffient != 0.0) { |
| 96 | for (integercxx . . $ integer# j = 1; j <= to_i2 - to_i1 + 1; j ++) { |
| 97 | //to [j] += mixingCoeffient * from [j]; |
| 98 | hiscxx . . $ `melder/melder.h:47:9`! zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).i] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).j + off1] += mixingCoeffient * mycxx . . $ `melder/melder.h:44:9`! zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).ichan] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).j + off2]; |
| 99 | } |
| 100 | } |
| 101 | } |
| 102 | } |
| 103 | } |
| 104 | return himcxx . . $ autoSomeThing#autoSomeThing(b79141a71f7bc86f).; |
| 105 | } catch (MelderErrorcxx . . $ MelderError#) { |
| 106 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (me, U": not mixed."); |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | autoSoundcxx . . $ autoSound# Sound_MixingMatrix_unmixcxx . . $ Sound_MixingMatrix_unmix(e0882d588ff3191a). (Soundcxx . . $ Sound# me, MixingMatrixcxx . . $ MixingMatrix# thee) { |
| 111 | try { |
| 112 | Melder_requirecxx . . $ `melder/melder_require.h:21:9`! (mycxx . . $ `melder/melder.h:44:9`! ny == thycxx . . $ `melder/melder.h:45:9`! numberOfColumns, |
| 113 | U"The number of inputs in the MixingMatrix and the number of channels in the Sound should be equal."); |
| 114 | |
| 115 | autoMATcxx . . $ autoMAT# minv = newMATpseudoInversecxx . . $ newMATpseudoInverse(d05068067bdbbf5a). (thycxx . . $ `melder/melder.h:45:9`! datacxx . . $ structTableOfReal#data..getcxx . . $ automatrix#get(b2b81450b5bdda66).(), 0.0); |
| 116 | autoSoundcxx . . $ autoSound# him = Sound_createcxx . . $ Sound_create(39ab106d6b9a0135). (thycxx . . $ `melder/melder.h:45:9`! numberOfColumnscxx . . $ structTableOfReal#numberOfColumns., mycxx . . $ `melder/melder.h:44:9`! xmincxx . . $ structFunction#xmin., mycxx . . $ `melder/melder.h:44:9`! xmaxcxx . . $ structFunction#xmax., mycxx . . $ `melder/melder.h:44:9`! nxcxx . . $ structSampled#nx., mycxx . . $ `melder/melder.h:44:9`! dxcxx . . $ structSampled#dx., mycxx . . $ `melder/melder.h:44:9`! x1cxx . . $ structSampled#x1.); |
| 117 | mul_MAT_outcxx . . $ mul_MAT_out(9e368e0da0f1874a). (hiscxx . . $ `melder/melder.h:47:9`! zcxx . . $ structMatrix#z..getcxx . . $ automatrix#get(b2b81450b5bdda66).(), minvcxx . . $ constmatrixview#constmatrixview(eed9513d24570b3d)..getcxx . . $ automatrix#get(b2b81450b5bdda66).(), mycxx . . $ `melder/melder.h:44:9`! zcxx . . $ structMatrix#z..getcxx . . $ automatrix#get(b2b81450b5bdda66).()); |
| 118 | return himcxx . . $ autoSomeThing#autoSomeThing(b79141a71f7bc86f).; |
| 119 | } catch (MelderErrorcxx . . $ MelderError#) { |
| 120 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (me, U": not unmixed."); |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | /* End of file Sound_and_MixingMatrix.cpp */ |