Converting Binary file to file of 2-byte integers

Visuals, audio, animation. Blended, not stirred. If LiveCode is part of your rich media production toolbox, this is the forum for you.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
peter_lboro
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 23
Joined: Mon Mar 20, 2006 10:32 pm
Location: Loughborough, UK
Contact:

Converting Binary file to file of 2-byte integers

Post by peter_lboro » Wed Oct 19, 2022 2:35 pm

I'm trying to understand how binaryDecode works. In particular I have the data from a WAV audio file. The data is a collection of 1-byte values. However, the data should be treated as a collection of 2-byte unsigned integers. I'm trying to use binaryDecode to convert the WAV data to a collection of sound samples, each 2-bytes long.

I tried the following:

put empty into tIntegerData
put binaryDecode("S", tBinaryData, tIntegerData) into tHowManyConverts

at best I get tHowManyConverts = 1 and tIntegerData = empty. I have experimented with "S" being "S2" and "S*" etc

Now I'm stuck!

Peter
--
Peter Reid
Loughborough, UK

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9670
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Converting Binary file to file of 2-byte integers

Post by dunbarx » Wed Oct 19, 2022 3:12 pm

Hi.

Hmmm. What is in the variable "tBinaryData"?

This function requires that all variable be previously either declared or loaded. It will not do so on the fly.

Craig

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9670
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Converting Binary file to file of 2-byte integers

Post by dunbarx » Wed Oct 19, 2022 3:36 pm

I never used that function before, and I do not want to distract from the issue of the OP, but why does this give me a "1" instead of "B"?

Code: Select all

on mouseUp
   put binaryDecode("h","12",temp)
   answer temp
end mouseUp
Craig

peter_lboro
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 23
Joined: Mon Mar 20, 2006 10:32 pm
Location: Loughborough, UK
Contact:

Re: Converting Binary file to file of 2-byte integers

Post by peter_lboro » Wed Oct 19, 2022 3:43 pm

the value is the number of conversions completed, not the values

Peter
--
Peter Reid
Loughborough, UK

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9842
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

Re: Converting Binary file to file of 2-byte integers

Post by FourthWorld » Wed Oct 19, 2022 5:03 pm

i do not get empty. What's in tBinaryData?
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9670
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Converting Binary file to file of 2-byte integers

Post by dunbarx » Wed Oct 19, 2022 6:49 pm

@Peter.

But the dictionary states
binaryDecode("h","M",theHex) -- converts M to its hex equivalent
but also
The binaryDecode function returns the number of dataTypes that were successfully converted and placed in variables (not counting data skipped by the x dataType). The actual data is placed in the variables, rather than returned by the function.
So I see what you meant. But doesn't at least the first quote above have the meaning it seems to?

@Klaus or Richard, if you think this side issue should be moved to its own thread, feel free.

Craig

peter_lboro
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 23
Joined: Mon Mar 20, 2006 10:32 pm
Location: Loughborough, UK
Contact:

Re: Converting Binary file to file of 2-byte integers

Post by peter_lboro » Wed Oct 19, 2022 11:15 pm

The WAV file I'm trying to analyse consists of a header of 44 bytes followed by a stream of bytes representing the sound samples. I picture this as follows:

WAV :[---header (44 bytes) ---][---------------- samples (thousands bytes) ---------------]
| |
| |
V V
tHeader tBinaryData
|
(1) binaryDecode("S", tBinaryData, tIntegerData) --> tConversions
|
|
V
(2) tIntegerData: [2-bytes][2-bytes][2-bytes]......[2-bytes][2-bytes]

Note (1) seems to work except that the output is tConversions = 1 and tIntegerData = 0, so (2) looks like the following instead:
(3) tIntegerData: [2-bytes (0)]
Does this make things clearer?

Peter
--
Peter Reid
Loughborough, UK

peter_lboro
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 23
Joined: Mon Mar 20, 2006 10:32 pm
Location: Loughborough, UK
Contact:

Re: Converting Binary file to file of 2-byte integers

Post by peter_lboro » Thu Oct 20, 2022 8:54 am

Any clearer?

The WAV file I'm trying to analyse consists of a header of 44 bytes followed by a stream of bytes representing the sound samples. I need to convert the X thousand bytes into X/2 thousand integers (each integer is 2-byte unsigned). I picture this as follows:
Attachments
binaryDecode.png
--
Peter Reid
Loughborough, UK

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 9388
Joined: Fri Feb 19, 2010 10:17 am
Location: Bulgaria

Re: Converting Binary file to file of 2-byte integers

Post by richmond62 » Thu Oct 20, 2022 8:59 am

Maybe I' a bit slow, but . . .

am I right in understanding you have numbers that look a bit like this:

9856431238765432859421986756487907653123456789076543123456789098765432123243546576876543

where the first 44 digits are the header, and what follows has to be decoded?

