home *** CD-ROM | disk | FTP | other *** search
/ Audio 4.94 - Over 11,000 Files / audio-11000.iso / msdos / tools / paper / paper.txt < prev    next >
Encoding:
Text File  |  1994-03-16  |  21.1 KB  |  493 lines

  1.  
  2.  
  3.            ACHIEVING INEXPENSIVE DIGITAL AUDIO ON PCS
  4.                     FOR EDUCATIONAL PURPOSES
  5.  
  6.  
  7.                        David T. Chappell
  8.                  Department of Computer Science
  9.                  North Carolina State University
  10.                        Raleigh, NC  27695
  11.  
  12.  
  13.                           INTRODUCTION
  14.  
  15.      In the past few years, the computer industry has been slowly
  16. gaining interest in computer-generated speech and sound.  In this
  17. area, the Apple Macintosh and Commodore Amiga provide built-in
  18. sound control so that their machines can play digitized
  19. recordings with little effort from the programmer.  IBM, however,
  20. has chosen not to include advanced sound capabilities in its line
  21. of personal computers.  The market has shown that relatively few
  22. people will buy speech or sound add-ons from either IBM or third
  23. parties.  Until IBM makes such hardware standard, speech software
  24. will encounter great difficulty in gaining acceptance.  It is in
  25. this regard, however, that mathematics, engineering, and software
  26. can come to the rescue:  it is possible for a PC to play good
  27. quality sound without additional hardware.
  28.  
  29.  
  30.                            BACKGROUND
  31.  
  32.      Digitized sounds consist of the numerical values for the
  33. amplitude at regular time intervals.  [See figure 1.]  Along with
  34. the amplitude data, the number of samples taken per second must
  35. be recorded.  By reproducing the amplitude changes at the
  36. original rate, the sound can then be played back.  Much research
  37. has gone into digitized sound, and it is now possible to produce
  38. digitized recordings which, to the human ear, are identical to
  39. their analog counterparts.
  40.      Once recorded, digital signals can be stored in a variety of
  41. formats.  Pulse amplitude modulation (PAM) is the standard method
  42. of representing analog data.  [See figure 2.]  In this method,
  43. each piece of data represents the amplitude at one instant in
  44. time.  Pulse width modulation (PWM) treats each piece of
  45. information as the duration of a pulse which starts at a regular
  46. frequency.  [See figure 3.]  Pulse code modulation (PCM) is the
  47. most common in digital audio recording.  [See figure 4.]  In PCM,
  48. each bit records whether the amplitude is high or low at each
  49. point in time. [1]
  50.  
  51.      Several brands of personal computers include dedicated sound
  52. chips.  The Macintosh, Amiga, and other computers can easily
  53. produce high-quality sound by using dedicated hardware.  On these
  54. computers, many programs are enhanced by the addition of sound
  55. and speech.  Likewise, IBM and a multitude of other companies
  56. have produced boards that allow PCs to record and playback
  57. digitized sound.  A lack of standards, however, limits the use of
  58. these boards, and few commercial programs use them.
  59.      Over the years, several attempts have been made to play
  60. digitized sound on PCs without special hardware.  A number of
  61. these have appeared in the public domain.  Commercial software
  62. has had greater success, but most of these programs produce
  63. rough-sounding speech.  With the recent rise of interest in sound
  64. and pictures, a few developers have produced good, intelligible
  65. speech and sounds.  The work presented here surpasses past
  66. attempts and adds to that of other successes.
  67.  
  68.  
  69.                       MATERIALS AND METHODS
  70.  
  71.      The Intel 8253 programmable interval timer, found on all IBM
  72. PCs and compatibles, is a flexible counter chip.  It has three
  73. 16-bit channels.  Each channel produces an output signal based on
  74. an input signal and an arbitrary 16-bit number.  The chip's six
  75. modes can produce varying types of output.  Table 1 summarizes
  76. the six modes.  Refer to Rosch [2] or Sargent and Shoemaker [3]
  77. for more details.
  78.  
  79.               Table 1:  Intel 8253 Operating Modes
  80.  
  81.                0 - interrupt on terminal count
  82.                1 - programmable one-shot
  83.                2 - rate generator
  84.                3 - square-wave generator
  85.                4 - software-triggered strobe
  86.                5 - hardware-triggered strobe
  87.  
  88.      On IBM PCs, the 8253's first channel increments the time-of-
  89. day clock, the second channel refreshes the DRAMs, and the third
  90. channel sends sound to the speaker.  The timer/counter of AT
  91. class machines is based on the Intel 8254-2 chip and is
  92. functionally equivalent to the 8253.  The PS/2 line has an 8253
  93. that is used the same way, except that a separate chip refreshes
  94. memory, and the second channel of the 8253 is used for
  95. diagnostics or is unassigned. [2]
  96.      The 8253 can only give two possible output states:  0 and 1.
  97. Possibly because of this limitation, the chip's use for sound
  98. production in PCs has typically been limited to being a square-
  99. wave generator (mode 2).  When functioning as a square-wave
  100. generator, the chip's output is equal to its input frequency
  101. (1.193 MHz on PCs) divided by a 16-bit number that is input to
  102. the chip by the programmer.  The output is a square wave whose
  103. high and low periods are equal.  The range of the chip falls
  104. between 18.2 hertz and 1.193 megahertz [4].  Mode 2 is used by
  105. channel 0 for the time-of-day clock and by channel 2
  106.      Despite the two-state output limitation, the 8253 can also
  107. play digitized sound.  When put in mode 0, the output is low for
  108. the duration of a number of input pulses equal to a programmed
  109. 16-bit number, after which the output goes high.  The net result
  110. is that the resulting wave is in the form of PWM.  [See figure
  111. 5.]
  112.      To program PWM sound output on the 8253, several steps must
  113. be taken.  First, the programmable peripheral interface (PPI)
  114. must be initialized to the desired mode.  Then the 8253 must be
  115. initialized before the sound data can be sent to it.  Table 2
  116. summarizes chip ports.
  117.  
  118.                 Table 2:  Chip I/O Port Addresses
  119.  
  120.                 Port                    Dec  Hex
  121.                 PPI chip                97   61
  122.                 8253 Channel 0          64   40
  123.                 8253 Channel 1          65   41
  124.                 8253 Channel 2          66   42
  125.                 8253 Control Word       67   43
  126.  
  127.      The PPI must be initialized to allow the 8253 to control the
  128. speaker.  The two lowest bits of port 97 must be turned on.  The
  129. other bits of this port should remain untouched since they are
  130. used for other purposes. [4]
  131.      Port 67 is the control word register which initializes the
  132. chip.  Each counter is initialized by sending this port one
  133. control byte.  Table 3 shows the meanings of the bits in the
  134. control word.
  135.  
  136.            Table 3:  Meaning of Control Word Register
  137.  
  138.           bit 0     = 0, count in binary
  139.                     = 1, count in Binary Coded Decimal
  140.           bits 1-3  = mode number (0 to 5 in binary)
  141.           bits 4,5  = 00, latch current count for reading
  142.                     = 01, read/load low byte
  143.                     = 10, read/load high byte
  144.                     = 11, read/load low byte, then high byte
  145.           bits 6,7  = counter number (0 to 2 in binary)
  146.  
  147.      (Bit 0 is least significant; 7 is most significant)
  148.  
  149. When reading both bytes of the 16-bit value, a latch command
  150. prevents the count from changing between reading the high byte
  151. and the low byte.  Latching is not needed when reading only a
  152. single byte.  For example to set the chip to generate musical
  153. tones in mode 3 the control word is 182 (B6 hex).  For digital
  154. audio via mode 0, use 176 (B0 hex).
  155.      Ports 64, 65, and 66 are used to read and write to timers 0,
  156. 1, and 2 respectively.  Data sent to these ports becomes the 16-
  157. bit number used to affect output.  If only one byte is sent, the
  158. other byte retains its previous value.
  159.      Listing 1 shows the general algorithm for 8-bit digital
  160. audio output with the 8253.  8-bit quality is achieved by leaving
  161. the high byte constant and sending data only to the low byte.
  162.  
  163.              Listing 1:  Algorithm for Digital Audio
  164.  
  165.           Load Digital Audio Data;
  166.           value = InPort(61h);     -- Initialize PPI
  167.           OutPort(61h, value OR 3);
  168.           OutPort(43h,B0h);        -- Initialize 8253
  169.           OutPort(41h,00h);
  170.           OutPort(41h,00h);
  171.           OutPort(43h,90h);
  172.           loop until end of data   -- Play sound
  173.                OutPort(41h,Data);
  174.                Wait Until Data Passes;
  175.           OutPort(43h,B6h);        -- Restore 8253
  176.  
  177.      Listing 2 gives an example program written for Turbo C.
  178. Note that the user must specify whether the input data is signed.
  179. Since the playback method using the 8253 must use unsigned data,
  180. a scaling factor of 128 must be added to all signed data.
  181.  
  182.            Listing 2:  Turbo C Code for Digital Audio
  183.  
  184. #include <conio.h>
  185. #include <dos.h>
  186. #include <io.h>
  187. #include <stdio.h>
  188. #include <stdlib.h>
  189.  
  190. /*   SOUND.C
  191.  
  192.      Author:   David Chappell
  193.      Version:  1.46c
  194.      Date:     24 June 1990
  195.      Method:   8253 PWM method
  196. */
  197.  
  198. FILE *soundfile;    /* input data file */
  199. unsigned long size; /* size of input file */
  200. int wait;        /* time to wait between sending samples out */
  201. unsigned char offset;   /* change signed samples to unsigned */
  202.  
  203. void error(char message[]);
  204. void playfile(void);
  205. void startspeaker(void);
  206. void openfile(void);
  207. void stopsound(void);
  208. void main(void);
  209.  
  210. void error(char message[])
  211. /* Purpose:  handles errors */
  212.      {
  213.      fprintf(stderr,"\nERROR:  %s\n",message);
  214.      exit(-1);
  215.      }
  216.  
  217. void playfile(void)
  218. /* Purpose:  loads file and plays digitized sound */
  219.      {
  220.      unsigned int count, pause;
  221.      char curr;
  222.      char *inputbuffer;
  223.  
  224.      if ((inputbuffer=(char*) calloc(size,sizeof(char))) == NULL)
  225.           error("Not enough memory to load file");
  226.      fread(inputbuffer, size, 1, soundfile);
  227.      disable();              /* disable interrupts */
  228.      for (count = 0; count < size; count++) {
  229.         curr = *(inputbuffer+count) + offset;
  230.         output(66,curr);
  231.         for (pause = 0; pause < wait; pause++);
  232.         }
  233.      enable();               /* enable interrupts */
  234.      }
  235.  
  236. void startspeaker(void)
  237. /* Purpose:  initialize speaker for output */
  238.      {
  239.      outp(97,inp(97) | 3);    /* set PPI */
  240.      outp(67,176);       /* send initial data to timer */
  241.      outp(66,00);
  242.      outp(66,00);
  243.      outp(67,144);       /* prepare timer chip to receive data */
  244.      }
  245.  
  246. void openfile(void)
  247. /* Purpose:  opens input file */
  248.      {
  249.      char choice;        /* key hit by user */
  250.  
  251.      clrscr();
  252.      puts("What file do you want to hear?");
  253.      if ((soundfile = fopen(gets(NULL),"rb")) == NULL)
  254.         error("Unable to open sound file");
  255.      fseek(soundfile,0,SEEK_SET);
  256.      size = filelength(fileno(soundfile));
  257.      printf("\nFile size = %lu bytes\n\n",size);
  258.      printf("What delay time do you want in FOR counter? ");
  259.      scanf("%d",&wait);
  260.      printf("Is the data signed? ");
  261.      choice=getche();
  262.      if ((choice=='Y') || (choice=='y'))
  263.         offset=128;
  264.      }
  265.  
  266. void stopsound(void)
  267. /* Purpose:  resets speaker to stop sound */
  268.      {
  269.      outp(67,182);       /* restore timer to mode 3 */
  270.      outp(66,51);        /* set channel 3 to power-on value */
  271.      outp(66,05);
  272.      nosound();
  273.      fclose(soundfile);
  274.      }
  275.  
  276. void main(void)
  277.    {
  278.    openfile();
  279.    startspeaker();
  280.    playfile();
  281.    stopsound();
  282.    }
  283.  
  284.  
  285.                              RESULTS
  286.  
  287.      The method described thus far has several limitations when
  288. put into practice on PCs.  The 16-bit quality of the chip reduces
  289. to 7 bits at most sample rates.  Also, a background tone is
  290. produced along with the desired sound because of the use of PWM.
  291. Both of these difficulties arise from timing problems.
  292.      The method described thus far has the capability of yielding
  293. high-quality 16-bit sound.  The code given, however, can only
  294. produce approximately 7-bit sound.  Although the 8253 has the
  295. ability to play 16-bit data, the timing limitations of the PC
  296. restrict the length of each pulse.  In order to produce sound at
  297. the rate of about 8-13 kHz, only about six or seven bits of data
  298. are processed before the next piece of data must begin output.
  299. At a slower sample rate of about 4-7 kHz, seven to eight bits of
  300. accuracy can be achieved.  A higher input frequency would resolve
  301. this difficulty; however, this hardware problem can not be easily
  302. accomplished in PCs but would be feasible in other applications
  303. of the 8253.
  304.      The maximum data size (volume) possible can be calculated
  305. mathematically.  The 8253 input rate divided by the output sample
  306. rate yields the number of time periods that pass before the next
  307. sample begins play:
  308.  
  309.      Maximum value = 1.193 MHz / sample rate
  310.  
  311. For example, the maximum volume for an 8 kHz sample is 147.  The
  312. 1.193 MHz input frequency that feeds the 8253 limits the chips
  313. sound capabilities.
  314.      As an annoying side-effect, the provided algorithm creates a
  315. background tone.  Due to the nature of PWM, at the beginning of
  316. each piece of data, the output goes from low to high. [See figure
  317. 4.]  This periodic oscillation produces a pitch equal to the
  318. frequency at which the sound is played.  For example, an 8 kHz
  319. sample will produce a background tone of 8 kHz.  The resulting
  320. tone overlays the digitized sound output.  A pitch of 18 kHz or
  321. greater is high enough that the human ear can not detect it.
  322. Thus, any sample of this frequency will not produce an audible
  323. background tone.  If a given sample is not of high enough
  324. frequency, this problem can be alleviated by outputting each
  325. piece of data multiple times in rapid succession so that the
  326. background tone is of such a high frequency that it is inaudible.
  327. For example, by playing each datum of an 8 kHz sample three
  328. times, the resulting pitch will be 24 kHz.  The first problem,
  329. however, becomes dramatic when a moderate-speed sample is sent
  330. repeatedly:  in order to maintain the original sampling rate, the
  331. 8253 has time to process fewer and fewer bits for each datum.
  332.  
  333.  
  334.                            DISCUSSION
  335.  
  336.      The development of this digital audio playback method has
  337. several implications and possibilities.  A variety of
  338. applications, from games to word processors, can use voice and
  339. sound.  On multi-tasking operating systems, sounds can easily be
  340. played in the background.  Other computers could use the same
  341. ideas for audio output.  When combined with extra hardware, this
  342. method can form a complete audio I/O system.  When a PC acts as a
  343. terminal, this procedure will allow mainframes and minicomputers
  344. to play digitized sound.
  345.      Speech interaction is currently put to several uses,
  346. especially to help disabled users.  For example, IBM's
  347. SpeechViewer helps deaf children and adults improve
  348. pronunciation.  A microphone and speech recognition allow the
  349. personal computer to understand the user's voice.  For blind
  350. users, IBM's ScreenReader program can vocally relate the text
  351. that appears on the monitor.  Theoretical physicist Stephen
  352. Hawking uses a computer to talk despite his crippling disease.
  353. There are numerous other instances of disabled users benefitting
  354. from talking computers.
  355.      There are also many computer programs that help people learn
  356. to read and write.  Across the nation, children can listen to the
  357. computer talk as they use IBM's Writing to Read software.
  358. Illiterate adults gain an invaluable skill as a computer speaks
  359. and displays words on the screen.
  360.      In addition, many musicians use computers to produce and mix
  361. sounds.  Computers can now produce rich tones that rival musical
  362. instruments.  Musical synthesizers are actually specialized
  363. computers built for the purpose of producing sound.  With the aid
  364. of digital signal processor boards, personal computers can make
  365. music equal in quality to the better synthesizers.
  366.      By reducing the need for extra hardware, digitized sound can
  367. easily be added to other programs.  As an obvious example, games
  368. can use sound for both special effects and general entertainment.
  369. As multimedia becomes more popular, sound becomes a necessity.
  370. Useful applications, from word processors to spreadsheets, can
  371. speak to help visually impaired users.  For example, allowing
  372. speech output from a word processor would do wonders to assist
  373. visually impaired writers and programmers.
  374.      Adding speech to personal computers would benefit new users.
  375. Inexperienced users would find a computer to be much friendlier
  376. if it could speak to them.  By providing verbal output in
  377. important areas such as error handling, the computer can help new
  378. and disabled users.  A user interface that includes speech can
  379. help bring computers to the level of interaction that humans use
  380. with each other.  Thus, nearly all types of software can benefit
  381. from the addition of speech and sound capabilities.
  382.      When running under PC-DOS, only one program can be run at a
  383. time.  The only way to allow the computer to play recorded sounds
  384. while continuing other work is to modify interrupts.  Under
  385. multi-tasking operating systems, such as OS/2 and Unix, the 8253
  386. could be continually fed data in a background task while the main
  387. program continues.  Playing sound in the background gives more
  388. flexibility.  For example, a communications package could
  389. verbally report an error while continuing to receive data, or a
  390. demonstration could play music while displaying graphics.
  391.      The data storage method used here is compatible with many
  392. others.  A huge number of digitized samples are available from
  393. Macintosh, Amiga, and Atari ST computers.  Several PC expansion
  394. boards also use the same storage method.  Data recorded on any of
  395. this hardware can be played back on an ordinary PC.  Furthermore,
  396. by purchasing an available expansion board or building one, sound
  397. recording is possible on PCs.
  398.      As the use of speech technology grows, speech can be added
  399. to other products.  According to IBM's long-range plan, all host
  400. computers will eventually be accessed via a PS/2 running OS/2.
  401. Although mainframes and minicomputers do not typically have sound
  402. speakers, they could use the PS/2's speaker for speech output.
  403. Thus, by using PCs as terminals, the full range of computers can
  404. handle digital audio.
  405.      The algorithm presented here can be used in settings other
  406. than in a PC.  The same method could be used in any computer with
  407. an 8253 chip, and a hardware expansion using the 8253 can be
  408. added to other computers.  More importantly, any hardware
  409. configuration capable of producing output similar to PWM can,
  410. when connected to a speaker, produce digitized sound.  Similarly,
  411. any system able to produce pulses similar to any digital
  412. recording method can output digitized sound.  As a result,
  413. hardware with only two states can play sound, and a digital-to-
  414. analog converter is not needed.  Most computers, such as the
  415. Amiga, use D/A converters, but this method shows that such is not
  416. required to play digitized sound.
  417.      Building an audio system based around the 8253 would be an
  418. excellent project for hardware students.  An expansion board that
  419. used a 8253 to play digitized sound (in place of a D/A converter)
  420. would be possible for many microcomputers.  A small, self-
  421. contained system created specifically for the 8253 would also be
  422. a good project.  In these cases, the student could design the
  423. hardware so that it does not have the limitations present in PCs.
  424.  
  425.  
  426.                            CONCLUSION
  427.  
  428.      Over the past several decades, engineers have searched for
  429. ways to make computers both talk and play high-quality music.
  430. One solution to both problems, digitization of sound using pulse
  431. modulation, requires little processing time and is thus
  432. appropriate for microcomputers.  Although previous usage of
  433. digitized sound has been limited to computers with specialized
  434. hardware, it is possible for a standard PC to play good quality
  435. sounds without extra hardware.  As the computer world strides
  436. deeper into sound-based applications despite a lack of hardware
  437. standards for sound output on PCs, this method may prove to be
  438. invaluable in bringing sound to the masses.  As people learn more
  439. from computers, their experiences will benefit from the addition
  440. of audio.  Students and teachers can use this knowledge as they
  441. learn about computers and sound.  With minimal effort, any
  442. program can add a new dimension with speech, music, and sound
  443. effects.
  444.  
  445.  
  446.  
  447.  
  448.                          REFERENCES
  449.  
  450.  
  451. [1] Pohlmann, Ken C.  Principles of Digital Audio.  H. W.
  452.      Sams, Indianapolis (1985).
  453.  
  454. [2] Rosch, Winn L.  The Winn Rosch Hardware Bible.  Simon &
  455.      Schuster, New York (1988).
  456.  
  457. [3] Sargent, Murray, III and Richard L. Shoemaker.  The IBM
  458.      Personal Computer from the Inside Out.  Addison-Wesley
  459.      (1984).
  460.  
  461. [4] Norton, Peter.  The Peter Norton Programmer's Guide to
  462.      the IBM PC.  1st ed.  Microsoft, Redmond, WA (1985).
  463.  
  464.  
  465. Copyright 1990 by the Consortium for Computing in Small Colleges.
  466. Permission to copy without fee all or part of this material is granted
  467. provided that the copies are not made or distributed for direct commercial
  468. advantage, the CCSC copyright notice and the title of the publication and
  469. its date appear, and notice is given that copying is by permission of the
  470. Consortium for Computing in Small Colleges.  To copy otherwise requires
  471. a fee and/or specific permission.
  472.  
  473. This article appeared in "Proceeding of the Fourth Annual Southeastern Small
  474. College Computing Conference", November 9-10, 1990.  Reprinted with permission
  475. of the CCSC.
  476.  
  477.  
  478.  
  479.  
  480.  
  481.  
  482.  
  483.  
  484.  
  485.  
  486.  
  487.  
  488.  
  489.  
  490.  
  491.  
  492.  
  493.