wav2plot.py 860 B

12345678910111213141516171819202122232425262728293031
  1. from os.path import dirname, join as pjoin
  2. from scipy.io import wavfile
  3. import scipy.io
  4. #data_dir = pjoin(dirname(scipy.io.__file__), 'tests', 'data')
  5. wav_fname = pjoin('Mozart-Alla-turca-from-Piano-Sonata-No.-11-in-A-Major-K.-331.wav')
  6. #print(wav_fname[:-4])
  7. # Load the .wav file contents.
  8. samplerate, data = wavfile.read(wav_fname)
  9. print(f"number of channels = {data.shape[0]}")
  10. length = data.shape[0] / samplerate
  11. print(f"length = {length}s")
  12. # Plot the waveform.
  13. import matplotlib.pyplot as plt
  14. import numpy as np
  15. time = np.linspace(0., length, data.shape[0])
  16. plt.plot(time, data[:,0])#, label="Left channel")
  17. #plt.plot(time, data[:, 1], label="Right channel")
  18. #plt.legend()
  19. plt.xlabel("Time [s]")
  20. plt.ylabel("Amplitude")
  21. plt.title(f'{wav_fname[:-4]}')
  22. plt.show()
  23. #plt.savefig("{}.png".format(wav_fname[:-4]), dpi=300)