Page 1 of 5

CVSD Compressed audio example

Posted: Sun Jul 05, 2009 2:51 am
by Chilly Willy
I know my post on CVSD compression was a bit esoteric for many folks, so I put together a practical example. This uses my new Z80 code discussed in the latest post in that thread to play back music. The main program is the particle fountain example that comes with Steff's Mini-DevKit. I loaded my cvsd driver, then do the particle fountain while waiting for input.

A or B = stop music
C = toggle Vblank sync
START = toggle pause (display and music)
UP = play song 1
RIGHT = play song 2
DOWN = play song 3
LEFT = play song 4

Bin and Src: musicdemo-1.7z

The audio is equivalent to unsigned 8-bit PCM at 11025 Hz at an effective compression ratio of 4:1, so you can fit just over 24 minutes of music in 4 MB of cart space. The demo is actually a bit over 3 MB, but I padded it to 4 MB. The music is Cassiopeia's "Eye of Time", found here.

Posted: Mon Jul 06, 2009 3:45 am
by tomaitheous
So the compression scheme takes 4 samples and does an average and creates/stores the angle of the amplitude?

Posted: Mon Jul 06, 2009 5:23 am
by Chilly Willy
Nope! CVSD works by comparing the current wave value with a derived sample value. If the wave is larger, it outputs a 1 and adds a delta to the derived sample value; if it's smaller, it outputs a 0 and subtracts a delta from the derived sample value. The key to improving the sound quality is how you alter the delta - in the "classic" CVSD algorithm, you look for three or four 1s or 0s in a row, and if you find them, you alter the delta bigger or smaller respectively. In my version of CVSD, I just compare the current output bit to the previous, altering the delta bigger if they are the same, or smaller if they differ. That makes the code much more simple, but doesn't seem to affect the quality. That simplification allowed me to make the Z80 code quick enough to handle 22050 Hz sample rate.

Actually, I timed it out today and my example code in the archive is about 9.5% fast. I need a couple NOPs here and there to get it to 22050. :D

Posted: Mon Jul 06, 2009 12:03 pm
by Stef
Chilly Willy wrote:Nope! CVSD works by comparing the current wave value with a derived sample value. If the wave is larger, it outputs a 1 and adds a delta to the derived sample value; if it's smaller, it outputs a 0 and subtracts a delta from the derived sample value. The key to improving the sound quality is how you alter the delta - in the "classic" CVSD algorithm, you look for three or four 1s or 0s in a row, and if you find them, you alter the delta bigger or smaller respectively. In my version of CVSD, I just compare the current output bit to the previous, altering the delta bigger if they are the same, or smaller if they differ. That makes the code much more simple, but doesn't seem to affect the quality. That simplification allowed me to make the Z80 code quick enough to handle 22050 Hz sample rate.

Actually, I timed it out today and my example code in the archive is about 9.5% fast. I need a couple NOPs here and there to get it to 22050. :D
I'm interested in playing compressed sample by Z80, actually i'm searching the best solution between compression ratio and sound quality.
I tried your CVSD example and imo the quality loss is a bit heavy. I guess it works better with 22 khz playback, did you tested it ?

I've developped an ADPCM player driver, compression ratio is only 1:2 (if you compare to 8 bits pcm) but the driver can handle 2 channels at 22 Khz. Now what i want is a 1 channel ADPCM player but with 1:4 ratio and possible variable playback rate up to 32 Khz, CVSD is a possible interesting choice here =)

Posted: Mon Jul 06, 2009 2:26 pm
by Chilly Willy
Stef wrote:I'm interested in playing compressed sample by Z80, actually i'm searching the best solution between compression ratio and sound quality.
I tried your CVSD example and imo the quality loss is a bit heavy. I guess it works better with 22 khz playback, did you tested it ?

I've developped an ADPCM player driver, compression ratio is only 1:2 (if you compare to 8 bits pcm) but the driver can handle 2 channels at 22 Khz. Now what i want is a 1 channel ADPCM player but with 1:4 ratio and possible variable playback rate up to 32 Khz, CVSD is a possible interesting choice here =)
Well, part of the issue with my example is that I worked from sources that aren't really great, and I didn't pre-filter them so that they'd work better. The min and max delta values also have an affect on granular noise vs qunatization error - I talked about those a bit in the thread in the Exchange forum. Right now, my code used in the example gets about 24 kHz for a single channel. If you wanted faster, you'd have to tighten the code a little more, and I'm not sure where you'd do that.

This code was really just an experiment - ADPCM might be better here. I was going to try something with that next seeing as one of the more common forms of ADPCM compresses 8 bit samples to 2 bits, which is the same 4:1 effective compression ratio I'm getting with this compressor.

One plus about CVSD is that it's compressing 16 bits, not 8. I actually toss out 8 bits of the sample on playback. This code could easily playback 10 or 12 bits on the 32X audio channels instead of 8 bits on the YM-DAC.

