changing sound frequency

This forum is for artists to post their music and graphics or discuss artistic matters that could be used in Uzebox games.
Post Reply
yllawwally
Posts: 73
Joined: Tue Mar 05, 2013 7:29 pm

changing sound frequency

Post by yllawwally »

I want to reduce the maximum frequency of a wav file file playback. I want to make the buffer 131 bytes per frame. Although this would reduce the ram requirements. I am more interested in reducing the sound loading requirements. Music recorded at 8Khz, is obviously not as good. However, it is still good enough. Is this easy to do? It seems just not incrementing the counter for the sample on odd lines would accomplish this. Another question, is sound played during hsync?
User avatar
D3thAdd3r
Posts: 3221
Joined: Wed Apr 29, 2009 10:00 am
Location: Minneapolis, United States

Re: changing sound frequency

Post by D3thAdd3r »

Sound is played during HSYNC. With the VSYNC mixer you are using(assuming based off UzeAmp) most of the magic happens beforehand and the pre-calculated/buffered samples then get output as a byte each line yielding 262*~60 = ~15720hz. I would guess if there is nothing else complicating the matter in HSYNC, repeating the same sample for 2 lines should yield ~7860hz, unless there is something about the PWM output specifics I am unaware of. Then of course whatever needs to happen to change the SD->ram stuff, I'd say it is worth trying.
User avatar
Jubatian
Posts: 1561
Joined: Thu Oct 01, 2015 9:44 pm
Location: Hungary
Contact:

Re: changing sound frequency

Post by Jubatian »

A while ago I did some experiments on various possible simple audio compressions, there I found that if I use 4 bit samples in 16 sample blocks, adding a 8 bit volume for each block (so 9 bytes encoding 16 samples), there is very little audible loss in quality, while the size of the audio data is almost halved.

Streaming from SD is another matter though. A big problem is that to reach the last bytes of a 512 byte sector, you need to read everything until those bytes, so trying to go below loading a full sector has no benefits except if you literally stream it (fetching data just as required, only the streaming accessing the SD card).

Playing at half frequency is possible, there is nothing about PWM which would hinder it in any way, so don't worry about that part.
User avatar
D3thAdd3r
Posts: 3221
Joined: Wed Apr 29, 2009 10:00 am
Location: Minneapolis, United States

Re: changing sound frequency

Post by D3thAdd3r »

It would be ideal if your data requirements for sampling were an integral multiple of the output frequency, but NTSC frequencies do not want to play nice with 512 so seems there is not sense worrying about it. Most solutions would yield some waste bytes anyway. As far as developing a custom inline HSYNC mixer for this to do true streaming, you would have to weigh the development time cost versus what the performance cost a simpler solution might have, actually visible to the end user, would be.

Have you tried directly reading a sector to the vsync buffer and found it wasn't fast enough to keep up in your game? Perhaps you could do 1 game tick with no WaitVsync(1), immediately followed by a sector read for almost 2 frames of audio(which might end the current frame and spill into the next)then logic again. Keep the timing straight by counting vsyncs and doing delays where necessary to make sure you get 60hz logic...or else simply go to 30hz logic like several games do; I really don't think it hurts the final playability for many types of games. I should think between 2 frames, you should easily be able to complete a sector read and get into the next, then it is about how much ram you have to buffer that dictates if that is applicable to your game.

I think something along these lines is faster to develop and so the rest of your game is better with the extra time saved. Maybe not that intricate or amazing though. Human programmer cycles saved is oft the most important kind :lol:
User avatar
Artcfox
Posts: 1382
Joined: Thu Jun 04, 2015 5:35 pm
Contact:

Re: changing sound frequency

Post by Artcfox »

I've written a completely asynchronous (interrupt-based) wave streamer that requires no RAM for buffering. It reads a sample, outputs it to a DAC, and then at the beginning of the next interrupt it latches the DAC and then reads the next 2 bytes. If it hits the end of a sector, it eats the CRC bytes and then eats the busy bytes until it gets another start of data token. While this was not in the context of the Uzebox (it was on an ATmega328) it should still apply. The only requirements were that the wave file was contiguous so it could use CMD18 to stream the data. I tested it with WAVE files with a sample rate of up to 96KHz, so the technique should be good for the lower sample rate that the Uzebox requires, I'm just not sure how nicely it would play with the video modes, which use interrupts of their own.
User avatar
Jubatian
Posts: 1561
Joined: Thu Oct 01, 2015 9:44 pm
Location: Hungary
Contact:

Re: changing sound frequency

Post by Jubatian »

Artcfox: You can't do this on Uzebox since the video frame itself is one huge interrupt routine maintaining cycle sync with the video output. Video mode 3 even uses the interrupt flag as a work bit (since it is known that no other interrupt will trigger). The concept is only workable if you replaced the inline mixer with it, the "eat away" part would define the cycle requirement, and you can't be too tolerant with the SD card there (some 10 reads could be affordable there, which needs it to be quite fast, like in Tempest, although it is more possible here to allow it skipping one or two samples without any drastic effect on output quality).
User avatar
Artcfox
Posts: 1382
Joined: Thu Jun 04, 2015 5:35 pm
Contact:

Re: changing sound frequency

Post by Artcfox »

Jubatian: That's good to know. When it needs to jump sectors, there is essentially a spin loop with a timeout waiting for the data ready flag, and this can take a bit of time on a slow card. That definitely wouldn't work inside a video mode where the timing is quite strict.
yllawwally
Posts: 73
Joined: Tue Mar 05, 2013 7:29 pm

Re: changing sound frequency

Post by yllawwally »

There is audio distortion, that I believe is related to the loading time for the audio from the sd card. Being a rogue like, I could skip taking player input for a frame. I don't think the player would even notice. If waitvsync isn't called, what would happen?
User avatar
D3thAdd3r
Posts: 3221
Joined: Wed Apr 29, 2009 10:00 am
Location: Minneapolis, United States

Re: changing sound frequency

Post by D3thAdd3r »

The main loop will run as fast as possible with no WaitVsync, unless you implement logic in a vsync callback to count frames...the kernel might have something like that now, I forget. Otherwise very simple to add a counter in a callback then at some point wait for the vsync flag before going further. Basically you can do any hertz logic you want.

The main thing is, it is then unpredictabe, if not keeping track, when video rendering will interrupt user code. It could happen in the middle of drawing a map or something, and result in visual tearing or other anomalies. You can check if the vsync flag is set, without waiting if it isn't. Hope that made some sense.
Post Reply