Creating Binary file headers

LiveCode is the premier environment for creating multi-platform solutions for all major operating systems - Windows, Mac OS X, Linux, the Web, Server environments and Mobile platforms. Brand new to LiveCode? Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
drhoward
Posts: 22
Joined: Tue Dec 10, 2013 9:03 am

Creating Binary file headers

Post by drhoward » Sat Mar 07, 2015 7:48 am

I'm currently developing a program that uses CSV files to provide control information for generating wav and eventually ogg sound files. Handling big endian and little endian conversion is not a problem, it just takes a simple repeat until loop.

Here's my problem: I'm wondering if there is a simple function call to convert 2 and 4 byte chunks of data to signed numbers and another one to do the reverse.

I can write a complicated subroutine to do this if necessary, but in most languages there is a simple function call to do this type of conversion. Any help will be appreciated. I'm using the free version 6.5.0 Build 3009. These conversion routines are needed to create or decode sound file header information, as well as for storing or interpreting the data entries.

The code I'm developing is not elegant, since I'm relatively new to LiveCode. I'm planning to release the code to the community, as an example of how to program a two channel frequency generator where each channel has a different frequency. The CSV file will have only 3 data entries per line: Frequency for channel A; Frequency for channel B; and the number of minutes to run these simultaneous frequencies. The next line will again have frequencies for channel A and channel B followed by a time parameter. There can be as many lines as the user wants to use. The wave file will be created, containing the data for the first CSV control line, followed by the data for the second control line, etc. The standard mathematical sin() function will be used to generate the data. The program could easily be extended to handle additional channels. Reading the CSV file has not been a problem.

Thank you!

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7393
Joined: Sat Apr 08, 2006 8:31 pm
Contact:

Re: Creating Binary file headers

Post by jacque » Sat Mar 07, 2015 9:26 pm

Being a LiveCode native speaker and having little understanding of other languages, I got lost. Could you give an example of the chunks involved and what result they should return? It sounds like you want byteToNum, but I'm not sure.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

drhoward
Posts: 22
Joined: Tue Dec 10, 2013 9:03 am

Re: Creating Binary file headers

Post by drhoward » Sun Mar 08, 2015 9:32 am

My understanding of byteToNum is that it only handles 1 byte at a time. However, I need to handle bigger numbers that when stored, span several bytes. Some of the numbers that I need to deal with take 2 byte of storage and some take up 4 bytes of storage. As an example, here is the beginning of the header for a wav file:

"RIFF"(4 byte number)"WAVE""fmt "(4 byte number)(2 byte number)(2 byte number) ...

Bytes 1-4 contain the ascii coding for a 4 byte string that contains the capital letters "RIFF"
Bytes 5-8 contain a 4 byte number specifying the the number of bytes that follow (i.e. the file length less 8 bytes)
Bytes 9-12 contain a 4 bye string with the ascii coding for "WAVE"
Bytes 13-16 contain a 4 byte string with the ascii coding for "fmt "
Bytes 17-20 contain a 4 byte number
Bytes 21-22 contain a 2 byte number
Bytes 23-24 contain a 2 byte number
and it goes on for a while from there until the data is presented

Numbers have an additional complication in terms of the order in which the bytes are stored. On some computer systems the most significant byte is stored first with the least significant byte being stored last. This is referred to as utilizing Big Endian storage. On others, such as PC's the reverse is true. The least significant byte is stored first in memory and the most significant byte is stored last. This is referred to as utilizing Little Endian storage. This is discussed at length on the web.

I can handle the Endian problem with a repeat loop that switches the data bytes associated with numbers around.

However, I don't want to have to invent a wheel that has already been invented, if it available for my use. Bytes 5 through 8 above are used to store a large number that requires 4 bytes of storage. My question is whether there is a function call where I can specify that a number is being stored in bytes 5-8 and which will return an integer or decimal value. Of course I also need a function to handle the reverse operation.

