Image Map Image Map
Page 1 of 3 123 LastLast
Results 1 to 10 of 25

Thread: PC Speaker (Square Wave) Sound Effects

  1. #1

    Default PC Speaker (Square Wave) Sound Effects

    I'm not sure how to ask this or what to search for which is probably why my attempts have come up with very little.

    What I think I am looking for is references on creating sound effects using a typical PC Speaker square wave (PIT#2 mode 3). Perhaps example code would be in QBasic using the SOUND statement or C using outp(), etc. It might say, "Here's how to make a coin sound like Super Mario Bros". These would give me a place to start when playing with my own sound effects. If nothing else it would be amusing to hear the kinds of sound effects people have come up with.

  2. #2

    Default

    The simple answer is that you can't.
    A square wave produces a number of harmonics. Without
    amplitude control and the ability to mix multiple square waves
    there is no way to control the harmonic content.
    Old organs used to mix harmonics of different amplitudes
    to clean up the sound.
    A PC speaker can only produce one amplitude.
    If you have a sound board, that is different.
    Dwight

  3. #3

    Default

    You could do a lot worse than taking a look at the tools that mangis wrote for creating MagiDuck: https://youtu.be/gqAwmbhQjrk?t=368 .

    More complicated and impressive effects are possible with faster update rates. I remember a program (possibly a magazine type-in) that made a noise that sounded like water - all you do is set the frequency to a random number as quickly as possible. By varying the update rate and/or distribution of random numbers you can change the sound. You can't really do other things while this is going on, though (at least on slower PCs).

    If you don't limit yourself to square waves, anything is possible right up to playback of arbitrary PCM waveforms using PWM (although, again, processor intensive on slow machines).

  4. #4

    Default

    The MagicDuck (almost misspelled that with the 'i' and 'u' switched -- whoops!) stuff is exactly the sort of thing I'm looking for. I see that it uses the PLAY command which is one step above the SOUND complexity but still not difficult.

    (By PC Speaker square wave I mean putting the PIT channel timer 2 in square wave mode 3 and then alternating the counter reset within a quick and simple loop. This is as opposed to PWM which is cool but overkill for little sound effects.)

    Thanks!

  5. #5

    Default

    Quote Originally Posted by Dwight Elvey View Post
    The simple answer is that you can't.
    A square wave produces a number of harmonics. Without
    amplitude control and the ability to mix multiple square waves
    there is no way to control the harmonic content.
    Well, you can employ manual frequency modulation to create interesting/weird sets of sidebands, although the application thereof is limited. But for bleepy game sound effects, it can be quite interesting - Jill of the Jungle does some interesting stuff with this.
    Computers: Amiga 1200, DEC VAXStation 4000/60, DEC MicroPDP-11/73
    Synthesizers: Roland JX-10/SH-09/HS-80/MT-32/D-50, Yamaha DX7-II/V50/TX7/TG33/FB-01, Korg MS-20 Mini/ARP Odyssey/DW-8000/M1, Ensoniq SQ-80, E-mu Emax HD/Proteus-2, Casio CZ-5000, Moog Satellite, Sequential Circuits Prophet-600
    "'Legacy code' often differs from its suggested alternative by actually working and scaling." - Bjarne Stroustrup

  6. #6

    Default

    Quote Originally Posted by neilobremski View Post
    The MagicDuck (almost misspelled that with the 'i' and 'u' switched -- whoops!) stuff is exactly the sort of thing I'm looking for. I see that it uses the PLAY command which is one step above the SOUND complexity but still not difficult.

    (By PC Speaker square wave I mean putting the PIT channel timer 2 in square wave mode 3 and then alternating the counter reset within a quick and simple loop. This is as opposed to PWM which is cool but overkill for little sound effects.)

    Thanks!
    Magi' actually has its own sound routines for direct control of the PIT. It doesn't use QuickBasics SOUND or PLAY since their timings are very difficult to control unless you're certain your program runs at a constant rate.

    I assume you just want to send a state change to the PIT every frame. So I would suggest making a simple sequence of states (notes, frequencies or other) that you either play one state per frame, or also have some timing data per sequence slot to compare if that state should be fired at that point in time.

    A one-state-change-per-frame is nice in the sense that it's very easy to copy short sequences on top of the current sequence and still have the previous data intact where data wasn't copied to. Like you hear sound effects cutting through music tracks in NES-games and the music taking over as soon as the sound effect is out.


    This is the main sound player source code for MagiDuck in case it helps.

    Code:
    aSoundPlay PROC
      ;
      ; Call this once per frame.
      ;
      ; Play a note from the sound queue,
      ; advance queue pointer by one.
      ; Clear current note after playing it.
      ;
      ; A note will keep playing until a Note-OFF is found in the queue.
      ;
      ; The queue is basically a circular buffer.
      ; A note is a lookup table offset. The lookup table stores the actual frequencies.
      ; 
      xor  bx, bx
      mov  bl, soundPos
      xor  ax, ax
      mov  al, soundQueue[bx]
      mov  soundQueue[bx], 0
      cmp  ax, 1
      jb   noPlay
      ;
      ; Get frequency from LUT
      ;
      shl  ax, 1
      xchg bx, ax
      mov  ax, freqData[bx]
      xchg al, bl
      ;
      ; Set frequency and enable speaker
      ;
      cli
      mov  al,  0B6h
      out  43h, al
      xchg al, bl
      out  42h, al
      xchg al, ah
      out  42h, al
      mov  al, soundOn
      out  61h, al
      sti
      jmp  exitPlay
      ;
      ; Note OFF
      ;
      noPlay: 
      cli
      mov  al, soundOff
      out  61h, al
      sti
      ;
      ; Advance sound queue pointer
      ;
      exitPlay:
      inc  soundPos
      and  soundPos, 63
      
      retf
    aSoundPlay ENDP
    Last edited by mangis; January 23rd, 2017 at 04:36 AM.

  7. #7

    Default

    I'd not mentioned PWM as a PC is really not a real time processor
    and not generally capable of doing PWM at high frequencies on
    its own.
    Dwight

  8. #8

    Default

    Quote Originally Posted by Dwight Elvey View Post
    I'd not mentioned PWM as a PC is really not a real time processor
    and not generally capable of doing PWM at high frequencies on
    its own.
    Well, I was talking about FM rather than PWM (you'd have to drive the speaker output manually to even do PWM at all, and yeah, that's impractical for most purposes,) but FM at anything that counts as anything that counts as audio rates can create quite a variety of interesting tones - if you could swing it using a timer interrupt at even under 1KHz for the modulator, you could do some crazy sounds.
    Computers: Amiga 1200, DEC VAXStation 4000/60, DEC MicroPDP-11/73
    Synthesizers: Roland JX-10/SH-09/HS-80/MT-32/D-50, Yamaha DX7-II/V50/TX7/TG33/FB-01, Korg MS-20 Mini/ARP Odyssey/DW-8000/M1, Ensoniq SQ-80, E-mu Emax HD/Proteus-2, Casio CZ-5000, Moog Satellite, Sequential Circuits Prophet-600
    "'Legacy code' often differs from its suggested alternative by actually working and scaling." - Bjarne Stroustrup

  9. #9
    Join Date
    Aug 2006
    Location
    Chicagoland, Illinois, USA
    Posts
    4,026
    Blog Entries
    1

    Default

    Quote Originally Posted by Dwight Elvey View Post
    I'd not mentioned PWM as a PC is really not a real time processor
    and not generally capable of doing PWM at high frequencies on
    its own.
    What do you consider high frequencies? I've successfully reproduced sampled audio at 29 KHz using PWM, paced by interrupts generated by the PIT (channel 0 for pacing the replay rate, and channel 2 to pulse the speaker), on a 4.77 MHz system. That reproduces frequencies up to 14.5 KHz (and a 29KHz carrier wave, but that's well above the range of human hearing so you don't hear it). Granted, there was no synthesis of data; just retrieval of pre-calced data. But it does work.

    (Of course, 29 KHz playback burns through 512K in about 18 seconds, so lower rates are more appropriate for most applications, like 16KHz.)
    Offering a bounty for:
    - Music Construction Set, IBM Music Feature edition (has red sticker on front stating IBM Music Feature)
    - Any very old/ugly IBM joystick (such as the Franklin JS-123)

  10. #10
    Join Date
    Dec 2014
    Location
    The Netherlands
    Posts
    1,457

    Default

    Quote Originally Posted by Dwight Elvey View Post
    I'd not mentioned PWM as a PC is really not a real time processor
    Why would it not be real time?
    We're talking DOS here, so it's easy to take over the entire system and have full control of every CPU cycle.
    As Trixter already pointed out, the PIT has enough precision for outputting single samples at high rates via the channel 0 interrupt. And you can use the one-shot mode of PIT channel 2 to perform the actual PWM (have the signal high for N ticks).
    It was a reasonably common trick back in the day, the most wellknown implementation being Access Software's RealSound(tm): https://en.wikipedia.org/wiki/RealSound

    The end-part of 8088 MPH shows an alternative approach: https://youtu.be/hNRO7lno_DM?t=6m46s
    It uses the same PIT channel 2 technique to play single PWM samples. However, instead of using timer interrupts to achieve a fixed sampling rate, the entire MOD playing/mixing routine is carefully timed so that each iteration takes exactly 288 CPU cycles (at least on a real IBM 5150/5155/5160). That's about as real-time as you can get.
    This gives you a sampling rate of approx 16.5 kHz.
    If you were to remove the 4 channel mixing from the loop, and just played static single-channel samples, you could reach considerably higher frequencies this way.

Page 1 of 3 123 LastLast

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •