Writing WAV file using Python, Numpy array and WAVE module
<p>I am trying to implement the Karplus-Strong algorithm.</p>

<p>All is looking fine when I play (through Jupyter Notebook using <strong>Audio(y, rate=Fs)</strong>) the collected numpy array (representing guitar accord).</p>

<p>Unfortunately, writing the numpy array: <strong>y</strong>, into wav file using WAVE module is incorrect (using the next python code):</p>

<pre><code>noise_output = wave.open('k-s.wav', 'w')
noise_output.setparams((1, 4, Fs, 0, 'NONE', 'not compressed'))

for i in range(0, len(y)):
     value = y[i]
     packed_value = struct.pack('f', value)
     noise_output.writeframes(packed_value)

noise_output.close()
</code></pre>

<p>Each element of <strong>y</strong> is </p>

<pre><code>&lt;type 'numpy.float64'&gt;
</code></pre>

<p>How should I amend the writing loop in order write the WAV file correctly? </p>

<p>Some more information about the issue. Before writing to WAV, the first elements of the y array are:</p>

<pre><code> [ 0.33659756  0.33659756 -0.43915295 -0.87036152  1.40708988  0.32123558
-0.6889402   1.9739982  -1.29587159 -0.12299964  2.18381762  0.82228042
 0.24593503 -1.28067426 -0.67568838 -0.01843234 -1.830472    1.2729578
-0.56575346  0.55410736]
</code></pre>

<p>After writing the elements to the WAV file, close the WAV file and read it again, I got this for the first 20 elements of the collected array:</p>

<pre><code>[ 1051481732  1051481732 -1092560728 -1084305405  1068768133  1050966269
 -1087349149  1073523705 -1079648481 -1107564740  1074512811  1062371576
  1048303204 -1079775966 -1087571478 -1130954901 -1075163928  1067642952
 -1089415880  1057872379]
</code></pre>