HOW YOU CAN PRODUCE DIGITAL SOUND Some preliminary explanations you should read before looking into xsd.txt .... ... and some detailate descriptions of the internal working of 386Mixer. If you think graphics routine are complex, wait to see how sound generation works. First of all, "sound" is the input we collect with our ears. The thing we call sound is actually a sum of waves of pressure propagating thru solid,liquid and gas and "bouncing" into our ears. Usually you can think at "plain" waves as made by a ponderated sum of sinusoidal waves of various frequencies (frequency is the reciprocal of the sinusoidal wave period and it is measured in Hertz) (Hertz = 1/sec). Our ears can detect sinusoidal waves with a frequency between 50..20000 Hertz (well, some people has a wider range). Below 50hz we have infrasounds (that we don't hear but that can cause us various troubles if too much poweful)(remember a wave carries energy) Above 20000 Hertz we have ultrasounds [we don't hear 'em because they are outside the resonance frequency range of our "sound to neural" converter system (it is a mechanical amplifier coupled to a "flash adc" made of resonating cell devices) (cool!) and they too can cause us troubles if too much loud]. Because sounds are just waves, they have all the nice attributes of waves. Do you remember all those nasty theorems about information transmission ? Well! now you can hear :) how they are useful: If you modulate a sinusoidal sound generator cycling at twice the maximum hearable frequency you can replicate "perfectly" (for a human) a sound. This explains why the base frequency of choice for high quality digital sound is around 44000Hz (a little above twice 20000Hz). The "modulation frequency" is usually called base frequency or playback frequency (because from it you can "simulate" the lower frequency waves). Going below 44000Hz you produce "not so good" sound, but depending on the application you can get very low, for example "telephone line" playback quality is around 8000Hz and radio station quality is around 11000...22000Hz (depending if AM or FM ). Then comes how smoothly you amplitude-modulate the base frequency 8bit sample resolution on 8000Hz is digital phone line quality 16bit sample resolution on 44000Hz is compact disk quality. Notice i don't talk of sound/noise ratio, this depends on the hardware you use and on the "sound synthesis" method you use. Digital sound playback adds some "noise" because of time and amplitude quantization, so the higher the sample resolution and the playback frequency the better, but this raises the i/o bandwidth utilized by the sound system. With 8bit at 8000Hz the sound system manages around 8Kbyte/sec but this if you play only one sound at a time (one voice), if say you want to make 20 sounds playing at the same time you have to add 'em together before sending 'em to the sound reproduction system and this means having to manage 20*8= 160Kbyte/sec ... .... and if you want 20 voice CD quality, you have to manage around 44*2*20 = 1.760 Mbyte/sec. This explains why high-quality soundcards usually comes with DMA support and on-board processing power to reduce the cpu work. Ok! I know the basics, but what i have to do to make sound? First of all you need a DAC (Digital to Analog Converter): It is a circuit that takes in input a digital value anc converts it to an analog value (usually a voltage level). If you properly connect a DAC to a speaker you can make sound by sending sequences of digital values to it. PC BEEPER: The original PC has a "poor man" DAC made by a 1bit TTL output connected to the PC speaker [ well, if you dare to call "airplane" a cow with a propeller hat, you can call the pc-beeper system a 1bit DAC :) ]. It was originally intended to make "buzz" and "beeps" like old terminals. To make these things easier, it is connected to the channel 2 of the Intel 8253 PIT (Programmable Interval Timer). The PIT is the "timer chip" every PC has, this can "count" a variable time interval and generate various "count related" outputs. The 8253 has three indipendent counters and outputs (time channels) [ The "system timer" 18,2Hz IRQ 0 (INT 8) is the PIT channel 0 output channel 1 was used for ram refresh on older systems and channel 2 output can be connected to the speaker by flipping a bit in the keyboard controller output port] What's more, the PIT can GENERATE SQUARE WAVES with a frequency range between 18,2Hz to 1193180Hz. So if you connect the PIT channel 2 output to the speaker and program channel 2 to emit a square wave with a frequency higher than 50Hz you can hear a constant sound coming from the speaker. This is good to make "buzzs and beeps" (as it was originally intended) but it is not very good to make sound (as it was NOT intended). A square wave is nearly the worst thing you can use for "amplitude modulation" sound synthesis (a "square" wave is equivalent to a big pack of sinusoidal waves), this means that if you use 1bit square wave modulation you ADD LOTS OF NOISE to digital sound sample you want to playback because you dont' modulate a single sinusoidal wave you actually modulate LOTS of different sinusoidal waves! (this explains why digital sound on the pc-speaker is so "noisy"). You can partially reduce noise AND raise digital samples resolution this is you think that the pc-speaker output is connected to a speaker circuit that is equivalent to a "poor man" low-pass (RC) filter (actually, it's more like an RLC, but what the heck!). "Pulsing" trains of square waves in a frequency range that goes between "speaker membrane fully oscillating" to "speaker membrane not oscillating" you can have smoother modulation control. You can perform sort of "pulse width modulation" sound synthesis with a NEARLY 6bit resolution (the "low-pass smoothed" square wave will look like a smells-like-a-sinusoid wave, with amplitude depending on how "large" was the square wave). (see the pc-speaker code file i included for more info). DACs ON THE PRINTER PORT: Well, what if you want to use something better and are short of cash? YOU MAKE DO A DAC OUTPUT ON YOUR OWN!!!! Now put on your propeller hat, look in a digital elctronics book about DACs and you 'll discover the uncanny truth: DACs ARE EASY TO BUILD ON YOUR OWN!!!! If you don't believe me, look into jdac.txt it includes all the info you need to make a dac to plug into the printer port. If you can't buy a soundcard but have enough resistors and capacitors, you can make a simple 8bit "scaled resistors" dac and plug it into the printer port (it has an 8bit digital output). There are lots of "sound device on printer port" devices around (the SoundPlayer, various COVOX devices, the Disney SoundSource and lots of other things) but they cost more than their value. If you give a look at jdac.txt you will discover that an equivalent thing can be build spending less than 10$ (or nothing, a friend of mine simply "cannibalized" old radios and other electronic things and reutilized resistors and capacitors) [ well, the same guy made a 100Watt amplifier "reutilizing" things ("big guts" RMS watts, not those idiot-fooling peak-to-peak VA ratings you usually find stamped in big letters on stereo packages) but the recycled dac is really easy to make]. [ By the way, recycling electronic devices is "ecologically correct" costs you nothing (once you have the "basic tools" and data books) and you can put your hands on "big gun" electronics you usually don't find in the electronic stores (like poweful electric engines,big power converters, laser devices, microwave generators and other things like that) ] (look into tronics.txt for more info about "recycled" items). THE PROBLEM WITH "simple" DACS: But to playback the samples you have to change the dac output at a constant rate (equivalent to the "playback frequency"). On a "just the dac" system you have to reprogram the timer interrupt frequency (PIT CHANNEL 0) to make it play at at least 8000Hz, and then you have to hook that irq so the interrupt handler will change the dac output. Such high interrupt rate spells disaster because processing an irq request takes some time, time you steal to the execution of your "main" programs! Usually on a 386, 16000hz is the maximum pc-speaker playback rate you can get (with your main program slower than an 8086 turtle). So it's time to call in a good soundcard for sound support. MORE THAN "just a dac", THE SOUNDBLASTER: A SoundBlaster (SB for short) or equivalent card has an on-board 8bit DAC (the SB16 has a 16bit DAC) PLUS a very weak DSP chip that helps reduce the cpu burden [Yeah! and my Fiat Uno car is a very weak Ferrari! ;) ]. What's more it has DMA support. Once you program the SB and DMA hardware the SB card can "self-time" and fetch sound samples from the pc memory using the DMA system. The SB DAC uses PCM (pulse code modulation) on 8bit cards (a sample gets converted to a "voltage pulse" that drops to zero if you don't send more samples) and SAM (signal amplitude modulation) on 16bit cards (plus 8bit PCM support) (with SAM a sample gets converted to a "voltage level" that does not change if you don't send other samples). If the board has stereo output support (like the SPpro and successive) it can "distribute" output on left and right speaker outputs (the first sample goes to the left speaker, the second to the right speaker, the third to the left, the fourth to the right and so on). This explains why on a SBpro you can have max. 44000Hz MONO output OR max. 22000Hz STEREO output. An SB or equivalent card has just ONE DIGITAL VOICE (can play only one stream of digital sound samples) but "plays itself" up to 64kbytes of digital sound (this is a dma limit) and by way of an interrupt handler managing the SB irq line it can be "cpu reprogrammed" on the flight so when a block of data ends plying, another block of data can be started. Told in another way: Once you program the dma chips and the SB chip you can make "automatically play" up to 64k of sound data without and at the same time the CPU can make other things. MULTIPLE VOICES ON A SINGLE VOICE DEVICE: So, what to do if you want multiple voices on a SB card? You simply "add 'em together" in a single sound sample block and then send it to the SB card by way of DMA. This of course can mean lots of cpu work again, but it requires less works than with a "just the dac" device, because you spend time mixing the sound data, but you don't spent time sending it to the output dac (the souncard driven dma system does it all). "HARDWARE BASED" MULTIPLE VOICE DIGITAL OUTPUT: So, what to do if you want "something better" ? Let's think again, a SB card is a SINGLE VOICE WAVETABLE DEVICE (also known as a DAC plus a few support chips :} ). A wavetable device is: a DAC plus "data feed" circuits that plays a sound using a sequence of sound samples in "table" stored into ram with programable sound volume and playback rate. What about a MULTIPLE VOICE WAVETABLE DEVICE? Immagine a soundcard with multiple programmable wavetable devices ... ... like a Gravis UltraSound for example! (Or the SB32AWE, or the Ensoniq or ...) Those cards have a multiple voice wavetable system, but they cannot fetch their sound samples from system ram (there are very few DMA channels available and a single 16bit ISA DMA channel tops at 2Mbyte/sec), so they need on-board ram to store wavetables to play. With such a device you can play multiple digital voices with nearly zero cpu overhead. No more problems, now? Not actually, what if you have too few ram and too many wavetables to play? Well, you can use the DMA system "a la SB" and fetch data from memory for one digital voice, if you need more voices from system ram, you can mix them like with the SoundBlaster. Another thing you can find with "wavetable hardware" is panning control (panning is the "direction" from where the sound is coming if you immagine it between the left and right speaker) and this can help you too if you want "stereo sound source positioning". EFFECTS NOT SUPPORTED DIRECTLY: You may think this is enough for most of the sound effects you want, isn't it? Well, some nice things are still missing, but they can be added by way of clever irq handlers that reprogram the sound hardware "on the flight". Doppler effect: the alteration of the wave frequencies caused by the motion and/or acceleration of the sound source. Echo and surround: "reflected sound" effects. 3D sound: a complex "differentiated-controlled echo" simulating the sound distortion caused by ears' padillions. We detect the tridimensional position of the sound source by way of this distortion. To simulate 3d sound you need multiple voice playing the same mono sample with different panning,volume and delays. I've included "hooks" for PLAYBACK RATE RAMPING in the mixer and at device driver level. Once you have ALL the other effects AND playback rate ramping you can perform lots of effects including plain doppler effect and other things like the doppler distortion caused by sound sources moving/accelerating at high speed around you. (if you want to make a big-gun-mecha simulator including high-speed close-range combat this is a thing you HAVE to add) [ this way the player will be able to "hear" if a mecha BEHIND him is changing speed or getting closer or more far !!!!] [Immagine this "audiovisual" support in a mecha simulator where you can fight with fists and kicks too!!] [ So for example you can "hear what's going on behind you" and so you can "sweep" 180 degrees and slash with a razor kick (have i told you your mecha has razor boots?) the mecha attacking you from behind ] (if you have enough cpu power) (maybe you will hear :) something about this in 1997)