If that is so, then, surely the first thing you need to do is to 'nip off' the header.

Having done that, as you are dealing with 2 digit 'chunks' don't you have to set up a repeat loop
to chew its way along the remaining number line, converting each pair of numbers as it goes and "spitting out"
the results as a series of 2-byte files.

What format do you want the 2-byte files to be in?

If the initial 44 digits of the original WAV file are the standard WAV header I assume you will have to
add them on the front of each 2-byte file.

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 9388
Joined: Fri Feb 19, 2010 10:17 am
Location: Bulgaria

Re: Converting Binary file to file of 2-byte integers

Post by richmond62 » Thu Oct 20, 2022 9:27 am

As has been previously pointed out, I assume (?) that your number string is present in your working
stack in a field.

I am interested to know how you obtained that number string.

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4003
Joined: Sun Jan 07, 2007 9:12 pm
Location: Bochum, Germany

Re: Converting Binary file to file of 2-byte integers

Post by bn » Thu Oct 20, 2022 2:01 pm

Hi Peter,

is this what you want to do?

Code: Select all

on mouseUp
   local tWav, tHeader, tData, tLengthFile, tResult, tBinary, tBinaryLength
   local tInstruct, tNumLines, tDecoded, tSkipped
   
   answer file "load a WAV" with type "Waveform-Audio|wav|WAV"
   if it is empty then exit mouseUp
   
   put url ("binfile:" & it) into tWav
   put the length of tWav into tLengthFile
   
   -- get binary data without header
   put byte 45 to -1 of tWav into tBinary
   put the length of tBinary into tBinaryLength
   
   -- tHeader will contain the headerData as ASCII
   put binaryDecode("a44",tWav,tHeader) into tResult
   
   -- get first instance of unsigned integer, tData will contain the unsigned integers
   put binaryDecode(("S"),tBinary, tData) into tResult
   put tData & cr after tDecoded
   
   -- get rest of unsigned integers, use "x & i" to skip 
   repeat with i = 2 to tBinaryLength - 2 step 2
      put "x" & i & "S" into tInstruct
      put binaryDecode((tInstruct ),tBinary,tSkipped, tData) into tResult
      put tData & cr after tDecoded
   end repeat
   delete char -1 of tDecoded -- return
   
   ## tDecoded contains unsigned integers, one per line
   
   put the number of lines of tDecoded into tNumLines -- should be half of the length of binary data
end mouseUp
I tried with the attached sample WAV
Kind regards
Bernd

Edit: I corrected this script because as Richard pointed out I was just putting the file path and not the actual file into the variable tWav.
Attachments
The_Dogcow.moof.wav.zip
(8.83 KiB) Downloaded 90 times
Last edited by bn on Thu Oct 20, 2022 7:36 pm, edited 1 time in total.

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9842
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

Re: Converting Binary file to file of 2-byte integers

Post by FourthWorld » Thu Oct 20, 2022 7:24 pm

Does tWav contain the filename or the file contents?
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4003
Joined: Sun Jan 07, 2007 9:12 pm
Location: Bochum, Germany

Re: Converting Binary file to file of 2-byte integers

Post by bn » Thu Oct 20, 2022 7:34 pm

FourthWorld wrote:
Thu Oct 20, 2022 7:24 pm
Does tWav contain the filename or the file contents?
It is the filename/path, how embarrassing. Thank your for pointing that out.

Here is the corrected script, even tested... :)

Code: Select all

on mouseUp
   local tWav, tHeader, tData, tLengthFile, tResult, tBinary, tBinaryLength
   local tInstruct, tNumLines, tDecoded, tSkipped
   
   answer file "load a WAV" with type "Waveform-Audio|wav|WAV"
   if it is empty then exit mouseUp
   
   put url ("binfile:" & it) into tWav
   put the length of tWav into tLengthFile
   
   -- get binary data without header
   put byte 45 to -1 of tWav into tBinary
   put the length of tBinary into tBinaryLength
   
   -- tHeader will contain the headerData as ASCII
   put binaryDecode("a44",tWav,tHeader) into tResult
   
   -- get first instance of unsigned integer, tData will contain the unsigned integers
   put binaryDecode(("S"),tBinary, tData) into tResult
   put tData & cr after tDecoded
   
   -- get rest of unsigned integers, use "x & i" to skip 
   repeat with i = 2 to tBinaryLength - 2 step 2
      put "x" & i & "S" into tInstruct
      put binaryDecode((tInstruct ),tBinary,tSkipped, tData) into tResult
      put tData & cr after tDecoded
   end repeat
   delete char -1 of tDecoded -- return
   
   ## tDecoded contains unsigned integers, one per line
   
   put the number of lines of tDecoded into tNumLines -- should be half of the length of binary data
end mouseUp
Kind regards
Bernd

Post Reply

Return to “Multimedia”