InDashPC.org Forum Index InDashPC.org
In Dash Car PC forum
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Waveform from mp3

 
Post new topic   Reply to topic    InDashPC.org Forum Index -> Software
View previous topic :: View next topic  
Author Message
rajah



Joined: 03 Jun 2010
Posts: 2

PostPosted: Thu Jun 03, 2010 1:06 am    Post subject: Waveform from mp3 Reply with quote

Hi, I'm trying to produce a graphical waveform out of an mp3 file and I am having some problems, the main problem is that the waveform created doesn't look like the one from the audio editor. The other problem is how slow the process takes. Eventually I would like to be able to scroll and zoom, but I think that I would need to send all the raw data to an array to do that. I'm pretty much looking for a more efficient way to go about this, any suggestions?

Code:
import sys
import pygame
from pygame.locals import *
import pymedia.audio.acodec as acodec
import pymedia.audio.sound as sound
import pymedia.muxer as muxer
import Numeric

WIDTH = 800
HEIGHT = 600

def main(mp3):
    pygame.init()
    screen = pygame.display.set_mode((WIDTH,HEIGHT))
    pygame.display.set_caption("MP3")
    bg = pygame.Surface(screen.get_size()).convert()
    bg.fill((0,0,0))
    screen.blit(bg,(0,0))
    dm = muxer.Demuxer('mp3')
    f = open(mp3,'rb')
    s= " "
    dec = None
    i = 0
    xIndex = 0;
    center = HEIGHT/2
    scaleFactor = 1.5
    data = []
    while len( s ):
        s= f.read()
        print len(s)
        if len(s):
            frames = dm.parse(s)
            for fr in frames:
                i+=1
                print "frame "+str(i)
                if dec == None:
                    dec= acodec.Decoder(dm.streams[0])
                r = dec.decode(fr[1])
                if r and r.data:
                    increment = r.bitrate*len(frames)/WIDTH
                    s=str(r.data)
                    data = Numeric.array(s,typecode='s')
                    ii=0
                    while ii<len(data):
                        d = data[ii]
                        y = center + d * scaleFactor
                        ny = center - d * scaleFactor
                        pygame.draw.line(bg,(255,255,min(xIndex,255)),(xIndex,ny),(xIndex,y))
                        xIndex+=1
                        screen.blit(bg,(0,0))
                        pygame.display.flip()
                        ii += increment
           
    screen.blit(bg,(0,0))
    pygame.display.flip()
            # Get fft info as bands
    while 1:
        for event in pygame.event.get():
            if event.type == QUIT:
                return False
            if (event.type == KEYUP) or (event.type == KEYDOWN):
                if (event.key == K_ESCAPE):
                    return False

if __name__ == "__main__":
    if len(sys.argv)<=1:
        print "Error: mp3 file needed"
        sys.exit()
    elif str(sys.argv[1]).split(".")[-1] != "mp3":
        print "Error: only mp3 files are accepted"
        sys.exit()
    main(sys.argv[1])
Laughing
Back to top
View user's profile Send private message
jbors
Site Admin


Joined: 12 Nov 2004
Posts: 1675

PostPosted: Sun Jun 06, 2010 7:52 pm    Post subject: Reply with quote

Here is the pygame example for audio player.
It uses sound.SpectrAnalyzer which is part of pymedia



sound_viz.zip
 Description:

Download
 Filename:  sound_viz.zip
 Filesize:  2.16 KB
 Downloaded:  1207 Time(s)

Back to top
View user's profile Send private message
rajah



Joined: 03 Jun 2010
Posts: 2

PostPosted: Fri Jun 11, 2010 10:43 am    Post subject: example Reply with quote

Have you ever actually run this example, it doesn't work for me. And just to be sure, will this give me the the waveform data or the frequency bands?
Back to top
View user's profile Send private message
jbors
Site Admin


Joined: 12 Nov 2004
Posts: 1675

PostPosted: Fri Jun 11, 2010 12:51 pm    Post subject: Reply with quote

That example worked some time back.
This example is giving the same visualization as Winamp for example.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    InDashPC.org Forum Index -> Software All times are GMT - 8 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You can attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group