Main Page | Class Hierarchy | Compound List | File List | Compound Members | File Members | Related Pages

SID_hp.i

Go to the documentation of this file.
00001 /*
00002  *  SID_hp.i - 6581 emulation, HP-UX specific stuff
00003  *
00004  *  Lutz Vieweg <lkv@mania.robin.de>
00005  */
00006 
00007 extern "C" {
00008         #include <sys/audio.h>
00009         #include <unistd.h>
00010         #include <fcntl.h>
00011         #include <sys/ioctl.h>
00012 }
00013 
00014 #define TXBUFSIZE  16384 // bytes, not samples
00015 #define TXFRAGSIZE 1024  // samples, not bytes
00016 #define TXLOWWATER (TXBUFSIZE-(TXFRAGSIZE*2)) // bytes, not samples
00017 
00018 /*
00019  *  Initialization
00020  */
00021 
00022 void DigitalRenderer::init_sound(void)
00023 {
00024         ready = false;
00025         
00026         if ((fd = open("/dev/audio", O_WRONLY | O_NDELAY, 0)) < 0) {
00027                 fprintf(stderr, "unable to open /dev/audio -> no sound\n");
00028                 return;
00029         }
00030         
00031         int flags;
00032         if ((flags = fcntl (fd, F_GETFL, 0)) < 0) {
00033                 fprintf(stderr, "unable to set non-blocking mode for /dev/audio -> no sound\n");
00034                 return;
00035         }
00036         flags |= O_NDELAY;
00037         if (fcntl (fd, F_SETFL, flags) < 0) {
00038                 fprintf(stderr, "unable to set non-blocking mode for /dev/audio -> no sound\n");
00039                 return;
00040         }
00041         
00042         if (ioctl(fd, AUDIO_SET_DATA_FORMAT, AUDIO_FORMAT_LINEAR16BIT)) {
00043                 fprintf(stderr, "unable to select 16bit-linear sample format -> no sound\n");
00044                 return;
00045         }
00046         
00047         if (ioctl(fd, AUDIO_SET_SAMPLE_RATE, 44100)) {
00048                 fprintf(stderr, "unable to select 44.1kHz sample-rate -> no sound\n");
00049                 return;
00050         }
00051         
00052         if (ioctl(fd, AUDIO_SET_CHANNELS, 1)) {
00053                 fprintf(stderr, "unable to select 1-channel playback -> no sound\n");
00054                 return;
00055         }
00056         
00057         // choose between:
00058         // AUDIO_OUT_SPEAKER
00059         // AUDIO_OUT_HEADPHONE
00060         // AUDIO_OUT_LINE
00061         if (ioctl(fd, AUDIO_SET_OUTPUT, AUDIO_OUT_SPEAKER)) {
00062                 fprintf(stderr, "unable to select audio output -> no sound\n");
00063                 return;
00064         }
00065 
00066         {
00067                 // set volume:
00068                 audio_describe description;
00069                 audio_gains gains;
00070                 if (ioctl(fd, AUDIO_DESCRIBE, &description)) {
00071                         fprintf(stderr, "unable to get audio description -> no sound\n");
00072                         return;
00073                 }
00074                 if (ioctl (fd, AUDIO_GET_GAINS, &gains)) {
00075                         fprintf(stderr, "unable to get gain values -> no sound\n");
00076                         return;
00077                 }
00078                 
00079                 float volume = 1.0;
00080                 gains.transmit_gain = (int)((float)description.min_transmit_gain +
00081                                   (float)(description.max_transmit_gain
00082                                           - description.min_transmit_gain)
00083                                           * volume);
00084                 if (ioctl (fd, AUDIO_SET_GAINS, &gains)) {
00085                         fprintf(stderr, "unable to set gain values -> no sound\n");
00086                         return;
00087                 }
00088         }
00089         
00090         if (ioctl(fd, AUDIO_SET_TXBUFSIZE, TXBUFSIZE)) {
00091                 fprintf(stderr, "unable to set transmission buffer size -> no sound\n");
00092                 return;
00093         }
00094         
00095         sound_calc_buf = new int16[TXFRAGSIZE];
00096         
00097         linecnt = 0;
00098         
00099         ready = true;
00100         return;
00101 }
00102 
00103 
00104 /*
00105  *  Destructor
00106  */
00107 
00108 DigitalRenderer::~DigitalRenderer()
00109 {
00110         if (fd > 0) {
00111                 if (ready) {
00112                         ioctl(fd, AUDIO_DRAIN);
00113                 }
00114                 ioctl(fd, AUDIO_RESET);
00115                 close(fd);
00116         }
00117 }
00118 
00119 
00120 /*
00121  *  Pause sound output
00122  */
00123 
00124 void DigitalRenderer::Pause(void)
00125 {
00126 }
00127 
00128 
00129 /*
00130  * Resume sound output
00131  */
00132 
00133 void DigitalRenderer::Resume(void)
00134 {
00135 }
00136 
00137 
00138 /*
00139  *  Fill buffer, sample volume (for sampled voice)
00140  */
00141 
00142 void DigitalRenderer::EmulateLine(void)
00143 {
00144         sample_buf[sample_in_ptr] = volume;
00145         sample_in_ptr = (sample_in_ptr + 1) % SAMPLE_BUF_SIZE;
00146 
00147         // testing the audio status at each raster-line is
00148         // really not necessary. Let's do it every 50th one..
00149         // ought to be enough by far.
00150 
00151         linecnt--;
00152         if (linecnt < 0 && ready) {
00153                 linecnt = 50;
00154                 
00155                 // check whether we should add some more data to the
00156                 // transmission buffer.
00157 
00158                 if (ioctl(fd, AUDIO_GET_STATUS, &status)) {
00159                         fprintf(stderr,"fatal: unable to get audio status\n");
00160                         exit(20);
00161                 }
00162                 
00163                 if (status.transmit_buffer_count < TXLOWWATER) {
00164                         // add one sound fragment..
00165                         calc_buffer(sound_calc_buf, TXFRAGSIZE*2);
00166                         
00167                         // since we've checked for enough space in the transmission buffer,
00168                         // it is an error if the non-blocking write returns anything but
00169                         // TXFRAGSIZE*2
00170                         if (TXFRAGSIZE*2 != write (fd, sound_calc_buf, TXFRAGSIZE*2)) {
00171                                 fprintf(stderr,"fatal: write to audio-device failed\n");
00172                                 exit(20);
00173                         }
00174                 }
00175         }
00176 }
00177 

Generated on Tue Feb 8 04:08:12 2005 for E32frodo by doxygen 1.3.3