Reading data in WAV file

Gsquared

Member
Licensed User
Longtime User
Hi folks,

I need to read a sound file in wav format that I saved using the AudioRecord library. I can load and play it with the media player, but I need to be able to access the individual values in the wav file for further analysis. I tried "File.ReadList", but got gibberish in the Log window when debugging. Can anyone help me?

Thanks, G^2
 

Gsquared

Member
Licensed User
Longtime User
reading WAV file

kickaha,

Do you mean "InputStream.ReadBytes"? If so, I will have to do some reading on inputstreams - haven't used this function yet.
 
Upvote 0

kickaha

Well-Known Member
Licensed User
Longtime User
Yes, my previous post contained a link to the relevant page in the manual.

Basically you can read the file into an array of bytes so that you can work on the file at byte level.
 
Upvote 0

Gsquared

Member
Licensed User
Longtime User
reading WAV file

Here's the code fragment I tried for reading the WAV file:


Dim buffer() As Byte
Dim InputStream1 As InputStream

File.OpenInput(File.DirDefaultExternal, "test.wav")
InputStream1.ReadBytes(buffer,44,buffer.Length)
Log("First"&buffer(0))
Log("Second"&buffer(1))

I received a runtime error indicated that I need to initialize the object in the ReadBytes line. I'm not sure how to do this.
 
Upvote 0

poseidon

Member
Licensed User
Longtime User
re

'Reads a file to byte array.
B4X:
Sub ReadAllBytes(folder As String , filename As String ) As Byte()
   Try 
      Dim in As InputStream
      in = File.OpenInput(folder,filename)
      Dim out As OutputStream
      out.InitializeToBytesArray(1)
      File.Copy2(in, out)

      Dim data() As Byte
      data = out.ToBytesArray
      
      out.Close
      in.Close 
      out.Flush
      
      Return data 
   Catch 
      Return Null 
   End Try 
End Sub
 
Upvote 0

Gsquared

Member
Licensed User
Longtime User
reading WAV file

Thanks Poseidon!

That seems to work. I just made one change to your Sub - I moved the "Dim data() As Byte" statement to "Process_Globals". If I understand the Sub correctly, the WAV file must only contain byte data, right? What about skipping the header on WAV files with one?
 
Upvote 0

poseidon

Member
Licensed User
Longtime User
re

nah, this is a ^function.. so

B4X:
Sub Button1_Click
dim wav() as byte

'here set the bytes of file to local variable wav
wav = ReadAllBytes(files.external,"test.wav")

'here loop the byte array (aka wav file)

for i=0 to wav.length
'what you want to do..(?)
'to read the byte just
'if wav(i) = 

next 

End Sub


according to this
Ring This... Wave File Format

first you have to read the chunksize (size of WAVE section chunck) then make a loop equal to chunksize, is a little bit complex if you havent 'play' with bytes again...
 
Upvote 0
Top