Posted: Mon Jul 06, 2009 5:45 pm
by Chilly Willy
Okay, here's a slight update to the demo - it's just the rom binary and the Z80 driver source as that was the only thing to change. I added a few NOPs to the loop to get it as close to 22050 Hz as I care to right now.

musicdemo-2.7z

EDIT: As to how close it is now - it was 9.5% fast, and now it's 0.47% fast. Close enough for government work. :)

Posted: Tue Jul 07, 2009 8:57 am
by Stef
Chilly Willy wrote:Okay, here's a slight update to the demo - it's just the rom binary and the Z80 driver source as that was the only thing to change. I added a few NOPs to the loop to get it as close to 22050 Hz as I care to right now.

musicdemo-2.7z

EDIT: As to how close it is now - it was 9.5% fast, and now it's 0.47% fast. Close enough for government work. :)
I'll try that as soon i got back to home (doesn't have sound here :-/).

Posted: Tue Jul 07, 2009 11:32 am
by TmEE co.(TM)
I just tried it out, and it seems to have quite a lot of noise in the sound, but the playback is very even :)

Make a demo with this file :)
http://www.fileden.com/files/2008/4/21/ ... %20you.mp3

Posted: Tue Jul 07, 2009 3:54 pm
by Chilly Willy
Stef wrote:
Chilly Willy wrote:Okay, here's a slight update to the demo - it's just the rom binary and the Z80 driver source as that was the only thing to change. I added a few NOPs to the loop to get it as close to 22050 Hz as I care to right now.

musicdemo-2.7z

EDIT: As to how close it is now - it was 9.5% fast, and now it's 0.47% fast. Close enough for government work. :)
I'll try that as soon i got back to home (doesn't have sound here :-/).
Do you mean that the system you're trying to run it on doesn't have sound, or that it does but it isn't making sound? I ran this under Gens/GS and on real hardware (CDX) with a flash cart.

Posted: Tue Jul 07, 2009 4:14 pm
by Chilly Willy
TmEE co.(TM) wrote:I just tried it out, and it seems to have quite a lot of noise in the sound, but the playback is very even :)

Make a demo with this file :)
http://www.fileden.com/files/2008/4/21/ ... %20you.mp3
GameMusic.7z

That has your file as song 1, then three more "game" type music scores as songs 2 to 4.

It's not bad, but the hiss is definitely noticeable.

Posted: Tue Jul 07, 2009 4:23 pm
by TmEE co.(TM)
I think Stef meant he did not have sound on that PC he was using (at work ?)

...and the Laser Dance tune sounds like its 8KHz or something :/ (the music genre is called Spacesynth, awesome synthesizer music of the 1980' :D )

but that much samples into so little ROM is definitely impressive :)

Posted: Tue Jul 07, 2009 4:44 pm
by Chilly Willy
The game music I included is really cool, but needs some pre-filtering to minimize the aliasing going on here. I just converted it straight and it's definitely showing the aliasing. Your track actually converted rather well with no work. Other than some noise, it sounds pretty good.

This method of compression isn't really meant for music unless you really have nothing else you can do. Seriously, 1 bit per sample on a Z80? I'm shocked at how good it is. :lol:

Posted: Tue Jul 07, 2009 4:49 pm
by TmEE co.(TM)
it sounds really muffled for being 22KHz....
I myself have worked out one compression method that's tuned for Z80 and 8bit output, I will have to write decoder and encoder.... the hard part :P

Posted: Tue Jul 07, 2009 4:56 pm
by Chilly Willy
TmEE co.(TM) wrote:it sounds really muffled for being 22KHz....
I myself have worked out one compression method that's tuned for Z80 and 8bit output, I will have to write decoder and encoder.... the hard part :P
Well, remember that to help suppress the noise more, I'm sampling the input at half the playback rate, so frequencies beyond 11kHz are gone. I could change the compressor to take the full 22kHz, but it would increase the noise.

I'm currently playing with compression - if you want some help coding, I can do that. I grew up coding assembly on the Z80 and 6502, so it's not a big deal to me. 8)

Posted: Tue Jul 07, 2009 5:18 pm
by TmEE co.(TM)
Usually, more noise > muffled sound. I have not heard the "more noise" in current case so I'm not too sure if the comparsion applies.

As for Z80, I have no issues with that... I could say I'm better with Z80 than with 68K, I have this love-hate relationship with Z80, its total fun most of the time, but there's occasions I'm getting angry... but in the end I work out a fun way to achieve something, and later on I will not understand what I did http://www.fileden.com/files/2008/4/21/1876835/ASM.png :P the fact that I don't comment my code does not make things any easier.....

Writing the playback code won't be anything troublesome, but writing the compressor will be more pain since there's quite some input sample analysis and stuff required...... it could give 70% savings on data size, and it should sound relatively good... but how it turns out in reality is yet unknown