If this is an external file then you can actually write a GCBASIC converter, in your language of choice. I would have it convert the external file into a GCBASIC table as UnsignedOutput values.
What is a GCBASIC Converter? (In Simple Terms)
A GCBASIC converter is like a "helper tool" for the GCBASIC compiler. It takes an outside file—like a raw audio file with sound samples—and turns it into a "TABLE" in your program code. This TABLE is like a list of numbers stored in the chip's permanent memory (ROM), so your code can quickly read and use them (e.g., to play sound). It's automatic: GCBASIC runs the converter behind the scenes to "create" the file data into your program.
How It Transforms an External Signed Audio File into an Unsigned TABLE
Imagine your external file is a simple binary file called signed_audio.bin—just a stream of 8-bit signed bytes (-128 to +127) for audio samples. Here's how a converter changes it to a TABLE of unsigned values (0–255), step by step:
Put Your File in the Right Spot: Drop signed_audio.bin into your project folder next to your source GCBASIC program.
Set Up a Simple Config File: Create a tiny text file like BIN2GCB.ini in the same gcbasic\converters folder. This tells GCBASIC: "Hey, when you see .bin files, use this tool to convert them." Example:
desc = Signed audio file (*.bin)
in = bin
out = GCB
exe = BIN2GCB.exe ' Your converter program
(You can make or download BIN2GCB.exe—it's a small program that does the magic; think of it like a Python script wrapped as an .exe.)
The Converter Does the Work: The BIN2GCB.exe reads your signed_audio.bin file byte by byte. For each signed byte (e.g., 0x80 for -128), it:
Converts it to unsigned: Just add 128 (or XOR with 0x80 for speed). So -128 becomes 0, 0 becomes 128, +127 becomes 255.
Writes the new unsigned bytes into a GCBASIC code file, like signed_audio.GCB. This file looks like:
UnsignedAudioTable As ROM Byte
Data 0x00, 0x80, 0xFF, ... ' Your transformed samples as hex numbers
End Table
GCBASIC checks if the .bin is newer than the .GCB—if yes, it auto-runs the converter to update it.
Link It to Your Program: In your main .gcb file, add one line:
#include <signed_audio.bin>
Boom—GCBASIC pulls in the TABLE automatically during compilation.
Use It in Code: Now your program has the unsigned TABLE ready. Example snippet:
Dim Index As Word
Dim Sample As Byte
Index = 0
ReadTable UnsignedAudioTable, Index, Sample ' Grabs an unsigned value (0–255)
PORTB = Sample ' Outputs to pins for sound
Play it back in a loop for audio!
This keeps your chip's memory efficient (data in ROM, not RAM) and handles the signed-to-unsigned shift once during build-time—not every time the code runs. If your file is huge, the converter processes it fast without bloating your code. For a ready-made converter, check GCBASIC docs or forums— or whip one up in Python to read the bin, transform bytes, and spit out the TABLE syntax.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
That is very useful, but I'd like to do the conversion byte by byte in the microcontroller, so that I can stream signed 8bit audio data through it. This audio data may come from outside (maybe streamed from a computer or from a large file in some externa memory). I might scale it up to higher resolution later, that is why I would like to find out if I can do it more efficiently in the chip without using a lookup table. -More efficiently than this logic (for 8bit):
If signed_input <127 then add 128
If signed_input >127 then subtract 128
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Magic! I did use XOR (hardware) logic back in the 90s (16bit soundcard for Atari ST), but I couldn't for some reason get it into code now. I made a table and the more I looked at it, the more confused I got. But ofcouse. Now I see it. Clearly.
Thanks!
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Excellent. You are having real fun. It is nice to help you.
I am working on a Lego Train project for schools here in the UK.
I was using XOR yesterday to understand which of the 16 Hall switches changed state. This tells the master mcu where the train is and what to do with the points. XOR is fast and makes coding a lot easier.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
If I have an 8-bit sound file (int8/two's complement) ranging from -128 to +127 and I want to scale it to 0-255 I could do something like:
If signed_input <127 then add 128
If signed_input >127 then subtract 128
Is there a more efficient way (besides a lookup table)?
A method
If this is an external file then you can actually write a GCBASIC converter, in your language of choice. I would have it convert the external file into a GCBASIC table as UnsignedOutput values.
What is a GCBASIC Converter? (In Simple Terms)
A GCBASIC converter is like a "helper tool" for the GCBASIC compiler. It takes an outside file—like a raw audio file with sound samples—and turns it into a "TABLE" in your program code. This TABLE is like a list of numbers stored in the chip's permanent memory (ROM), so your code can quickly read and use them (e.g., to play sound). It's automatic: GCBASIC runs the converter behind the scenes to "create" the file data into your program.
How It Transforms an External Signed Audio File into an Unsigned TABLE
Imagine your external file is a simple binary file called
signed_audio.bin—just a stream of 8-bit signed bytes (-128 to +127) for audio samples. Here's how a converter changes it to a TABLE of unsigned values (0–255), step by step:Put Your File in the Right Spot: Drop
signed_audio.bininto your project folder next to your source GCBASIC program.Set Up a Simple Config File: Create a tiny text file like
BIN2GCB.iniin the samegcbasic\convertersfolder. This tells GCBASIC: "Hey, when you see .bin files, use this tool to convert them." Example:desc = Signed audio file (*.bin) in = bin out = GCB exe = BIN2GCB.exe ' Your converter program(You can make or download
BIN2GCB.exe—it's a small program that does the magic; think of it like a Python script wrapped as an .exe.)BIN2GCB.exereads yoursigned_audio.binfile byte by byte. For each signed byte (e.g., 0x80 for -128), it:Writes the new unsigned bytes into a GCBASIC code file, like
signed_audio.GCB. This file looks like:UnsignedAudioTable As ROM Byte Data 0x00, 0x80, 0xFF, ... ' Your transformed samples as hex numbers End TableGCBASIC checks if the .bin is newer than the .GCB—if yes, it auto-runs the converter to update it.
.gcbfile, add one line:#include <signed_audio.bin>Boom—GCBASIC pulls in the TABLE automatically during compilation.
Dim Index As Word Dim Sample As Byte Index = 0 ReadTable UnsignedAudioTable, Index, Sample ' Grabs an unsigned value (0–255) PORTB = Sample ' Outputs to pins for soundPlay it back in a loop for audio!
This keeps your chip's memory efficient (data in ROM, not RAM) and handles the signed-to-unsigned shift once during build-time—not every time the code runs. If your file is huge, the converter processes it fast without bloating your code. For a ready-made converter, check GCBASIC docs or forums— or whip one up in Python to read the bin, transform bytes, and spit out the TABLE syntax.
That is very useful, but I'd like to do the conversion byte by byte in the microcontroller, so that I can stream signed 8bit audio data through it. This audio data may come from outside (maybe streamed from a computer or from a large file in some externa memory). I might scale it up to higher resolution later, that is why I would like to find out if I can do it more efficiently in the chip without using a lookup table. -More efficiently than this logic (for 8bit):
If signed_input <127 then add 128
If signed_input >127 then subtract 128
UnsignedOutput = SignedInput XOR 0x80
Magic! I did use XOR (hardware) logic back in the 90s (16bit soundcard for Atari ST), but I couldn't for some reason get it into code now. I made a table and the more I looked at it, the more confused I got. But ofcouse. Now I see it. Clearly.
Thanks!
Excellent. You are having real fun. It is nice to help you.
I am working on a Lego Train project for schools here in the UK.
I was using XOR yesterday to understand which of the 16 Hall switches changed state. This tells the master mcu where the train is and what to do with the points. XOR is fast and makes coding a lot easier.
What a great way to teach and learn logic.