As far as I know the numToByte function only deals with 1 byte at a time. I know that these multi-byte routines are available at some level in the code compiler, since sound file objects are available for general use. Most operations involving binary files requires this type of function to be available. However, there is the question as to whether these functions are high or low level routines, where what I'm calling high level routines are available for a programmer, such as myself, to use, or whether I need to write my own high level routine for this purpose. In which case I will be using NumToByte to convert a number into the format required for 2 byte storage in binary file writes.

I hope that this clarified my situation.

Respectfully,

Howard

Thierry
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 875
Joined: Wed Nov 22, 2006 3:42 pm

Re: Creating Binary file headers

Post by Thierry » Sun Mar 08, 2015 1:10 pm

AFAIK, you have to build your own functions formula to deal with your multi-bytes stored numbers;
Hope with this code sample you get an idea how to do it.

Code: Select all

on mouseUp
   put url ("binfile:./1.wav") into inWav
   put the length of inWav into lengthOfFile -- 7690 <- as ls -l  1.wav
   
   -- Bytes 1-4 contain the ascii coding for a 4 byte string that contains the capital letters "RIFF"
   put char 1 to 4 of inWav into v1
   -- Bytes 5-8 contain a 4 byte number specifying the the number of bytes that follow 
   put chartonum( char 5 of inWav ) into v2a
   put chartonum( char 6 of inWav  ) into v2b
   put chartonum( char 7 of inWav ) into v2c
   put chartonum( char 8 of inWav ) into v2d
   -- v2 = 7682 which is correct as lengthOfFile is 7690 and we have 8 chars already parsed.
   put v2d*1024*256 + v2c*1024 + v2b*256 + v2a into v2
   -- Bytes 9-12 contain a 4 bye string with the ascii coding for "WAVE"
   put char 9 to 12 of inWav into v3
   -- Bytes 13-16 contain a 4 byte string with the ascii coding for "fmt "
   put char 13 to 16 of inWav into v4
   -- ....
   
   
   -- Ok, now write back to a .wav file
   put empty into outWav
   -- looks like format() is buggy ???? no time for me to investigate more.
   -- put format( "RIFF%c%c%c%cWAVEfmt ", numtobyte(v2a), numtobyte( v2b), numtobyte( v2c), numtobyte( v2d) ) into outWav
   
   put "RIFF" after outWav
   put  numtobyte(v2a) & numtobyte( v2b) & numtobyte( v2c) & numtobyte( v2d) after outWav
   put "WAVEfmt " after outWav
   put outWav into url ("binfile:./2.wav")
end mouseUp
Typing od -c 2.wav in a terminal window, I have the expected result which is
the same header as my original 1.wav file.

HTH,

Thierry
!
SUNNY-TDZ.COM doesn't belong to me since 2021.
To contact me, use the Private messages. Merci.
!

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10052
Joined: Sat Apr 08, 2006 7:05 am
Contact:

Re: Creating Binary file headers

Post by FourthWorld » Sun Mar 08, 2015 3:31 pm

Would binaryEncode do what you need?:
http://livecode.com/developers/api/6.0. ... aryEncode/
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

Thierry
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 875
Joined: Wed Nov 22, 2006 3:42 pm

Re: Creating Binary file headers

Post by Thierry » Sun Mar 08, 2015 4:11 pm

Would binaryEncode do what you need?:
Oops, forgot this one.

So, did try again and first go I had to force LC 6.7.3 to quit (CPU getting hot)

This one returns good values, on a Mac with LC 6.7.3:

Code: Select all

   get binaryDecode( "a4Ia4a4", inWav, x1, x2, x3, x4)
   -- x1 ->  "RIFF"
   -- x2 -> 7682  (same as v2 in my previous post)
   -- x3 -> "WAVE"

Thierry
!
SUNNY-TDZ.COM doesn't belong to me since 2021.
To contact me, use the Private messages. Merci.
!

drhoward
Posts: 22
Joined: Tue Dec 10, 2013 9:03 am

Re: Creating Binary file headers

Post by drhoward » Mon Mar 09, 2015 5:56 am

binaryEncode and binaryDecode appear to be what I was looking for.

Thank you!

Howard

Post Reply