WAV files being saved in wrong order
<p>I have a program that loops over a for loop, and saves sine waves as wav files to create a range of tones for a keyboard. The loop goes from -int to +int, and saves these WAV files in each iteration. The only problem is that when it gets to around x = 2, the file order is messed up. I don't know why this happens or how to fix it.</p>

<pre><code>out_int = 0

for x in xrange(-43, 47, 1):   
    CFreq = CFreq * 2**(x/12.)                                 
    out_int += 1

    .
    .
    .
        wavdata    = np.zeros(len(data), np.int16)
        wavdata[:] = data / max(data) * 2**15

        wavfile = wave.open("WavFiles/Modes_%02d_.wav" % (out_int), "wb")
</code></pre>

<p>Theres a lot of data going into these files, which is why I'm guessing it takes longer to produce some notes and save it before saving the previous samples.
The output frequency for each sample is correct but they sound off.</p>

<p>Is there anyway to make sure the code writes and saves each file first before going to the next? I tried changing the order for the loop to go from positive to negative values, but then the samples at the negative iteration were being messed up.</p>

<p>There is not enough code to replicate, However, here is an example using enumerate that will put the subsequent xrange elements in order. Perhaps this will aid in keeping your wavefiles in sequence. </p>

<pre><code>for i, x in enumerate(xrange(-43, 47, 1)):    
    file_name = "file{0}-{1}.wav".format(i, x)
    print file_name
</code></pre>

<p>Output:</p>

<pre><code>Python 2.7.9 (default, Dec 10 2014, 12:24:55) [MSC v.1500 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
&gt;&gt;&gt; ================================ RESTART ================================
&gt;&gt;&gt; 
file0--43.wav
file1--42.wav
file2--41.wav
file3--40.wav
|
|
file86-43.wav
file87-44.wav
file88-45.wav
file89-46.wav
</code></pre>

python for-loop audio wav