Unphasing harmonics to rebalance sound samples distribution

Probably the most technical forum around. It does not have to be coding related, or assembly code only. Just talk about how you can use the AY chip to do cool sounding things :)
User avatar
jbperin
Flight Lieutenant
Posts: 480
Joined: Wed Nov 06, 2019 11:00 am
Location: Valence, France

Unphasing harmonics to rebalance sound samples distribution

Post by jbperin »

Hi all,

I want to share with you all an idea that I got about sound sample replaying on an AY chip.

I really don't know what it worth but I feel appealed by this idea.

If you're not familiar with AY sound chip, you can leave now because the rest of the post is going to be somewhat hardcore.

If you know about the AY chip, please find the courage to read and tell me what you think because I myself don't know what to think.

Playing sampled sounds on the Oric is done by using the fact that, when no tone is generated by the tone generator of the AY chip, the digital to analog converter of the envelope amplitude controller acts as a simple voltage DAC with output connected to speaker.

(I know it's over simplifying .. ISS is more accurate here )


PCM rendering sound is then possible by making the voltage follows the waveform of the sound we want to replay.

The basic thing to be careful about is that the DAC of the envelope amplitude controller is logarithmically scaled rather than linear.
This is either compensated by using compensation tables on the fly (Oric Tech Demo)
Or the correction can even be incorporated right into the sample (it is done in NY2020demo by preprocessing samples with SampleTweaker.exe of OSDK).

And I'm wondering if we could get a better sound quality by taking into account the fact that the accuracy of the linearisation is not the same on the whole range of value and by incorporating that reality into the sample preprocessing.

Please fasten your seat belt .. i'm going to get you high in a few seconds

The figure below shows what we do when we linearize the DAC log ouput:
Linearisation.PNG
  • X-axis is the value we put in the amplitude register.
  • Y-axis is voltage out of the channel env amplitude
  • yellow line shows what would be the perfect linearisation.
  • green dots shows the uncompensated voltage.
  • blue dots are compensated values
We can see that blue dots are far closer from the yellow line than green dots are .. so it's undoubtedly a very good idea to use this type of compensation.

But if we pay a closer look at the error we still make in spite of the linearisation there's something that immediately catch the attention.
This is shown in the figure below that comes by drawing the difference between blue dots and yellow line.
ErreurLinearisation.PNG
It appears that errors are bigger on high values than on low values.

So the question is : "For a given sound sample, could we get a waveform in which small values are more numerous than big values ?"

The idea behind is that, statistically speaking, the more small values we have in the sample waveform the more accurate we will be in replaying.

But of course, we don't want to destroy the sound. We just want samples that compose the waveform to be as low as possible.

In order to experiment, I took a basic square signal as the one below:
IdealSquare.png
(I took a square because it is full of harmonic and it is easy to decompose for sake of demonstration)
Last edited by jbperin on Mon Apr 19, 2021 7:18 pm, edited 1 time in total.
User avatar
jbperin
Flight Lieutenant
Posts: 480
Joined: Wed Nov 06, 2019 11:00 am
Location: Valence, France

Re: Unphasing harmonics to rebalance sound samples distribution

Post by jbperin »

Then I used Fourier to approach this signal with the following series of harmonic sine .
InPhaseHarmonics.png
When I sum the series of sine, I get :
RebuildSquare.png
which is a (not so bad) approximation of my initial signal. It would require an infinite series of harmonic to get a perfect square.

Now I draw an histogram of samples values to count them on different "slices" of level.
HistogramOfSquare.png
Almost half of samples are in high level values .. exactly in the area where errors are biggest. :-(
Last edited by jbperin on Mon Apr 19, 2021 7:18 pm, edited 1 time in total.
User avatar
jbperin
Flight Lieutenant
Posts: 480
Joined: Wed Nov 06, 2019 11:00 am
Location: Valence, France

Re: Unphasing harmonics to rebalance sound samples distribution

Post by jbperin »

Then I just changed the phase of the first and the third harmonic. The harmonics become:
NotInPhaseHarmonics.png
The only difference with the previous set of harmonics is that the first and the third one are "inverted" (i.e shifted by PI).
And when I sum these sines waves, I get the following waveform:
NewWaveForm.png
OK it doesn't look like a square signal at all.

But it has the same frequency composition and each involved frequency has the same level of energy as in the original signal. It is just changing the phase of some harmonics.

Now if I do the histogram of the new signal, I get the following figure:
NewHistogram.png
Samples are now mainly in the mid of the possible value range which is a more favourable area regarding the figure of our error compensation mechanism.

Thanks for reading.
Last edited by jbperin on Mon Apr 19, 2021 7:19 pm, edited 1 time in total.
User avatar
Dbug
Site Admin
Posts: 4437
Joined: Fri Jan 06, 2006 10:00 pm
Location: Oslo, Norway
Contact:

Re: Unphasing harmonics to rebalance sound samples

Post by Dbug »

What I find funny, is that you started with a square wave, which is exactly what the YM/AY is doing by default: The best way to regenerate a square wave is to actually not play it as a sample at all :D
User avatar
iss
Wing Commander
Posts: 1637
Joined: Sat Apr 03, 2010 5:43 pm
Location: Bulgaria
Contact:

Re: Unphasing harmonics to rebalance sound samples

Post by iss »

Very interesting! I follow your work closely. If you need any measurements with real Oric I'll be happy to help!
User avatar
jbperin
Flight Lieutenant
Posts: 480
Joined: Wed Nov 06, 2019 11:00 am
Location: Valence, France

Re: Unphasing harmonics to rebalance sound samples

Post by jbperin »

Dbug wrote: Mon Apr 19, 2021 6:39 pm What I find funny, is that you started with a square wave, which is exactly what the YM/AY is doing by default: The best way to regenerate a square wave is to actually not play it as a sample at all :D
Yes .. I must admit it is funny to take the square wave by the north face. I realized that.
But I think I took the square for the exact same reason as designers of sound chip used the square .
It is because it is a very rich signal from the spectral point of view.
With one very simple signal to generate, you create tons of frequencies ..
So it's a very good study case.
iss wrote: Mon Apr 19, 2021 7:13 pm Very interesting! I follow your work closely. If you need any measurements with real Oric I'll be happy to help!
Thank you for your proposal Iss ..
For now I need to study a bit more to see if my idea has a chance to work before I go to the prototyping step.
And also I will try to understand the pictures that you gave us and try to make the link between the command you ran and the relative oscilloscope capture.
But I will be glad to ask for some measurement once I have something interesting to measure. So your proposal is highly appreciated.

Do you know if someone has ever modeled the schematic your providing here in term of transfer function ?
User avatar
iss
Wing Commander
Posts: 1637
Joined: Sat Apr 03, 2010 5:43 pm
Location: Bulgaria
Contact:

Re: Unphasing harmonics to rebalance sound samples

Post by iss »

jbperin wrote: Wed Apr 21, 2021 7:18 am Do you know if someone has ever modeled the schematic your providing here in term of transfer function ?
This schematic is copied from the official Oric schematic and represents exactly what's inside Oric
and the oscillograms are captured on pins 1,4,5 of AY-3-8912 (which are connected together).
User avatar
jbperin
Flight Lieutenant
Posts: 480
Joined: Wed Nov 06, 2019 11:00 am
Location: Valence, France

Re: Unphasing harmonics to rebalance sound samples distribution

Post by jbperin »

iss wrote: Mon Apr 19, 2021 7:13 pm If you need any measurements with real Oric I'll be happy to help!
Hi iss,

I've packed, in a .tap, 6 signals that I would love to be captured on pins 1,4,5 of AY-3-8912.
  • 4 bits uncompensated triangle
  • 4 bits compensated triangle
  • 12 bits ramp (atari tables)
  • 8 bits ramp (balanced channels)
  • 8 bits ramp (unbalanced channels)
  • 12 bits ramp (msx tables)
To record the signals with the program AYRAMP.TAP below, follow the procedure:

1- Reset the Oric (to be sure no setup remains from previous test)
2- Load the program AYRAMP.tap and run it
3- Press 1, 2, 3, 4, 5 or 6
4- Record the oscillo
...
5- Stop record oscillo
6- Stop signal by pressing any key on the Oric
7- Go back to step 1 for next signal

I'm sorry for I failed to have a program that can restart without having to leave between two signals :-(
AYRAMP.tap
(19.63 KiB) Downloaded 173 times
Source code of program:
ayramp.zip
(37.11 KiB) Downloaded 166 times
With records of these signals, I think we can very well characterise the behaviour of AY analog output when used in sample replaying mode.

It covers 4, 8 and 12 bits signals with various compensation strategies.
Post Reply