Converting Binary file to file of 2-byte integers
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller
-
- 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
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
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
Peter Reid
Loughborough, UK
-
- VIP Livecode Opensource Backer
- Posts: 9751
- Joined: Wed May 06, 2009 2:28 pm
- Location: New York, NY
Re: Converting Binary file to file of 2-byte integers
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
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
-
- VIP Livecode Opensource Backer
- Posts: 9751
- Joined: Wed May 06, 2009 2:28 pm
- Location: New York, NY
Re: Converting Binary file to file of 2-byte integers
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"?
Craig
Code: Select all
on mouseUp
put binaryDecode("h","12",temp)
answer temp
end mouseUp
-
- 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
the value is the number of conversions completed, not the values
Peter
Peter
--
Peter Reid
Loughborough, UK
Peter Reid
Loughborough, UK
-
- VIP Livecode Opensource Backer
- Posts: 9856
- Joined: Sat Apr 08, 2006 7:05 am
- Location: Los Angeles
- Contact:
Re: Converting Binary file to file of 2-byte integers
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
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn
-
- VIP Livecode Opensource Backer
- Posts: 9751
- Joined: Wed May 06, 2009 2:28 pm
- Location: New York, NY
Re: Converting Binary file to file of 2-byte integers
@Peter.
But the dictionary states
@Klaus or Richard, if you think this side issue should be moved to its own thread, feel free.
Craig
But the dictionary states
but alsobinaryDecode("h","M",theHex) -- converts M to its hex equivalent
So I see what you meant. But doesn't at least the first quote above have the meaning it seems to?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.
@Klaus or Richard, if you think this side issue should be moved to its own thread, feel free.
Craig
-
- 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
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
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 Reid
Loughborough, UK
-
- 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
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:
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:
--
Peter Reid
Loughborough, UK
Peter Reid
Loughborough, UK
-
- Livecode Opensource Backer
- Posts: 9454
- Joined: Fri Feb 19, 2010 10:17 am
- Location: Bulgaria
Re: Converting Binary file to file of 2-byte integers
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.
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.
-
- Livecode Opensource Backer
- Posts: 9454
- Joined: Fri Feb 19, 2010 10:17 am
- Location: Bulgaria
Re: Converting Binary file to file of 2-byte integers
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.
stack in a field.
I am interested to know how you obtained that number string.
-
- VIP Livecode Opensource Backer
- Posts: 4027
- Joined: Sun Jan 07, 2007 9:12 pm
- Location: Bochum, Germany
Re: Converting Binary file to file of 2-byte integers
Hi Peter,
is this what you want to do?
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.
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
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 111 times
Last edited by bn on Thu Oct 20, 2022 7:36 pm, edited 1 time in total.
-
- VIP Livecode Opensource Backer
- Posts: 9856
- Joined: Sat Apr 08, 2006 7:05 am
- Location: Los Angeles
- Contact:
Re: Converting Binary file to file of 2-byte integers
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
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn
-
- VIP Livecode Opensource Backer
- Posts: 4027
- Joined: Sun Jan 07, 2007 9:12 pm
- Location: Bochum, Germany
Re: Converting Binary file to file of 2-byte integers
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
Bernd