Asteroids on Oscilloscope

The Joy of Tech - Anything and everything about all things shiny..
Post Reply
User avatar
gumboot
Baaaaaaa!
Baaaaaaa!
Posts: 7652
Joined: Sun Aug 26, 2007 3:42 am 0
Location: trumpstain!

Re: Asteroids on Oscilloscope

Post by gumboot »

gumboot wrote:
Kamineko wrote:Definitely kHz; I'm pushing uncompressed values from an array of (triples of) bits onto the four GPIO pins to use them as a DAC.
Ok, well a quick test shows that you can economically take about 5dB off of the most critical band with noise shaping, but that doesn't really seem worth the hassle.

You got a link to the audio samples you're working with? I might have a wee play around to see if they can't be pre-cooked for optimum 8kHz 3-bit fidelity.
Haven't listened to any proper audio, but it seems to work on 1kHz tones (requires libsndfile):

Code: Select all

    const mask = 0x1fff, round = 0x1000;
    float prev_err = 0.0, prev_prev_err = 0.0;
    for (;;)
    {
        int len = sf_read_float(infile, in, 4096);
        if (len <= 0)
            break;
        samples -= len;

        for (i = 0; i < len; i++)
        {
            float sample = in[i] * 0x8000;
            if (shaped)
                sample -= prev_err * 0.61 + prev_prev_err * 0.21;

            int s2 = (int)rint(sample);
           
            if (dithered)
                s2 += (rand() & mask) - (rand() & mask);

            s2 = (s2 + round) & ~mask;

            prev_prev_err = prev_err;
            prev_err = sample - s2;

            if (s2 < -0x8000) s2 = -0x8000;
            if (s2 > 0x7fff) s2 = 0x7fff;

            out[i] = s2;
        }

        (void)sf_write_short(outfile, out, len);
    }
Those 'shaped' coefficients are specially tuned for 8kHz... and by 'specially tuned' I mean a machine tried random values over and over until it found some that worked alright. Thanks machine!
User avatar
Kamineko
Baaaaaaa!
Baaaaaaa!
Posts: 7569
Joined: Fri Dec 17, 2004 2:53 pm 0
Contact:

Re: Asteroids on Oscilloscope

Post by Kamineko »

Right now I'm not on my good computer so I'm just using whatever I can find. I've got it saying the 'Arduino Duemilanove' sample from this page legibly. Bitrate is 8kHz * 3bits/sample = 3000 bytes/second.

I'm scaling them up in audacity to fill the amplitude range and accepting a bit of clipping. Ideally I'd use Goldwave on my good comp, and using the compressor with tiny attack and release to make the speech exaggerated and boomy without technically clipping it. I haven't looked into any complicated technical processing beyond that. Whatever the audio editor spits out is what I use.

