SAMPLING -------- By The Gatekeeper Last issue, there where three great articles about sampling ( by AlphaSys and Frankenstein ! ) This time I would like to tell some more. 6-Bits play routine? -------------------- First of all, I would like to give some more details about the, which Frankenstein called, 6-bit play method which I also used in my MOD-Player. Within the Pokey chip, the four audio channels are mixed together like this: Channel1--| | Channel2--| |------Audio Out Channel3--| | Channel4--| This way, if Channel1 has a volume of 15 and Channel2 one of 15 ( 3 and 4 are zero ) the Audio Out has a volume of 30 ! A little math tells us that 4 channels of 16 different volumes can give us 4*16=64 different Audio Out volumes. Right! Well, not exactly. :-) C1:15 C2:00 C3:00 C4:00 C1:00 C2:15 C3:00 C4:00 C1:00 C2:00 C3:15 C4:00 C1:00 C2:00 C3:00 C4:15 have the same Audio Out. O.K. then it's three less. You still have 61 different output values. Alas, the XL isn't fast enough to output four values at 'almost' the same time. Take a look at this :-Q LDA C1 LDX C2 LDY C3 STA $D201 STX $D203 STY $D205 LDA C4 STA $D207 The fourth channel is 'poked' to the right value 9.03 ms after channel one. In practice this turned out to be too slow... This means the XL can only use up to three channels which gives 46 different output values. Here they are: C1 C2 C3 Result --------------- 00 00 00 00 01 00 00 01 02 00 00 02 . . . . . . . . 14 00 00 14 15 00 00 15 15 01 00 16 <-- !!! 15 02 00 17 15 03 00 18 . . . . . . . . 15 14 00 29 15 15 00 30 15 15 01 31 <-- !!! 15 15 02 32 15 15 03 33 . . . . . . . . 15 15 14 44 15 15 15 45 This is not 6-bits but LOG(46)/LOG(2) = 5.52 bits. This is very unhandy. For example, take the MOD-Player. It has four channels, playing a sample in a resoltion of 4 bits. 4*(2^4) ends up into 64 different combinations, which needs 6 bits to be played without quality loss. Sadly, we only have 5.52 bits... We have to calculate 6 bits into 5.52 bits with this formula : X=6-bits value Y=5.52-bits value Y = X / 64 * 46 Well, try this in Machine Language, and you'll be happy if you get a replay speed of 1 kHz ! Solution : Look-Up Table !!! The Look-Up Table ----------------- You build up three tables, one for each channel, containing 64 values, the values that have to be played for that 6-bits combination. Then, a certain sample can be played by this small routine : TABLE1 EQU $2000 TABLE2 EQU $2040 TABLE3 EQU $2080 PLAY LDX VALUE LDA TABLE1,X PHA LDY TABLE3,X LDA TABLE2,X TAX PLA STA $D201 STX $D203 STY $D205 RTS VALUE DFB 0 On Side-B you'll find a small Turbo-Basic program which builds up these three tables. It'S briefly documented, so I won't tell anything more about it here... Oversampling ------------ Frankenstein mentioned the term oversampling in his theory article. I really agree with him, that the meaning of it is getting vague. A lot of people use it in a different context and don't tell what they actually mean. I'm sorry to say, but to my opinion, Frankenstein is also wrong. He combined two subjects into one. The method of calculating values between to samples and the result of that, having a higher replay frequency. Let's get a clear mind ! (?) Oversampling : When you digitaly record an audio signal, with a frequency spectrum ranging from 20 Hz to 20 kHz, the theory of Nyquist tells us you need a recording frequency of at least 40 kHz. Two times the highest input frequency that is. Now, it's called oversampling when the samplefrequency is not twice the input frequency, but e.g. 5 or 10 times. It's used to lessen the terrible effect of high frequencies. But what about that calculating of values between two taken samples ? Well, that is called interpolation. CD-Players use interpolation technics to 'recover' lost data. Take this situation. An (imaginairy) 2-bits CD-Player has read the following values : A 11|* *l ^ 10|** *o * | 01|** **s** 00|*****t** ---------- abc --> t After the second occurance of %11, the player wasn't able to regain the sample information. Now it can just play the %11 again ( 0-order ), but it can also calculate the so called 'expected value'. b = ( a + c ) / 2 This is interpolation of the first order. ( Lineair Interpolation. ) There are also higher order interpolation technics using logarithmic functions, sines and cosines etc. As Freddy told us, it's possible to calculate all averages of two samples, and play this one between the two known samples. The resulting data-stream has to be played twive as fast as the original, that results in 1 time oversampling. Processing the 1 time oversampled data stream once again results in 2 times oversampling etc. Analog Filters -------------- With two components, a resistor and a capacitor, you can filter out either low and high frequencies. Take a look ate the following two schemes : ---- In -----| R1 |-------- Out ---- | | ___ ___ C1 | | GND _figure 1_ ||C1 In -----||------------ Out || | - | | |R| |1| | | - | GND _figure 2_ Figure 1 will push down high frequencies above a certain f(R,C) and figure 2 pushes down all frequencies below f(R,C). f(R,C) = 1 / ( 2*pi*R*C ) e.g. R=1K C=10 pF --> f=15.9 kHz All frequencies above or under 15.9 kHz will be pushed down. Digtal Filtering ---------------- First of all, you don't need to use oversampling for digtal filtering. It's based on taking not a 100% of the next sample taken. That way, high frequencies will fade away. You can simulate both RC-Filters seperately, but not together. The essention of this algorithm is to add only a fraction of the new sample to a fraction of the old one. Watch this :-) a=1/2 b=1/2 (-: Last Sample New Sample Calculated... (Y) (X) (aY+bX) 128 140 134 134 (!) 144 139 139 151 145 145 143 144 Mention that a+b=1 and a>=b. All quick changes of amplitudo will dissapear. The imaginairy f(R,C) is very low when a>>b. To simulate the other RC-filter, bY has to be subtracted from aX. ( aX - bY | a+b=1 v a>=b ). All sorts of exotic filters are possible. Modulation Technics ------------------- There is a whole range of different modulation methods. Named here are only a few... PAM - Pulse Amplitudo Modulation %11 |* %10 |** %01 |*** %00 ----- abcd PPM - Pulse Place Modulation Pls | * * * ----------------- a b c d PWM - Pulse Width Modulation Pls |*** ** * ----------------- a b c d PNM - Pulse Number Modulation Pls |* * * * * * --------------------------------- a b c d PCM - Pulse Code Modulation Pls |*** * --------- a b c d PAM is used on our XL/XE and so it is on ST(E)'s, Amigoes, MSX(2) and C64s. You can generate and n number of pulses, all with a different amplitudo. IBM compatible PC'S could use PAM, but they only have one bit and can therefore only generate block-waves. But wait a minute, all the other modulation technics only require two states. ( Pulse or no pulse ! ) That's why MOD-Players on PC use PWM. They just vary the width of the pulse. Be carefull, every pulse, no matter the length of the one before, starts at a certain frequency! Offcourse, there are a lot of disadvantages. The frequency of the starting points have to be very high so that one pulse sounds like PAM. Luckily the PC sound producer is VERY VERY BAD. It isn't able to produce sounds higher than 8kHz, therefore a starting point frequency of about 10 kHz is fine. Still you need at least a 12 MHz AT. Imagine you want an 4-bit play routine. The length of one period is 16. ( e.g. 15 times 1 and one time 0. ) Now the replay rate of all zeroes and ones is 160 kHz !!! ( This is still slow compared to the 2.56 MHz for an 8-bits player !!! ) Probably you think, why do you tell me this ? Our XL/XE is much too slow for this ! Then I must admit that you are right, but this last paragraph about modulation is not meant to be used in practice on an XL. It's just some more theoretical background. Maybe it inspires somebody to get one of these modulation technics working on a XL. ( What about some external hardware, producing a 1-bit PWM output from an 8-bit paralell digital input ? ) Well, this is the end. ( Finally. ) Let me tell you this, Frankenstein, I can secure you that there were two people that liked your article. ( Me and you that is ! ) I hope we like this one also ! Editor: Yes, I liked it. Thanx!