// // Programmer: Craig Stuart Sapp // Creation Date: Wed Jun 27 12:28:03 PDT 2001 // Last Modified: Wed Jun 27 12:28:16 PDT 2001 // Filename: ...sig/maint/code/audio/AudioIO/oss/DspIO_oss.cpp // Web Address: http://sig.sapp.org/src/sig/DspIO_oss.cpp // Syntax: C++ // // Description: Basic Audio input/output functionality for the // Linux OSS audio device /dev/dsp. This class // is inherited by the classes AudioIn_oss and // AudioOut_oss. Only one device is allowed // at this time. // #ifdef LINUX #include "DspIO_oss.h" #include #include #include #include #include #include #ifndef OLDCPP #include using namespace std; #else #include #endif // define static variables: const char* DspIO_oss::dsp = "/dev/dsp"; int DspIO_oss::dsp_fd = -1; int DspIO_oss::openQ = 0; int DspIO_oss::numchans = 0; int DspIO_oss::srate = 0; int DspIO_oss::format = 0; int DspIO_oss::class_count = 0; /////////////////////////////// // // DspIO_oss::DspIO_oss -- // default value: autoOpen = 1; // DspIO_oss::DspIO_oss(void) { class_count++; } ////////////////////////////// // // DspIO_oss::~DspIO_oss -- // DspIO_oss::~DspIO_oss() { class_count--; if (class_count == 0) { close(); } else if (class_count < 0) { cerr << "Unusual class instatiation count: " << class_count << endl; exit(1); } } ////////////////////////////// // // DspIO_oss::close -- close the dsp device. The device // automatically closes once the program ends, but you can close it // so that other programs can use it. // void DspIO_oss::close(void) { if (getFd() >= 0) { ::close(getFd()); dsp_fd = -1; } openQ = 0; } ///////////////////////////// // // DspIO_oss::is_open -- returns true if the device // is already open, false if closed. // int DspIO_oss::is_open(void) { return openQ; } ///////////////////////////// // // DspIO_oss::open -- returns true if the device // was successfully opended (or already opened) // int DspIO_oss::open(void) { if (getFd() < 0) { dsp_fd = ::open(dsp, O_RDONLY, 0); if (getFd() >= 0) openQ = 1; else openQ = 0; } return is_open(); } ////////////////////////////// // // DspIO_oss::open16LSB -- // default values: channels = 1; samplingrate = 44100.0 // int DspIO_oss::open16LSB(int channels, double samplingrate) { if (is_open()) { close(); } open(); // (1) set the sample format DspIO_oss::format = AFMT_S16_LE; // know this one works on my computer if (ioctl(dsp_fd, SNDCTL_DSP_SETFMT, &format) == -1) { cout << "Error seting sound input to 16bit little endian" << endl; exit(1); } if (format != AFMT_S16_LE) { cout << "Error: cannot handle 16-bit little endian input" << endl; exit(1); } // (2) set the number of channels DspIO_oss::numchans = channels; if (channels < 1) { channels = 1; } if (ioctl(dsp_fd, SNDCTL_DSP_CHANNELS, &channels) == -1) { cout << "Error: cannot set input to " << channels << " channels " << endl; exit(1); } if (channels != 1) { cout << "Error: cannot set input to specified channels" << endl; cout << "Can do " << channels << " channels however" << endl; exit(1); } // (3) set the sampling rate DspIO_oss::srate = (int)samplingrate; if (ioctl(dsp_fd, SNDCTL_DSP_SPEED, &srate) == -1) { cout << "Error setting the speed of the recording to " << srate << endl; } if (abs(srate - (int)samplingrate ) > samplingrate/20) { cout << "Error: " << samplingrate << " sampling rate not supported" << endl; exit(1); } return getFd(); } ////////////////////////////// // // DspIO_oss::read -- reads audio data. // void DspIO_oss::read(uchar* buf, int aSize) { ::read(getFd(), buf, aSize); } ////////////////////////////// // // DspIO_oss::getFd -- returns the file descriptor of the device. // int DspIO_oss::getFd(void) { return dsp_fd; } #endif /* LINUX */ // md5sum: 73b5d8dc50870ea986a7f5cde8fedb4a DspIO_oss.cpp [20050403]