The proof of concept conversion code (unsigned 8-bit headerless pcm input) is abominable, unexciting but functional.
Spoiler: show
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int main(int argc, char *argv[])
{
FILE *f = fopen("input.raw", "rb");

fseek(f, 0, SEEK_END);

int s = ftell(f);

rewind(f);

unsigned char *inbytes = (unsigned char *)malloc(s);

fread(inbytes, 1, s, f);
fclose(f);

int outs = ((s * 3) + 7) / 8;

unsigned char *outbytes = (unsigned char *)malloc(outs);

for (unsigned int b = 0; b < outs; b++)
{
outbytes = 0;
}

for (unsigned int b = 0; b < s; b++)
{
unsigned char inbyte = inbytes;

float bytefloat = (float)inbyte;

float zeroone = bytefloat / 255.0f;

float scaled = zeroone * 8.0f;

unsigned char result = (unsigned char)scaled;

unsigned char targeted_trip = b % 8;

// printf("Converting input byte %02X into output triplet %d.\n", inbyte, result);

switch (targeted_trip)
{
case (0):
{
outbytes[((b * 3 / 8) + 0)] |= (result << 0);
} break;
case (1):
{
outbytes[((b * 3 / 8) + 0)] |= (result << 3);
} break;
case (2):
{
outbytes[((b * 3 / 8) + 0)] |= (result << 6);
outbytes[((b * 3 / 8) + 1)] |= (result >> 2);
} break;
case (3):
{
outbytes[((b * 3 / 8) + 0)] |= (result << 1);
} break;
case (4):
{
outbytes[((b * 3 / 8) + 0)] |= (result << 4);
} break;
case (5):
{
outbytes[((b * 3 / 8) + 0)] |= (result << 7);
outbytes[((b * 3 / 8) + 1)] |= (result >> 1);
} break;
case (6):
{
outbytes[((b * 3 / 8) + 0)] |= (result << 2);
} break;
case (7):
{
outbytes[((b * 3 / 8) + 0)] |= (result << 5);
} break;
}
}

FILE *fout = fopen("output.bin", "wb");

fwrite(outbytes, 1, outs, fout);

fclose(fout);

for (unsigned int b = 0; b < s; b++)
{

unsigned int targeted_trip = (b % 8);

unsigned char value;

switch (targeted_trip)
{
case (0):
{
value = (outbytes[((b * 3) / 8) + 0] >> 0);
} break;
case (1):
{
value = (outbytes[((b * 3) / 8) + 0] >> 3);
} break;
case (2):
{
value = (outbytes[((b * 3) / 8) + 0] >> 6)
| (outbytes[((b * 3) / 8) + 1] << 2);
} break;
case (3):
{
value = (outbytes[((b * 3) / 8) + 0] >> 1);
} break;
case (4):
{
value = (outbytes[((b * 3) / 8) + 0] >> 4);
} break;
case (5):
{
value = (outbytes[((b * 3) / 8) + 0] >> 7)
| (outbytes[((b * 3) / 8) + 1] << 1);
} break;
case (6):
{
value = (outbytes[((b * 3) / 8) + 0] >> 2);
} break;
case (7):
{
value = (outbytes[((b * 3) / 8) + 0] >> 5);
} break;
}

value &= 7;

// printf("Retrieved %d value from stream.\n", value);

unsigned long int valuelong = value;

valuelong *= 255;
valuelong /= 7;

inbytes = (unsigned char) valuelong;
}

FILE *out2 = fopen("outputtestblah.raw", "wb"); // Open this as 8bit pcm to see what the system is trying to play.

fwrite(inbytes, 1, s, out2);

fclose(out2);

free(inbytes);

free(outbytes);

return 0;
}


If you're super curious, you might be interested to know how the output is physically formed: The four resistors forming the PSG are 180k, 200k, 220k and 240k. Each can be set high or low. They can be considered to contribute a value to the final voltage proportional to their reciprocal. Add these up and you've got the final value.

The following grid sorts the possible combinations by output value, scaled from -1.0 to 1.0.
Image

Because there's not much difference between many of the possible output levels, I figured i'd just take the red squares and leave the rest.
Image

