device_al.cpp

Go to the documentation of this file.
00001 #include <errno.h>
00002 #include <unistd.h>
00003 #include "device_al.h"
00004 #include "debug.h"
00005 
00006 
00007 namespace audiere {
00008 
00009   ALAudioDevice*
00010   ALAudioDevice::create(const ParameterList& parameters) {
00011     ADR_GUARD("ALAudioDevice::create");
00012 
00013     // if anything goes wrong, assume 44100 Hz
00014     int rate = 44100;
00015 
00016     ADR_LOG("Getting resource");
00017 
00018     int device = alGetResourceByName(AL_SYSTEM, "DefaultOut", AL_DEVICE_TYPE);
00019     if (device) {
00020       ADR_LOG("Getting sampling rate");
00021 
00022       ALpv pv;
00023       pv.param = AL_RATE;
00024       if (-1 == alGetParams(device, &pv, 1)) {
00025         fprintf(stderr, "Couldn't get rate: %s\n",
00026                 alGetErrorString(oserror()));
00027       }
00028       rate = pv.value.i;
00029     } else {
00030       fprintf(stderr, "Couldn't get DefaultOut resource: %s\n",
00031               alGetErrorString(oserror()));
00032     }
00033 
00034     ADR_LOG("Creating config");
00035 
00036     ALconfig config = alNewConfig();
00037     if (!config) {
00038       fprintf(stderr, "Couldn't create ALconfig: %s\n",
00039               alGetErrorString(oserror()));
00040       return 0;
00041     }
00042 
00043     ADR_LOG("Setting channels");
00044 
00045     // stereo
00046     int result = alSetChannels(config, 2);
00047     if (result != 0) {
00048       fprintf(stderr, "Couldn't set channels: %s\n",
00049               alGetErrorString(oserror()));
00050       alFreeConfig(config);
00051       return 0;
00052     }
00053 
00054     ADR_LOG("Opening port");
00055 
00056     ALport port = alOpenPort("Audiere Output Port", "w", config);
00057     if (!port) {
00058       fprintf(stderr, "Couldn't open port: %s\n",
00059               alGetErrorString(oserror()));
00060       alFreeConfig(config);
00061       return 0;
00062     }
00063 
00064     ADR_LOG("Creating audio device");
00065 
00066     alFreeConfig(config);
00067     return new ALAudioDevice(port, rate);
00068   }
00069 
00070 
00071   ALAudioDevice::ALAudioDevice(ALport port, int rate)
00072     : MixerDevice(rate)
00073   {
00074     ADR_GUARD("ALAudioDevice::ALAudioDevice");
00075 
00076     m_port = port;
00077   }
00078 
00079 
00080   ALAudioDevice::~ALAudioDevice() {
00081     ADR_GUARD("ALAudioDevice::~ALAudioDevice");
00082 
00083     alClosePort(m_port);
00084   }
00085 
00086 
00087   void
00088   ALAudioDevice::update() {
00089     ADR_GUARD("ALAudioDevice::update");
00090 
00091     // how much data can we write?
00092     const int filled = alGetFilled(m_port);
00093     int can_write = 5000 - filled;  // empty portion of the buffer
00094 
00095     // write 1024 frames at a time
00096     static const int BUFFER_SIZE = 1024;
00097     u8 buffer[BUFFER_SIZE * 4];
00098     while (can_write > 0) {
00099       int transfer_count = std::min(can_write, BUFFER_SIZE);
00100 
00101       ADR_LOG("reading");
00102 
00103       read(transfer_count, buffer);
00104 
00105       ADR_LOG("writing");
00106 
00107       alWriteFrames(m_port, buffer, transfer_count);
00108       can_write -= transfer_count;
00109     }
00110 
00111     usleep(50000);  // 50 milliseconds
00112   }
00113 
00114 
00115   const char* ADR_CALL
00116   ALAudioDevice::getName() {
00117     return "sgial";
00118   }
00119 
00120 }

Generated on Mon Feb 13 23:07:46 2006 for audiere by  doxygen 1.4.6