[Python] Try to analyze wav files (Ver without additional plugins)

The default GUI kit that comes with Python, Let's draw the wav waveform by force using tkInter's canvas.

There are no additional plugins required. All you have to do is prepare Python and the sound source you want to play.

python


#!/usr/bin/env python
# -*- coding: utf8 -*-
import sys
import Tkinter
import wave
import numpy as np

window_width = 600
window_height = 500

root = Tkinter.Tk()
root.title(u"Software Title")
root.geometry(str(window_width) + "x" + str(window_height))

#Read wav data
wav = wave.open("./test.wav")

#Move to the beginning of the audio part
wav.rewind()

#Binary reading
wavdata = wav.readframes(wav.getnframes())

#Convert to int
wavdata = np.frombuffer(wavdata,'int16')


#
#Canvas area
#
canvas = Tkinter.Canvas(root, width = window_width, height = window_height)

#Number of X-axis steps
step = float(window_width)/float(wav.getnframes())
x = 0			#X axis
b_i = 0			#Previous value

for c,i in enumerate(wavdata):
	#Keep the previous coordinates
	if (c%2 == 0):
		b_i = i
		x = x + step
		continue
	
	#Create a waveform graph using the previous coordinates and the current coordinates
	canvas.create_line(int(x), (b_i/window_height)+(window_height/2), int(x+step), (i/window_height)+(window_height/2), fill = "blue")
	
	#Advance the X coordinate by step
	x = x + step
	print "(x,y) = ("+ str(x) +","+ str(i) +")"

#center lane
canvas.create_line(0, window_height/2, window_width, window_height/2, fill = "black")


#
#Canvas bind
#
canvas.place(x=0,y=0)


#
#close wav
#
wav.close()

root.mainloop()

スクリーンショット 2015-06-02 14.44.40.png

Although it is rough, it is output like this.

Recommended Posts

[Python] Try to analyze wav files (Ver without additional plugins)
Try to analyze Twitter trends
Try to understand Python self
[Python] Use this to read and write wav files [wavio]
Try to analyze online family mahjong using Python (PART 1: Take DATA)
How to play music (wav / mp3) files on Raspberry Pi python
Try to operate Facebook with Python
[Python] POST wav files with requests [POST]
MP3 to WAV conversion with Python
Try to calculate Trace in Python
Try converting cloudmonkey CLI to python3 -1
Put Cabocha 0.68 on Windows and try to analyze the dependency with Python
Try logging in to qiita with Python
I want to analyze logs with Python
Try to operate Excel using Python (Xlwings)
Python amateurs try to summarize the list ①
Convert HEIC files to PNG files with Python
[Python] Add comments to standard input files