0

I have a sequence generated by an LSTM which I'm trying to save to disk as an audio file. I tried several libraries but none of them seem to produce working audiofiles.

The sequence I'm working with are float values between -1 and 1.

Example: [ 0.1772334 0.1772334 0.1772334 ... -0.1110365 -0.1110365 -0.1110365]

Using sd.play() works fine (my sequence is just some white noise for now). But the files written to disk do not work, none of these work. And sd.playrec() throws an error.

import numpy as np
import sounddevice as sd
import soundfile as sf
import librosa
import scipy

seq = np.load("Sequence.npy")

#Works
sd.play(seq, samplerate = 4000, blocking = True)

#Doesn't work
librosa.output.write_wav('LibrosaSequence.wav', seq, sr = 4000, norm = [-1,1])
scipy.io.wavfile.write('ScipySequence.wav', 4000, seq)

file4000 = sd.playrec(seq, samplerate = 4000, channels = 2, blocking = True)
sf.write("file4000.wav", file4000, sampling_frequency = 4000)

Ahmad Moussa
  • 876
  • 10
  • 31
  • what is the error ? – PV8 Jun 17 '19 at 06:15
  • did you check this: https://stackoverflow.com/questions/10357992/how-to-generate-audio-from-a-numpy-array ? – PV8 Jun 17 '19 at 06:15
  • @PV8 So I should scale the floats to integers? since the scipy function only works for integers? What If I don't want to lose precision? The error is: `sounddevice.PortAudioError: Error querying device -1` – Ahmad Moussa Jun 17 '19 at 06:17
  • 1
    for this you can try: `scikits.audiolab`,it is also in the post – PV8 Jun 17 '19 at 06:18
  • Alright! Thank you, I'll try it and if it works I'll delete my question. – Ahmad Moussa Jun 17 '19 at 06:19
  • @AhmadMoussa If it answers your question, then it should be made into a proper answer. Questions should not be deleted after you got a working solution for them. The point of SO is that others can learn as well. `scipy.io.wavfile.write` should be indeed scaled to integers, `librosa` will do that for you. What do you mean by saying it doesn't work? Can you open the output file in any music player? – Lukasz Tracewski Jun 17 '19 at 06:31
  • By saying it doesn't work, I mean that it won't open in any music player. I said I would delete, since this is a duplicate, if another question already answered this. I am still figuring out if the other answer solves my problem. – Ahmad Moussa Jun 17 '19 at 06:43
  • Your error seems to be related to audio playback, and not related to writing WAV files. Can you please remove all of the audio playback code and check what the error is then? – Nils Werner Jun 17 '19 at 07:14
  • scikits.audiolab throws a lot of errors for me using python 3.7 and I don't have a lot of time to waste on debugging. This being one of them https://stackoverflow.com/questions/55144034/scikits-audiolab-modulenotfounderror-no-module-named-pysndfile @NilsWerner ok I will try your suggestion – Ahmad Moussa Jun 17 '19 at 07:21
  • For the scipy function, double check that the values in `seq` are between -1 and 1, and convert the data to single precison (i.e. `numpy.float32`) before writing, i.e. `scipy.io.wavfile.write('ScipySequence.wav', 4000, seq.astype(np.float32))` – Warren Weckesser Jun 17 '19 at 14:03
  • I double checked, and still doesn't work. Is there a minimum length my sequence has to be? Currently it's 16000 samples, so roughly a second of audio. – Ahmad Moussa Jun 18 '19 at 05:03

0 Answers0