<div dir="ltr"><div dir="ltr"><div dir="ltr"><div class="gmail_default" style="font-size:large">Hi Stef, Hi Levente,</div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Sat, Sep 19, 2020 at 2:27 AM Stéphane Rollandin <<a href="mailto:lecteur@zogotounga.net">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">Ok, it looks like your choice of frequency let a bug surface.<br>
<br>
In SampledSound>>#reset, the computation of scaledIncrement depends on <br>
the sampling rate. But scaledIncrement is also supposed to be somewhat <br>
commensurate with ScaledIndexOverflow which has a fixed value set in the <br>
class-side initialize.<br>
<br>
So I guess the whole LargePositiveIntegers avoidance scheme is bogus. If <br>
you get rid of it altogether, which in <br>
SampledSound>>#mixSampleCount:into:startingAt:leftVol:rightVol: <br>
translates as follow (primitive uncommented):<br></blockquote><div><br></div><div class="gmail_default" style="color:rgb(0,0,0);font-size:large"></div><div class="gmail_default" style="color:rgb(0,0,0);font-size:large"></div><div><span style="color:rgb(0,0,0);font-size:large">Stef, thanks for looking at the overflow arithmetic.  In 64-bits we may not need it at all, and so I could add an ifTrue:ifFalse: based on the value of SmallInteger maxVal that would avoid the complex arithmetic on 64-bits.  What do you think?</span></div><div> </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">-----<br>
<br>
mixSampleCount: n into: aSoundBuffer startingAt: startIndex leftVol: <br>
leftVol rightVol: rightVol<br>
        "Mix the given number of samples with the samples already in the given <br>
buffer starting at the given index. Assume that the buffer size is at <br>
least (index + count) - 1."<br>
<br>
        | lastIndex outIndex sample i s |<br>
"       <primitive:'primitiveMixSampledSound' module:'SoundGenerationPlugin'><br>
        <var: #aSoundBuffer declareC: 'short int *aSoundBuffer'><br>
        <var: #samples declareC: 'short int *samples'><br>
"<br>
        lastIndex := (startIndex + n) - 1.<br>
        outIndex := startIndex.    "index of next stereo output sample pair"<br>
<br>
        [(outIndex <= samplesSize) and: [outIndex <= lastIndex]] whileTrue: [<br>
                sample := ((samples at: outIndex) * scaledVol) // ScaleFactor.<br>
                leftVol > 0 ifTrue: [<br>
                        i := (2 * outIndex) - 1.<br>
                        s := (aSoundBuffer at: i) + ((sample * leftVol) // ScaleFactor).<br>
                        s >  32767 ifTrue: [s :=  32767].  "clipping!"<br>
                        s < -32767 ifTrue: [s := -32767].  "clipping!"<br>
                        aSoundBuffer at: i put: s].<br>
                rightVol > 0 ifTrue: [<br>
                        i := 2 * outIndex.<br>
                        s := (aSoundBuffer at: i) + ((sample * rightVol) // ScaleFactor).<br>
                        s >  32767 ifTrue: [s :=  32767].  "clipping!"<br>
                        s < -32767 ifTrue: [s := -32767].  "clipping!"<br>
                        aSoundBuffer at: i put: s].<br>
<br>
                scaledVolIncr ~= 0 ifTrue: [<br>
                        scaledVol := scaledVol + scaledVolIncr.<br>
                        ((scaledVolIncr > 0 and: [scaledVol >= scaledVolLimit]) or:<br>
                         [scaledVolIncr < 0 and: [scaledVol <= scaledVolLimit]])<br>
                                ifTrue: [  "reached the limit; stop incrementing"<br>
                                        scaledVol := scaledVolLimit.<br>
                                        scaledVolIncr := 0]].<br>
<br>
                outIndex := outIndex + 1].<br>
        count := count - n.<br>
<br>
-----<br>
<br>
then you get a nice sine wave (which still play badly in my Squeak image <br>
but now only because it is too slow to generate without a primitive).<br>
<br>
Stef<br>
<br>
</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>