Menu

Two's complement 8bit data scaled to byte (0-255)

Help
2025-12-09
2025-12-09
  • Roger Jönsson

    Roger Jönsson - 2025-12-09

    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)?

     
  • Anobium

    Anobium - 2025-12-09

    A method

    ' A conversion method 
    ReadTable SignedAudioData, Index, SignedInput 
    UnsignedOutput = SignedInput XOR 0x80
    UnsignedSamples(Index) = UnsignedOutput
    

    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:

    1. Put Your File in the Right Spot: Drop signed_audio.bin into your project folder next to your source GCBASIC program.

    2. 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.)

    1. 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:
    2. Converts it to unsigned: Just add 128 (or XOR with 0x80 for speed). So -128 becomes 0, 0 becomes 128, +127 becomes 255.
    3. 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.

    1. 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.

    1. 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.

     
  • Roger Jönsson

    Roger Jönsson - 2025-12-09

    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

     
    • Anobium

      Anobium - 2025-12-09

      UnsignedOutput = SignedInput XOR 0x80

       
  • Roger Jönsson

    Roger Jönsson - 2025-12-09

    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!

     
    • Anobium

      Anobium - 2025-12-09

      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.

       
  • Roger Jönsson

    Roger Jönsson - 2025-12-09

    What a great way to teach and learn logic.

     

Log in to post a comment.