audio.py 700 B

123456789101112131415161718192021222324252627
  1. import wave
  2. def combine_audio_files(files: list, outfile: str = "sounds.wav"):
  3. first_file = files[0]
  4. for i in range(len(files)-1):
  5. combine_two_files([first_file, files[i+1]], outfile)
  6. first_file = outfile
  7. def combine_two_files(files: list, outfile: str = "sounds.wav"):
  8. data= []
  9. for infile in files:
  10. try:
  11. w = wave.open(infile, 'rb')
  12. except:
  13. w = wave.open('.empty.vav', 'rb')
  14. data.append( [w.getparams(), w.readframes(w.getnframes())] )
  15. w.close()
  16. output = wave.open(outfile, 'wb')
  17. output.setparams(data[0][0])
  18. output.writeframes(data[0][1])
  19. output.writeframes(data[1][1])
  20. output.close()