Hello. I’m using pygame. I need code example which fetch different parameters(frequency, volume, … , other) from mp3 in real time moment and draw it the window of pygame.
And so I’m gonna get:
MyArray[x][y]
where [x]: is a for e.g. frequency.
and [y]: is a value, but in real time.
, like in visual equalizer implementation in most popular audio players under windows platform. But I don’t know which parameters consist of mp3 audio file and how to make drawing chart from that. Help me please. It have to making like this but I need also above talking MyArray[x][y]
:
Could S.o.m.e.o.n.e. help me?
You aren’t likely to find someone who will do it for you. Try to do it yourself, and ask for help if you get stuck.
I found something, but don’t understand how to draw a my own chart (just Cartesian Diagram with (x,y)). Can you help me to modify this code for that purposes.:
import sys, math, wave, numpy, pygame
from pygame.locals import *
from scipy.fftpack import dct
import random
Number = 30 # number of bars
HEIGHT = 600 # HEIGHT of a bar
WIDTH = 40 #WIDTH of a bar
FPS = 10
file_name = sys.argv[0]
status = 'stopped'
fpsclock = pygame.time.Clock()
#screen init, music playback
pygame.init()
pygame.mixer.init()
screen = pygame.display.set_mode([Number * WIDTH, 50 + HEIGHT])
pygame.display.set_caption('Audio Visualizer')
my_font = pygame.font.SysFont('consolas', 16)
pygame.mixer.music.load("0.wav")
pygame.mixer.music.play()
pygame.mixer.music.set_endevent()
pygame.mixer.music.set_volume(0.2)
status = "Playing"
#process wave data
f = wave.open("0.wav", 'rb')
params = f.getparams()
nchannels, sampwidth, framerate, nframes = params[:4]
str_data = f.readframes(nframes)
f.close()
wave_data = numpy.fromstring(str_data, dtype = numpy.short)
wave_data.shape = -1, 2
wave_data = wave_data.T
num = nframes
def Visualizer(nums):
num = int(nums)
h = abs(dct(wave_data[0][nframes - num:nframes - num + Number]))
h = [min(HEIGHT, int(i**(1 / 2.5) * HEIGHT / 100)) for i in h]
draw_bars(h)
def vis(status):
global num
if status == "stopped":
num = nframes
return
elif status == "paused":
Visualizer(num)
else:
num -= framerate / FPS
if num > 0:
Visualizer(num)
def get_time():
seconds = max(0, pygame.mixer.music.get_pos() / 1000)
m, s = divmod(seconds, 60)
h, m = divmod(m, 60)
hms = ("%02d:%02d:%02d" % (h, m, s))
return hms
def controller(key):
global status
if status == "stopped":
if key == K_RETURN:
pygame.mixer_music.play()
status = "playing"
elif status == "paused":
if key == K_RETURN:
pygame.mixer_music.stop()
status = "stopped"
elif key == K_SPACE:
pygame.mixer.music.unpause()
status = "playing"
elif status == "playing":
if key == K_RETURN:
pygame.mixer.music.stop()
status = "stopped"
elif key == K_SPACE:
pygame.mixer.music.pause()
status = "paused"
def draw_bars(h):
bars = []
for i in h:
bars.append([len(bars) * WIDTH , 50 + HEIGHT - i, WIDTH - 1, i])
for i in bars:
pygame.draw.rect(screen, ChangeColor(0,1,1), i, 0)
# pygame.draw.rect(screen, ChangeColor(255,1,1), 1, 0)
def ChangeColor(AA,OO,UU):
if AA==1:
AA= random.randint(0, 255)
if OO==1:
OO= random.randint(0, 255)
if UU==1:
UU= random.randint(0, 255)
return (AA,OO,UU)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
elif event.type == KEYDOWN:
controller(event.key)
if num <= 0:
status = "stopped"
name = my_font.render(file_name, True, ChangeColor(255,255,255))
info = my_font.render(status.upper() + "" + get_time(), True, ChangeColor(255,255,255))
screen.fill((0,0,0))
screen.blit(name,(0,0))
screen.blit(info,(0, 18))
fpsclock.tick(FPS)
vis(status)
pygame.display.update()
for event in pygame.event.get():
#print(event)
if event.type == pygame.QUIT:
print('Application Closed')
pygame.quit()
quit()
if event.type == pygame.MOUSEWHEEL:
if event.y==1:
print("Pressed Up!")
break
elif event.y==-1:
print("Pressed Down!")
break