#!python # -*- mode: python; Encoding: utf-8; coding: utf-8 -*- # Last updated: <2018/12/05 18:18:26 +0900> """ use pydub. divide wav file. 【Python/pydub】mp3、wavファイルから部分抽出(切り分け、分割) | アルゴリズム雑記 https://algorithm.joho.info/programming/python/pydub-split/ 【Python/pydub】mp3、wavのデータをNumPy配列に変換 | アルゴリズム雑記 https://algorithm.joho.info/programming/python/pydub-numpy/ Windows10 x64 + Python 2.7.15 32bit """ from pydub import AudioSegment from pydub.playback import play import numpy as np import matplotlib.pyplot as plt infile = "hello.wav" sound = AudioSegment.from_wav(infile) # play(sound) data = np.array(sound.get_array_of_samples()) x = data[::sound.channels] rate = sound.frame_rate frm = rate / 60 sample_len = len(x) print("Input file : %s" % infile) print("Channel : %d" % sound.channels) print("Sampling rate : %d Hz" % rate) print("1 Frame length : %f point" % frm) print("Duration : %f msec" % len(sound)) print("Sample length : %f point" % sample_len) dt = [] for i in range(0,sample_len,frm): dt.append(x[i:i+frm]) if True: # plt.plot(x[::1]) row = 10 for i in range(row): plt.subplot(row, 1, i + 1) plt.plot(dt[i]) plt.grid() plt.show()