<div dir="ltr"><div dir="ltr"><div dir="ltr"><div class="gmail_default" style="font-size:large"><div dir="ltr" style="color:rgb(0,0,0)"><div class="gmail_default"><font size="4" face="arial, sans-serif">Hi Dan, Hi Stef, Hi Levente, Hi Christoph,</font></div></div><div style="color:rgb(0,0,0)"><font size="4" face="arial, sans-serif"><br></font></div><div style="color:rgb(0,0,0)"><font size="4" face="arial, sans-serif">   (duplicating to change the subject line)</font></div><div style="color:rgb(0,0,0)"><font size="4" face="arial, sans-serif"><br></font></div><div class="gmail_default" style="color:rgb(0,0,0)"><font size="4" face="arial, sans-serif">   this is not a bug.  I'm just noting something for our collective understanding and pointing to a potential improvement in the sound system.  This is concerning the code in SampledSound>>#mixSampleCount:into:startingAt:leftVol:rightVol:.  If you look at the results of mixing down a 16KHz sound into e.g. a 22.1KHz sound (the default sampling rate for a mix down) then if the input is a 220Hz sine wave at 16KHz (A below middle C, computed in the example below) then the first few samples look like this</font></div><div class="gmail_default" style="color:rgb(0,0,0)"><font size="4" face="arial, sans-serif"><br></font></div><div class="gmail_default" style="color:rgb(0,0,0)"><font size="4" face="arial, sans-serif">a SoundBuffer(4 19 42 75 117 166 222 285 353 425 ...<br></font></div><div class="gmail_default" style="color:rgb(0,0,0)"><font size="4" face="arial, sans-serif"><br></font></div><div class="gmail_default" style="color:rgb(0,0,0)"><font size="4" face="arial, sans-serif">When we mix this down the corresponding range of samples in the stereo 22.1Khz mixed-down sound is</font></div><div class="gmail_default" style="color:rgb(0,0,0)"><font size="4" face="arial, sans-serif"><br></font></div><div class="gmail_default" style="color:rgb(0,0,0)"><font size="4" face="arial, sans-serif">a SoundBuffer(4 4 4 4 18 18 41 41 41 41 74 74 116 116 165 165 165 165 221 221 284 284 284 284 352 352 424 424 500 500 ...<br></font></div><div class="gmail_default" style="color:rgb(0,0,0)"><font size="4" face="arial, sans-serif"><br></font></div><div class="gmail_default" style="color:rgb(0,0,0)"><font size="4" face="arial, sans-serif">Now this comes because of the scaled indexing in SampledSound>>#mixSampleCount:into:startingAt:leftVol:rightVol: and is to be expected.  The input sample rate is less than the output sample rate so the algorithm smears mixed output samples until the next matching input sample is reached.  </font></div><div class="gmail_default" style="color:rgb(0,0,0)"><font size="4" face="arial, sans-serif"><br></font></div><div class="gmail_default" style="color:rgb(0,0,0)"><font size="4" face="arial, sans-serif">The improvement would be not to smear, but to interpolate.  If we added (say, simple linear) interpolation then the algorithm would instead produce</font></div><div class="gmail_default" style="color:rgb(0,0,0)"><font size="4" face="arial, sans-serif"><br></font></div><div class="gmail_default" style="color:rgb(0,0,0)"><font size="4" face="arial, sans-serif">a SoundBuffer(4 4 11 11 18 18 41 41 57 57 74 74 116 116 165 165 193 193 221 221 284 284 318 318 352 352 424 424 500 500 ...<br></font></div><div dir="ltr" style="color:rgb(0,0,0)"><br></div><div dir="ltr" style="color:rgb(0,0,0)"><br></div><div class="gmail_default" style="color:rgb(0,0,0)"><font size="4" face="arial, sans-serif">Now I (we?) just have to find time to work on this ;-)</font></div></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Sat, Sep 19, 2020 at 10:56 AM Eliot Miranda <<a href="mailto:eliot.miranda@gmail.com">eliot.miranda@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div style="font-size:large">Hi Stef,</div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Sat, Sep 19, 2020 at 1:17 AM Stéphane Rollandin <<a href="mailto:lecteur@zogotounga.net" target="_blank">lecteur@zogotounga.net</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex">>      if you play the example you’ll see that the first version works fine.  The second version, which adds MixedSound yo create stereo doesn’t.  That proves that the issue is in MixedSound.  Please just try the two examples.<br>
<br>
Right. What I can see If I do<br>
<br>
| samples sineTable sound |<br>
"1 second of A below middle C (220Hz). 16000 / 220 is 72.72 recurring"<br>
sineTable := SoundPlayer sineTable: 73.<br>
sineTable doWithIndex:<br>
        [:sample :index| sineTable at: index put: sample // 4].<br>
samples := SoundBuffer new: 16000.<br>
1 to: samples size by: sineTable size do:<br>
        [:i| samples replaceFrom: i to: (i + sineTable size - 1 min: 16000) <br>
with: sineTable startingAt: 1].<br>
1 to: 146 do:<br>
        [:i|<br>
        samples at: i put: ((samples at: i) * i / 146) asInteger.<br>
        samples at: 16001 - i put: ((samples at: 16001 - i) * i / 146) asInteger].<br>
sound := SampledSound samples: samples samplingRate: 16000.<br>
sound := MixedSound new<br>
                        add: sound pan: 0.25;<br>
                        add: sound pan: 0.75;<br>
                        yourself.<br>
(SampledSound samples: sound monoSamples samplingRate: sound <br>
originalSamplingRate) edit<br>
<br>
in the muO image I pointed to in my previous message, is a phasing <br>
problem leading to periodic glitches (the attached image is directly <br>
taken from the muO editor)<br></blockquote><div><br></div><div><img src="cid:ii_kf9ygws40" alt="image.png" width="563" height="130"><br></div><div><br></div><div><div><span style="font-size:large"></span><font size="4">Interesting!!  I don't see this in my data, but I do see evidence of an off-by-one error in misdown.  Here's the start, first zero crossing, and end of the mono sound (the original samples sound buffer)</font></div><font size="4"><br></font></div><div><font size="4"> { samples first: 8.<span class="gmail_default"> </span>samples copyFrom: 70 to: 77.<span class="gmail_default"> </span>samples last: 8 }</font></div><div><font size="4">{a SoundBuffer(4 19 42 75 117 166 222 285) .</font></div><div><font size="4"><span class="gmail_default"> </span>a SoundBuffer(-1003 -682 -347 0 356 720 1088 1457) .</font></div><div><font size="4"><span class="gmail_default"> </span><span class="gmail_default"></span>a SoundBuffer(221 222 213 196 170 136 96 50)}</font></div><div><font size="4"><br></font></div><div><div><font size="4">So these look fine and smooth.</font></div><div><font size="4"><br></font></div><div><font size="4">But here's the corresponding data from the computeSamplesForSeconds: result of the MixedSound:</font></div><div><div><font size="4">{ self first: 16. self copyFrom: 70 * 2 to: 77 * 2 + 1. self last: 16 }</font></div><div><font size="4"> {a SoundBuffer(4 4 4 4 18 18 41 41 41 41 74 74 116 116 165 165) .</font></div><div><font size="4">  a SoundBuffer(-2712 -2712 -2712 -2836 -2836 -2940 -2940 -2940 -2940 -3022 -3022 -3084 -3084 -3124 -3124 -3214) .</font></div><div><font size="4">  a SoundBuffer(212 212 212 212 196 196 169 169 169 169 136 136 96 96 49 49)}</font></div></div><div style="font-size:large"><br></div></div><div style="font-size:large">The data *should* start with <span style="color:rgb(0,0,0);font-family:-webkit-standard;font-size:medium">a SoundBuffer(4 4 18 18...</span></div><div style="font-size:large"><span style="color:rgb(0,0,0);font-family:-webkit-standard;font-size:medium">The data ends well.  But the first crossing is completely wrong.</span></div><div><br></div><div><div style="font-size:large">Strange, because it sounds ok...</div><div style="font-size:large"><br></div><div style="font-size:large">Let me count the crossings using:</div><div style="font-size:large">| sign crossings |</div><div style="font-size:large">crossings := 0.</div><div style="font-size:large">sign := self first sign.</div><div style="font-size:large">self do:</div><div style="font-size:large"><span style="white-space:pre-wrap">       </span>[:each|</div><div style="font-size:large"><span style="white-space:pre-wrap">        </span>each sign = sign negated ifTrue:</div><div style="font-size:large"><span style="white-space:pre-wrap">               </span>[crossings := crossings + 1.</div><div style="font-size:large"><span style="white-space:pre-wrap">           </span>sign := each sign]].</div><div style="font-size:large">crossings</div><div style="font-size:large"><br></div><div style="font-size:large">Both give 438 as expected.  Strange.  Why is there no crossing between 140 and 154 in the mixed down sound?</div><br></div><div><br></div><div><div style="font-size:large">Ah, ok, the sample rate has changed:</div><div style="font-size:large"><br></div><div style="font-size:large"><br></div><div style="font-size:large">| sign crossings ncrossings |</div><div style="font-size:large">ncrossings := 0.</div><div style="font-size:large">crossings := OrderedCollection new.</div><div style="font-size:large">sign := self first sign.</div><div style="font-size:large">self doWithIndex:</div><div style="font-size:large"><span style="white-space:pre-wrap">    </span>[:each :idx|</div><div style="font-size:large"><span style="white-space:pre-wrap">   </span>each sign = sign negated ifTrue:</div><div style="font-size:large"><span style="white-space:pre-wrap">               </span>[crossings add: idx - 1 -> idx.</div><div style="font-size:large"><span style="white-space:pre-wrap">             </span> ncrossings := ncrossings + 1.</div><div style="font-size:large"><span style="white-space:pre-wrap">         </span>sign := each sign]].</div><div style="font-size:large">{ crossings first: 4. ncrossings } {an OrderedCollection(100->101 202->203 302->303 404->405) . 438}</div><br></div><div><br></div><div><div style="font-size:large">So everything is fine *except for* the extra sample at the beginning.  That's clearly an error, right?</div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex">I'm looking at this..<br>
<br>
Stef<br></blockquote></div><div><br></div><div dir="ltr"><div dir="ltr"><div><span style="font-size:small;border-collapse:separate"><div>_,,,^..^,,,_<br></div><div>best, Eliot</div></span></div></div></div></div></div></div></div></div></div></div></div>
</blockquote></div><br clear="all"><div><br></div>-- <br><div dir="ltr" class="gmail_signature"><div dir="ltr"><div><span style="font-size:small;border-collapse:separate"><div>_,,,^..^,,,_<br></div><div>best, Eliot</div></span></div></div></div></div></div>