If the resistances were powers of two this would be a straight ramp, but then the PSG would have channels 0.125, 0.250, 0.500 and 1.000 volume instead of roughly 0.750, 0.835, 0.915, 1.000. (And that's before considering that voltage != volume due to logarithmic hearing response stuff that I always end up interpreting backwards.)

Alternatively, I could have omitted full 1 and full 0 for a nice ramp between +/-0.567 except for the holes between +/-0.42 -- +/-0.09. Just using the centre ramp would have resulted in a lot of volume difference between PSG output and my lo-fi DAC.
User avatar
gumboot
Baaaaaaa!
Baaaaaaa!
Posts: 7652
Joined: Sun Aug 26, 2007 3:42 am 0
Location: trumpstain!

Re: Asteroids on Oscilloscope

Post by gumboot »

Yuck!
Spoiler: show

Code: Select all

#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <math.h>

static const uint8_t qtab[256] = {
    0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
    0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,
    1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
    1,1,1,1,1,1,1,1,1,1,1,2,2,2,2,2,
    2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,
    2,2,2,2,2,2,2,2,2,2,2,2,3,3,3,3,
    3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,
    3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,
    4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,
    4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,
    4,4,4,4,5,5,5,5,5,5,5,5,5,5,5,5,
    5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
    5,5,5,5,5,6,6,6,6,6,6,6,6,6,6,6,
    6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
    6,6,6,6,6,6,6,6,6,6,6,6,6,7,7,7,
    7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
};
static const int16_t rtab[8] = { -32768, -18596, -13872, -3135, 3135, 13872, 18596, 32767 };

int main(int argc, char *argv[])
{
    int dithered = 1, shaped = 1;

    if (argc == 2)
    {
        shaped = atoi(argv[1]) & 2;
        dithered = atoi(argv[1]) & 1;
    }

    FILE *f = fopen("input.raw", "rb");

    fseek(f, 0, SEEK_END);

    int s = ftell(f);

    rewind(f);

    unsigned char *inbytes = (unsigned char *)malloc(s);

    fread(inbytes, 1, s, f);
    fclose(f);

    int outs = ((s * 3) + 7) / 8;

    unsigned char *outbytes = (unsigned char *)malloc(outs);

    for (unsigned int b = 0; b < outs; b++)
    {
        outbytes[b] = 0;
    }

    float e1 = 0.0, e2 = 0.0;

    for (unsigned int b = 0; b < s; b++)
    {
        unsigned char inbyte = inbytes[b];

        float sample = (inbyte - 128) / 128.0;

        if (shaped)
            sample -= e1 * 0.61 + e2 * 0.21;

        int inshort = rint(sample * 32768.0) + 32768;

        if (dithered)
            inshort += (rand() & 0x1fff) - (rand() & 0x1fff);
        inshort = (inshort + 128) >> 8;
        if (inshort < 0) inshort = 0;
        if (inshort > 255) inshort = 255;

        unsigned char result = qtab[inshort];

        e2 = e1;
        e1 = sample - (rtab[result] - 32768) / 32768.0;

        unsigned char targeted_trip = b % 8;

        // printf("Converting input byte %02X into output triplet %d.\n", inbyte, result);

        switch (targeted_trip)
        {
        case (0):
            {
            outbytes[((b * 3 / 8) + 0)] |= (result << 0);
            } break;
        case (1):
            {
            outbytes[((b * 3 / 8) + 0)] |= (result << 3);
            } break;
        case (2):
            {
            outbytes[((b * 3 / 8) + 0)] |= (result << 6);
            outbytes[((b * 3 / 8) + 1)] |= (result >> 2);
            } break;
        case (3):
            {
            outbytes[((b * 3 / 8) + 0)] |= (result << 1);
            } break;
        case (4):
            {
            outbytes[((b * 3 / 8) + 0)] |= (result << 4);
            } break;
        case (5):
            {
            outbytes[((b * 3 / 8) + 0)] |= (result << 7);
            outbytes[((b * 3 / 8) + 1)] |= (result >> 1);
            } break;
        case (6):
            {
            outbytes[((b * 3 / 8) + 0)] |= (result << 2);
            } break;
        case (7):
            {
            outbytes[((b * 3 / 8) + 0)] |= (result << 5);
            } break;
        }
    }

    FILE *fout = fopen("output.bin", "wb");

    fwrite(outbytes, 1, outs, fout);

    fclose(fout);

    for (unsigned int b = 0; b < s; b++)
    {

        unsigned int targeted_trip = (b % 8);

        unsigned char value;

        switch (targeted_trip)
        {
        case (0):
            {
            value = (outbytes[((b * 3) / 8) + 0] >> 0);
            } break;
        case (1):
            {
            value = (outbytes[((b * 3) / 8) + 0] >> 3);
            } break;
        case (2):
            {
            value = (outbytes[((b * 3) / 8) + 0] >> 6)
            | (outbytes[((b * 3) / 8) + 1] << 2);
            } break;
        case (3):
            {
            value = (outbytes[((b * 3) / 8) + 0] >> 1);
            } break;
        case (4):
            {
            value = (outbytes[((b * 3) / 8) + 0] >> 4);
            } break;
        case (5):
            {
            value = (outbytes[((b * 3) / 8) + 0] >> 7)
            | (outbytes[((b * 3) / 8) + 1] << 1);
            } break;
        case (6):
            {
            value = (outbytes[((b * 3) / 8) + 0] >> 2);
            } break;
        case (7):
            {
            value = (outbytes[((b * 3) / 8) + 0] >> 5);
            } break;
        }

        value &= 7;

        // printf("Retrieved %d value from stream.\n", value);

        long int valuelong = rtab[value];

        valuelong >>= 8;
        valuelong += 128;

        inbytes[b] = (unsigned char) valuelong;
    }

    char filename[32];
    sprintf(filename, "outputtestd%ds%d.raw", dithered ? 1 : 0, shaped ? 1 : 0);
    FILE *out2 = fopen(filename, "wb"); // Open this as 8bit pcm to see what the system is trying to play.

    fwrite(inbytes, 1, s, out2);

    fclose(out2);

    free(inbytes);

    free(outbytes);

    return 0;
}
Don't dither at that sample rate or bit depth.
User avatar
gumboot
Baaaaaaa!
Baaaaaaa!
Posts: 7652
Joined: Sun Aug 26, 2007 3:42 am 0
Location: trumpstain!

Re: Asteroids on Oscilloscope

Post by gumboot »

gumboot wrote:Yuck!
Thinking about this, I probably messed up the noise shaping. I think that without dither it shouldn't sound worse than the un-shaped. Probably relates to clipping thresholds and/or the fact that I deliberately over-drove the input while calculating the quuantisation table.

But yeah, with only 3 bits to work with, dither sounds pretty terrible, and noise shaping can't help an awful lot. The compressor is the most important thing.

For arbitrary content you might also look at a high-pass filter to remove frequencies your speaker can't reproduce, so as to give more room for the compressor to work in the audible frequencies.

Removing [audible] low frequencies is also meant to help reduce the perception that audio sounds muffled when the high frequencies have been removed.
User avatar
Kamineko
Baaaaaaa!
Baaaaaaa!
Posts: 7569
Joined: Fri Dec 17, 2004 2:53 pm 0
Contact:

Re: Asteroids on Oscilloscope

Post by Kamineko »

Just requested some samples of TI's precision dual DAC range. The TLV5618A is looking like a good output for the Ocelot. It comes in a tiny 8 pin parallel dip package and has SPI and probably doesn't suck. I HOPE.

Which, of course, means that there's absolutely no reason why I couldn't have used the DAC-less PICs after all, and there was no reason to get the PIC KIT as I could have done all of this with just the bare Microstick (if I didn't mind having to install a wider socket to accommodate the mothership, and when finishing the circuit I would have to unseat the chip and shift it about dangerously every time I wanted to do something.)

What would be super boffin is if there was an electronics simulator that had every single device in existence built into it, so I could find out this kind of thing definitively before I make a compounded pyramid of mistakes, each trying to recover from the last, like a guy betting his shirt at the track. (And somehow, every 4am electronics troubleshooting session is accompanied with one recording or another of Por una Cabeza...)
Kamineko wrote:Oh no you ain't. I ain't makin' two trips to the store! :lol:

But yeah, thanks a lot. I hadn't considered that route. :) I don't know one bit about how I2C is typically used in designs, this thread is just my whiteboard for now. The ideal solution for me is the cheapest really. If there's a big glaring hole in my layman's idea that says 'don't use internal audio DSP DACs for oscillo, you'll just get the same crap as before' that can be fixed with a more capable external one, that's exactly the kind of thing I need to know.

I wonder if the TI MSP430 teeny tiny chip I already have lying around is I2C capable...
Hey there, Kamineko of the past, Kamineko of the present here. I have some advice: don't use internal audio DSP DACs for oscillo, you'll just get the same crap as before. But that's okay because most likely it can be fixed with a more capable external one. I just thought that was exactly the kind of thing you might need to know. You'll have to ask Kamineko of the future how his TI DACs work out.

The DAC in the dsPIC33 is for audio. As far as I can tell, it has a number of stages. It gobbles up samples, shoots them through a serial thing, re-slurps up the serial, and then mashes it all up into analogue. Finally it low-pass filters the result. If you put in a high frequency signal (and remember yours is a signal with -sharp corners- due to the angled lines you're interested in spewing), you're going to be disappointed with the result. Speed up the output on the screen and you'll see something that looks as strangely wrong... the wavefronts of the square wave will have chunky pieces to them. It's as if the dsPICs DAC is methodically screwing with your signal as you put it in, and then methodically screwing with it -again- as it comes out. It is not what you want.

Either that or we're both using it wrong. If you figure it out before I do, past Kam, tell future Kam before he makes a mistake.
User avatar
Kamineko
Baaaaaaa!
Baaaaaaa!
Posts: 7569
Joined: Fri Dec 17, 2004 2:53 pm 0
Contact:

Re: Asteroids on Oscilloscope

Post by Kamineko »

Today I've learned something new.

In voltage regulators, the term 'drop out' refers to the minimum difference between the input voltage and the output voltage. For example, a 3.3V regulator with a drop out of 2.0V has to be given 5.3V or it won't work. Yeah... I should have gone for the 100mV drop out one instead, shouldn't I?
User avatar
Kamineko
Baaaaaaa!
Baaaaaaa!
Posts: 7569
Joined: Fri Dec 17, 2004 2:53 pm 0
Contact:

Re: Asteroids on Oscilloscope

Post by Kamineko »

The number of different companies' samples that are going to be on this board... it's really becoming a team effort. :o

Edit - Huh... I seem to have soldered the mixing row of my PSG resistors one row further up on the board than I intended (according to all my schematics), so that they're going into the centre tap of the trimmer rather than the edge. The result is that the trimmer is a variable resistance rather than an adjustable divider. I can't really remember whether or not this was deliberate! That's weird. I should definitely fix that. There's one penny's worth of resistors I'll never see again *shakes fist at heavens*.

So the plan is to replace the power section, the DAC section and the PSG section, having already replaced the input cable, the audio cable and the programmer. Basically, the Ocelot is somewhere between the Ship of Theseus and Lister's Triple Fried Egg Chili Chutney Sandwich.
adam
Baaaaaa!
Baaaaaa!
Posts: 4418
Joined: Sat Jun 24, 2006 9:15 pm 0

Re: Asteroids on Oscilloscope

Post by adam »

some companies are not
as genourous with samples as microchip - you can request them but they won't always come, or you might get an email to find out how many thousand units you'll be wanting in future
User avatar
Kamineko
Baaaaaaa!
Baaaaaaa!
Posts: 7569
Joined: Fri Dec 17, 2004 2:53 pm 0
Contact:

Re: Asteroids on Oscilloscope

Post by Kamineko »

Got my voltage regulators from Micrel, but no DAC from TI yet.
User avatar
Kamineko
Baaaaaaa!
Baaaaaaa!
Posts: 7569
Joined: Fri Dec 17, 2004 2:53 pm 0
Contact:

Re: Asteroids on Oscilloscope

Post by Kamineko »

3-button multiplexed Mega Drive pad reading code works first time! Nice!
User avatar
Kamineko
Baaaaaaa!
Baaaaaaa!
Posts: 7569
Joined: Fri Dec 17, 2004 2:53 pm 0
Contact:

Re: Asteroids on Oscilloscope

Post by Kamineko »

What's the point of having a super micro low power dual DAC that has a SPI interface if the 'update both channels simultaneously' pin is completely distinct from the freakin' SPI?

The TI tlv5618a dual DAC uses SPI, but it has a control bit in the communication word that lets you update both channels simultaneously: it has a built in data buffer stage that lets you load in a value for DAC B to be stored in the buffer, then load in a value for DAC A with a command to copy the buffer value to DAC B, updating them simultaneously. Ordered a sample a week ago (they come in 8-pin ceramic PDIP, hooray!), still not here. Sad face.

The MAX548A and the Microchip dacs all have a separate LDAC signal that copies the data from internal buffers for both channels onto the outputs, but it's totally useless if you can't sync it up with the SPI isn't it?
User avatar
venusian
Baaaaaaa!
Baaaaaaa!
Posts: 6653
Joined: Wed Nov 06, 2002 2:22 pm 0
Location: South Gloucestershire

Re: Asteroids on Oscilloscope

Post by venusian »

Presumably the datasheet will tell you how long you have to wait after the last SPI clock before hitting the sync bit. Otherwise it is, yes, a bit useless.
User avatar
Kamineko
Baaaaaaa!
Baaaaaaa!
Posts: 7569
Joined: Fri Dec 17, 2004 2:53 pm 0
Contact:

Re: Asteroids on Oscilloscope

Post by Kamineko »

Your sample request has been canceled.

US Government export regulations require TI to know your complete company name. We could not locate your company using the name provided. Also, we are not allowed to use abbreviated company names to get export clearance.

The Ship To company name provided: INDEPENDENT STUDENT

The End Customer company name provided: INDEPENDENT STUDENT

Please resubmit your request with your complete company name. Do Not Abbreviate. Also, please provide a valid company URL so that we can verify that your company is not a foreign military or government entity.

All contact and shipping information must be provided in English.

If you are a student, please enter your complete University Name in the company name field and submit a new request with this information.

If you are self-employed, then use Independent Designer or Independent Consultant in the company name field and submit a new request with this information.

We apologize for the inconvenience.
That's what I get for trying to tell the truth.
User avatar
Baggypants
Baaaaaaa!
Baaaaaaa!
Posts: 5328
Joined: Sun Mar 19, 2006 12:54 pm 0
Location: Salford(UK);
Contact:

Re: Asteroids on Oscilloscope

Post by Baggypants »

Kamineko wrote:
Your sample request has been canceled.

US Government export regulations require TI to know your complete company name. We could not locate your company using the name provided. Also, we are not allowed to use abbreviated company names to get export clearance.

The Ship To company name provided: INDEPENDENT STUDENT

The End Customer company name provided: INDEPENDENT STUDENT

Please resubmit your request with your complete company name. Do Not Abbreviate. Also, please provide a valid company URL so that we can verify that your company is not a foreign military or government entity.

All contact and shipping information must be provided in English.

If you are a student, please enter your complete University Name in the company name field and submit a new request with this information.

If you are self-employed, then use Independent Designer or Independent Consultant in the company name field and submit a new request with this information.

We apologize for the inconvenience.
That's what I get for trying to tell the truth.
"The eggs, get them!"
User avatar
Kamineko
Baaaaaaa!
Baaaaaaa!
Posts: 7569
Joined: Fri Dec 17, 2004 2:53 pm 0
Contact:

Re: Asteroids on Oscilloscope

Post by Kamineko »

I don't attend university; I graduated years ago. This device is part of my study as an independent student.

It seems like the week-long delay on my last TI sample request was due to their system desperately trying to Google the company 'Independent Student'. Ten minutes ago I got an e-mail from TI with my fedex tracking number for my updated sample request (following the advice on sample request convention I got from TI over e-mail).

Yay!

(Estimated delivery: This Wednesday. What?? There's no way...)
Post Reply