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 

Extract raw sound data from mp3?

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



Joined: 26 Mar 2006
Posts: 3

PostPosted: Sun Mar 26, 2006 3:54 pm    Post subject: Extract raw sound data from mp3? Reply with quote

I need to get the raw sound data (actual samples) from an MP3 file. I can get to the ACString data, but I don't know how to get to the raw samples from there. Any suggestions? Thanks in advance.

--david
________
INDIAN RECIPES


Last edited by dmccallie on Wed Mar 09, 2011 7:24 pm; edited 1 time in total
Back to top
View user's profile Send private message
SeB



Joined: 29 Sep 2005
Posts: 3
Location: Germany

PostPosted: Mon Mar 27, 2006 2:57 am    Post subject: Reply with quote

You can get it by unpacking the ACString (to String) with the help of the module struct.
Back to top
View user's profile Send private message Visit poster's website
jbors
Site Admin


Joined: 12 Nov 2004
Posts: 1675

PostPosted: Mon Mar 27, 2006 7:16 am    Post subject: Reply with quote

Try that.
vfr= vc.decode( .... )
samples= str( vfr.data )
Back to top
View user's profile Send private message
dmccallie



Joined: 26 Mar 2006
Posts: 3

PostPosted: Tue Mar 28, 2006 7:53 pm    Post subject: Reply with quote

Thank you. I tried the form str(data.raw) and this did convert the ACString data to regular string data. I then tried to create a Numeric array from the decoded string data using:

vfr = vc.decode(...)
samples= str(vfr.raw)
data_array= Numeric.array(samples, typecode='i') # i = 16bit ints

and this gives me some numbers, but the numbers are all small integers between -127 and +127 (in other words, 8 bit numbers.) This doesn't seem right for sound data.

Could it be that the samples are encoded in a delta-PCM format, or is my Numeric typecode wrong? Is there some other good way to get the samples extracted from the string?

Thank you!!

--david
________
ACCIDENTAL DEATH AND DISMEMBERMENT INSURANCE ADVICE


Last edited by dmccallie on Wed Mar 09, 2011 7:24 pm; edited 1 time in total
Back to top
View user's profile Send private message
dmccallie



Joined: 26 Mar 2006
Posts: 3

PostPosted: Tue Mar 28, 2006 8:39 pm    Post subject: Reply with quote

Nevermind.

The Numeric typecode has to be 's' (short int) With that change, the above code works fine.

--david
________
Los angeles dispensaries


Last edited by dmccallie on Wed Mar 09, 2011 7:24 pm; edited 1 time in total
Back to top
View user's profile Send private message
TheBeege



Joined: 04 Dec 2009
Posts: 3

PostPosted: Fri Dec 04, 2009 4:09 am    Post subject: Umm... what? Reply with quote

Oi, something's just not right for me here. I've been messing with this for hours with little to no luck. Where the hell is decent documentation?!
/rant

How did you get the raw data from the frame? When I tried, neither the decoded frame nor the ACString had a "raw" attribute.
I tried casting ACString to a string using str() with no luck as well. It's still a jarbled mess.

Here's my code...

Code:

# If the file isn't a wav
        if uriSplit[-1].lower() != 'wav':
            # Create a demuxer for the sample's file type if it isn't a wav
            demux = muxer.Demuxer( uriSplit[-1].lower() )
            # Open the sample file
            file = open(uri, 'rb')
            fileData = file.read()
            # Demultiplex the sample file into audio frames
            frames = demux.parse(fileData)
           
            # Create decoder from the demuxer after it has parsed the data
            decoder = acodec.Decoder( demux.streams[0] )
            # Now that we've got info and a decoder, for each frame we've parsed...
            for frame in frames:
                # Get decoded frame to grab sample data from
                decodedFrame = decoder.decode( frame[1] )
                print decodedFrame.data
                stringFrame = str(decodedFrame.data)
                print stringFrame
                if self.sampleRate == 0:
                    # Get and store the file's sample rate
                    self.sampleRate = decodedFrame.sample_rate
                    # Get and store the number of channels in the file
                    self.channels = decodedFrame.channels
                    # Get and store the sample size in bytes
                    self.sampleSize = decodedFrame.bitrate/8
                # Append the samples to the sampleData list
                self.sampleData.append(int(stringFrame))
            print self.sampleData[0] + 'mp3 read correctly'


It spits out a whole bunch of characters like ( from the print statements.

And it returns this exception: ValueError: invalid literal for int() with base 10: ''
That means there's no valid int within the string, correct?

I get the feeling I'm approaching this from the completely wrong direction or something. Can anyone give me a hand?

EDIT: Forgot to mention that I tried using struct's unpack() earlier with no success. I didn't know how to get the correct size. The length of the data of each frame is 27648... something seems wrong here.
Back to top
View user's profile Send private message
jbors
Site Admin


Joined: 12 Nov 2004
Posts: 1675

PostPosted: Sat Dec 05, 2009 2:56 pm    Post subject: Reply with quote

You can generate documentation yourself with pydoc.
As I can see you get the right binary string as a result of decode().
27648 / 2*2 = 6912 audio samples or 153ms of audio data.
Did you check all tutorials and samples out there ?
http://pymedia.org/tut/index.html
Back to top
View user's profile Send private message
TheBeege



Joined: 04 Dec 2009
Posts: 3

PostPosted: Wed Dec 09, 2009 7:36 am    Post subject: Reply with quote

Hey, apologies for the delay in reply. Finals week... ugh.

Ahh, ok. So... Once I grab this sample data as an ACString, do I unpack it 4 bytes at a time, split that into the two channels' data, and then cast each half of that (2 bytes) as ints? Err, I would probably have to decode it 4 bytes at a time then all of those steps. That's all simple assuming this is the correct way to do it.

My goal is to get the sample data into ints so that I can do mathematical operations on them. I've pulled most of my code from the "Decompress mp3 file" tutorial, but all of the tutorials only do PyMedia operations on the data. I need to access the data in a format I can do math on.
Back to top
View user's profile Send private message
jbors
Site Admin


Joined: 12 Nov 2004
Posts: 1675

PostPosted: Wed Dec 09, 2009 11:08 am    Post subject: Reply with quote

I guess you can use Numeric for that purpose and yes you have 4 bytes of data in the raw string which represent 1 sample.
Converting that many samples into Python objects will harm performance real bad Sad
Back to top
View user's profile Send private message
TheBeege



Joined: 04 Dec 2009
Posts: 3

PostPosted: Wed Dec 09, 2009 1:30 pm    Post subject: Reply with quote

Well, I'm writing a procedural remixing application. Feels strange to say, but performance doesn't matter too terribly much. The files being read in will just be samples for mixing, and the time for entire process is expected to be lengthy.

So, just to make sure I have everything understood well, each sample in this particular case has dual channels, providing one sample of 16 bits in size for each channel?

How would I separate these channels? Just read every other 3rd and 4th bytes into a second array? And this is more of a numpy question, but would numpy.frombuffer() be the appropriate function to read this data into an array? It appears the other array construction methods are for specific data types discluding raw byte data.

And I know this isn't related to PyMedia directly, but any idea how I could pass the two channel arrays to the wave.write() in a way that it will work properly?

I apologize for what may be silly, obvious questions. I'm relatively new to working with raw data and audio data.
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