Python PyAudio, output little cracky. Maybe math
<p>Good day. I have a small problem that may be partly math.</p>

<p>The thing is I want to play Sine wave without fixed frequency. Therefore, not to make the sound cracky between transitions or during fixed frequency i need the sine wave to start and to end with amplitude zero. Mathematicly I understand what has to be done. </p>

<p>I chosed a way, where I adapt 'time' of the sine wave so it has time to finish all cycles. Basicly y=sin(2*pi<em>f</em>t) where f*t must be whole number.</p>

<p>The problem is that it actually works but not fully. All waves end up very near to zero, but not exactly there. Sound is quite ok while changing frequency but not perfect. I cant figure out why the last element cant land on zero. </p>

<p>If you would go through it and check i would be really greatful. Thx</p>

<pre><code>            import pyaudio
            import numpy as np
            import matplotlib.pyplot as plt

            p = pyaudio.PyAudio()
            volume = 0.5     # range [0.0, 1.0]
            fs = 44100*4       # sampling rate, Hz, must be integer
            time = 0.1  # in seconds, may be float
            f = 400        # sine frequency, Hz, may be float
            k = np.arange(int(time*fs))
            t=np.arange(0,time,1/fs)
            start=0
            end=time




            stream = p.open(format=pyaudio.paFloat32,
                            channels=1,
                            rate=fs,
                            output=True)



            # generate samples, note conversion to float32 array
            for i in range(1000):

                start = 0
                end = 40 / f #time to acomplish whole whole cycles according to the give frequency - must be whole number

                print(len(t))
                t = np.arange(start, end, 1 / fs)
                samples = (np.sin(2*np.pi*f*t)).astype(np.float32)

                print(samples[0],samples[-1]) # The main problem. I need first and last elements in the sample to be zero.
                                            # Problem is that last element is only close to zero, which make the sound not so smooth

                #print(start+i,end+i)
                #print(samples)  # # # # # Shows first and last element

                f+=1

                # for paFloat32 sample values must be in range [-1.0, 1.0]


            # play. May repeat with different volume values (if done interactively)
                stream.write(volume*samples)

            stream.stop_stream()
            stream.close()

            p.terminate()
</code></pre>

<p>The sine function repeats itself every multiple of <code>2*pi*N</code> where N is a whole number. IOW, <code>sin(2*pi) == sin(2*pi*2) == sin(2*pi*3)</code> and so on. </p>

<p>The typical method for generating samples of a particular frequency is <code>sin(2*pi*i*freq/sampleRate)</code> where <code>i</code> is the sample number.</p>

<p>What follows is that the sine will only repeat at values of <code>i</code> such that  <code>i*freq/sampleRate</code> is exactly equal to a whole number (I'm disregarding phase offsets).</p>

<p>The net result is that some frequency/sampleRate combinations may repeat after only a single cycle (1kHz @ 48kSr) whereas others may take a very long time to repeat (997Hz @ 48kSr).</p>

<p>It is not necessary that you change frequencies at exact zero crossings in order to avoid glitches. A better approach is this:</p>

<ol>
<li>Compute the phase increment for the desired frequency as <code>phaseInc = 2*pi*freq/sampleRate</code></li>
<li>For each output sample compute the output sample from the current phase. <code>y = sin(phase)</code></li>
<li>Update the phase by the phase increment: <code>phase += phaseInc</code></li>
<li>Repeat 2-3 for the desired number of samples.</li>
<li>Goto step one to change freq</li>
</ol>

<p>If you are insistent on changing at a zero crossing, just do it at the nearest sample where the phase crosses a multiple of 2*pi. </p>

python audio wave pyaudio sine