2011-06-06 07:02:13 +02:00
|
|
|
/* fhandler_dev_dsp: code to emulate OSS sound model /dev/dsp
|
2001-06-26 16:47:48 +02:00
|
|
|
|
2012-02-09 16:23:17 +01:00
|
|
|
Copyright 2001, 2002, 2003, 2004, 2008, 2011, 2012 Red Hat, Inc
|
2001-06-26 16:47:48 +02:00
|
|
|
|
2001-04-16 05:27:16 +02:00
|
|
|
Written by Andy Younger (andy@snoogie.demon.co.uk)
|
2004-03-23 12:05:56 +01:00
|
|
|
Extended by Gerd Spalink (Gerd.Spalink@t-online.de)
|
|
|
|
to support recording from the audio input
|
2001-06-26 16:47:48 +02:00
|
|
|
|
2001-04-16 05:27:16 +02:00
|
|
|
This file is part of Cygwin.
|
2001-06-26 16:47:48 +02:00
|
|
|
|
2001-04-16 05:27:16 +02:00
|
|
|
This software is a copyrighted work licensed under the terms of the
|
|
|
|
Cygwin license. Please consult the file "CYGWIN_LICENSE" for
|
|
|
|
details. */
|
|
|
|
|
|
|
|
#include "winsup.h"
|
|
|
|
#include <sys/soundcard.h>
|
2001-07-26 21:22:24 +02:00
|
|
|
#include "cygerrno.h"
|
|
|
|
#include "security.h"
|
* devices.cc: New file.
* devices.gperf: New file.
* devices.shilka: New file.
* cygwin-gperf: New file.
* cygwin-shilka: New file.
* fhandler_fifo.cc: New file.
* fhandler_nodevice.cc : New file. Reorganize headers so that path.h precedes
fhandler.h throughout. Remove device argument and unit arguments from fhandler
constructors throughout. Remove pc arguments to fhandler functions and use
internal pc element instead, throughout. Use dev element in pc throughout.
Use major/minor elements rather than units and device numbers previously in
fhandler class. Use correct methods for fhandler file names rather than
directly accessing file name variables, throughout.
* Makefile.in (DLL_OFILES): Add devices.o, fhandler_fifo.o
* dcrt0.cc (dll_crt0_1): Call device::init.
* devices.h: Renumber devices based on more Linux-like major/minor numbers.
Add more devices. Declare standard device storage.
(device): Declare struct.
* dir.cc (opendir): Use new 'build_fh_name' to construct a fhandler_* type.
* dtable.cc (dtable::get_debugger_info): Ditto.
(cygwin_attach_handle_to_fd): Ditto.
(dtable::release): Remove special FH_SOCKET case in favor of generic
"need_fixup_before" test.
(dtable::init_std_file_from_handle): Use either build_fh_dev or build_fh_name
to build standard fhandler.
(dtable::build_fh_name): Renamed from dtable::build_fhandler_from_name. Move
out of dtable class. Don't accept a path_conv argument. Just build it here
and pass it to:
(build_fh_pc): Renamed from dtable::build_fhandler. Move out of dtable class.
Use intrinsic device type in path_conv to create new fhandler.
(build_fh_dev): Renamed from dtable::build_fhandler. Move out of dtable class.
Simplify arguments to just take new 'device' type and a name. Just return
pointer to fhandler rather than trying to insert into dtable.
(dtable::dup_worker): Accommodate above build_fh name changes.
(dtable::find_fifo): New (currently broken) function.
(handle_to_fn): Use strechr for efficiency.
* dtable.h: Reflect above build_fh name changes and argument differences.
(fhandler_base *&operator []): Return self rather than copy of self.
* fhandler.cc (fhandler_base::operator =): Use pc element to set normalized
path.
(fhandler_base::set_name): Ditto.
(fhandler_base::raw_read): Use method to access name.
(fhandler_base::write): Correctly use get_output_handle rather than get_handle.
(handler_base::device_access_denied): New function.
(fhandler_base::open): Eliminate pc argument and use pc element of
fhandler_base throughout.
(fhandler_base::fstat): Detect if device is based in filesystem and use
fstat_fs to calculate stat, if so.
(fhandler_base::fhandler_base): Eliminate handling of file names and, instead,
just free appropriate component from pc.
(fhandler_base::opendir): Remove path_conv parameter.
* fhandler.h: Remove all device flags.
(fhandler_base::pc): New element.
(fhandler_base::set_name): Change argument to path_conv.
(fhandler_base::error): New function.
(fhandler_base::exists): New function.
(fhandler_base::pc_binmode): New function.
(fhandler_base::dev): New function.
(fhandler_base::open_fs): New function.
(fhandler_base::fstat_fs): New function.
(fhandler_base::fstat_by_name): New function.
(fhandler_base::fstat_by_handle): New function.
(fhandler_base::isfifo): New function.
(fhandler_base::is_slow): New function.
(fhandler_base::is_auto_device): New function.
(fhandler_base::is_fs_special): New function.
(fhandler_base::device_access_denied): New function.
(fhandler_base::operator DWORD&): New operator.
(fhandler_base::get_name): Return normalized path from pc.
(fhandler_base::get_win32_name): Return windows path from pc.
(fhandler_base::isdevice): Renamed from is_device.
(fhandler_base::get_native_name): Return device format.
(fhandler_fifo): New class.
(fhandler_nodevice): New class.
(select_stuff::device_specific): Remove array.
(select_stuff::device_specific_pipe): New class element.
(select_stuff::device_specific_socket): New class element.
(select_stuff::device_specific_serial): New class element.
(select_stuff::select_stuff): Initialize new elements.
* fhandler_disk_file.cc (fhandler_base::fstat_by_handle): Move to base class
from fhandler_disk_file.
(fhandler_base::fstat_by_name): Ditto.
(fhandler_base::fstat_by_name): Ditto.
(fhandler_disk_file::open): Move most functionality into
fhandler_base::open_fs.
(fhandler_base::open_fs): New function.
(fhandler_disk_file::close): Move most functionality into
fhandler_base::close_fs.
(fhandler_base::close_fs): New function.
* fhandler_mem.cc (fhandler_dev_mem::open): Use device name in debugging
output.
* fhandler_socket.cc (fhandler_socket::set_connect_secret): Copy standard
urandom device into appropriate place.
(fhandler_socket::accept): Reflect change in fdsock return value.
* fhandler_tty.cc: See "throughouts" above.
* net.cc: Accommodate fdsock change throughout.
(fdsock): Return success or failure, accept fd argument and device argument.
* path.cc (symlink_info::major): New element.
(symlink_info::minor): New element.
(symlink_info::parse_device): Declare new function.
(fs_info::update): Accommodate changes in path_conv class.
(path_conv::fillin): Ditto.
(path_conv::return_and_clear_normalized_path): Eliminate.
(path_conv::set_normalized_path): New function.
(path_conv::path_conv): Set info in dev element. Use path_conv methods Check
for FH_FS rather than FH_BAD to indicate when to fill in filesystem stuff.
where appropriate rather than direct access. Use set_normalized_path to set
normalized path.
(windows_device_names): Eliminate.
(get_dev): Ditto.
(get_raw_device_number): Ditto.
(get_device_number): Ditto.
(win32_device_name): Call new device name parser to do most of the heavy
lifting.
(mount_info::conv_to_win32_path): Fill in dev field as appropriate.
(symlink_worker): Handle new device files.
(symlink_info::check): Ditto.
(symlink_info::parse_device): Define new function.
* path.h (executable_states): Move here from fhandler.h.
(fs_info): Rename variables to *_storage and create methods for accessing same.
(path_conv): Add dev element, remove devn and unit and adjust inline methods to
accommodate.
(set_normalized_path): Declare new function.
* pinfo.cc (_pinfo::commune_recv): Add broken support for handling fifos.
(_pinfo::commune_send): Ditto.
* pipe.cc (fhandler_pipe::close): check for existence of handle before closing
it.
(handler_pipe::create): Rename from make_pipe. Change arguments to accept
fhandler_pipe array. Accommodate fifos.
(pipe): Rework to deal with fhandler_pipe::create changes.
(_pipe): Ditto.
* select.cc: Use individual device_specific types throughout rather than
indexing with obsolete device number.
(set_bits): Use is_socket call rather than checking device number.
* shared_info.h (CURR_MOUNT_MAGIC): Update.
(conv_to_win32_path): Reflect addition of device argument.
* syscalls.cc (mknod_worker): New function.
(open): Use build_fh_name to build fhandler.
(chown_worker): Detect if this is an 'auto' device rather than an on-filesystem
device and handle appropriately.
(chmod_device): New function.
(chmod): Detect if this is an 'auto' device rather than an on-filesystem device
and handle appropriately. Use chmod_device to set mode of in-filesystem
devices.
(stat_worker): Eliminate path_conv argument. Call build_fh_name to construct
fhandler. Use fh->error() rather than pc->error to detect errors in fhandler
construction.
(access_worker): New function pulled from access. Accommodate in-filesystem
devices.
(access): Use access_worker.
(fpathconf): Detect if this is an 'auto' device rather than an on-filesystem
device and handle appropriately.
(mknod_worker): New function.
(mknod32): New function.
(chroot): Free normalized path -- assuming it was actually cmalloced.
* tty.cc (create_tty_master): Tweak for new device class.
(tty::common_init): Ditto.
* winsup.h (stat_worker): Remove.
(symlink_worker): Declare.
* exceptions.cc (set_process_mask): Just call sig_dispatch_pending and don't
worry about pending_signals since sig_dispatch_pending should always do the
right thing now.
(sig_handle): Reorganize SIGCONT handling to more closely conform to SUSv3.
* pinfo.h: Move __SIG enum to sigproc.h.
(PICOM_FIFO): New enum element.
(_pinfo): Remove 'thread2signal' stuff throughout class.
(_pinfo::commune_send): Make varargs.
(_pinfo::sigtodo): Eliminate.
(_pinfo::thread2signal): Ditto.
* signal.cc (kill_worker): Eliminate call to setthread2signal.
* sigproc.cc (local_sigtodo): Eliminate.
(getlocal_sigtodo): Ditto.
(sigelem): New class.
(pending_signals): New class.
(sigqueue): New variable, start of sigqueue linked list.
(sigcatch_nonmain): Eliminate.
(sigcatch_main): Eliminate.
(sigcatch_nosync): Eliminate.
(sigcomplete_nonmain): Eliminate.
(pending_signals): Eliminate.
(sig_clear): Call signal thread to clear pending signals, unless already in
signal thread.
(sigpending): Call signal thread to get pending signals.
(sig_dispatch_pending): Eliminate use of pending_signals and just check
sigqueue.
(sigproc_terminate): Eliminate all of the obsolete semaphore stuff. Close
signal pipe handle.
(sig_send): Eliminate all of the obsolete semaphore stuff and use pipe to send
signals.
(getevent): Eliminate.
(pending_signals::add): New function.
(pending_signals::del): New function.
(pending_signals::next): New function.
(wait_sig): Eliminate all of the obsolete semaphore stuff. Use pipe to
communicate and maintain a linked list of signals.
* sigproc.h: Move __SIG defines here. Add __SIGPENDING.
(sig_dispatch_pending): Remove "C" specifier.
(sig_handle): Accept a mask argument.
* thread.cc: Remove signal handling considerations throughout.
2003-09-25 02:37:18 +02:00
|
|
|
#include "path.h"
|
2001-07-26 21:22:24 +02:00
|
|
|
#include "fhandler.h"
|
2004-08-17 11:52:50 +02:00
|
|
|
#include "dtable.h"
|
|
|
|
#include "cygheap.h"
|
2011-05-04 13:41:22 +02:00
|
|
|
#include "sigproc.h"
|
2012-06-17 22:50:24 +02:00
|
|
|
#include "cygwait.h"
|
2001-04-16 05:27:16 +02:00
|
|
|
|
2004-03-23 12:05:56 +01:00
|
|
|
/*------------------------------------------------------------------------
|
|
|
|
Simple encapsulation of the win32 audio device.
|
|
|
|
|
|
|
|
Implementation Notes
|
2004-08-17 11:52:50 +02:00
|
|
|
1. Audio structures are malloced just before the first read or
|
2004-03-23 12:05:56 +01:00
|
|
|
write to /dev/dsp. The actual buffer size is determined at that time,
|
|
|
|
such that one buffer holds about 125ms of audio data.
|
|
|
|
At the time of this writing, 12 buffers are allocated,
|
|
|
|
so that up to 1.5 seconds can be buffered within Win32.
|
|
|
|
The buffer size can be queried with the ioctl SNDCTL_DSP_GETBLKSIZE,
|
|
|
|
but for this implementation only returns meaningful results if
|
|
|
|
sampling rate, number of channels and number of bits per sample
|
|
|
|
are not changed afterwards.
|
2004-08-17 11:52:50 +02:00
|
|
|
The audio structures are freed when the device is reset or closed,
|
|
|
|
and they are not passed to exec'ed processes.
|
|
|
|
The dev_ member is cleared after a fork. This forces the child
|
|
|
|
to reopen the audio device._
|
2004-03-23 12:05:56 +01:00
|
|
|
|
2004-08-17 11:52:50 +02:00
|
|
|
2. Every open call creates a new instance of the handler. After a
|
2004-03-23 12:05:56 +01:00
|
|
|
successful open, every subsequent open from the same process
|
|
|
|
to the device fails with EBUSY.
|
2004-08-17 11:52:50 +02:00
|
|
|
The structures are shared between duped handles, but not with
|
|
|
|
children. They only inherit the settings from the parent.
|
2004-03-23 12:05:56 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
class fhandler_dev_dsp::Audio
|
|
|
|
{ // This class contains functionality common to Audio_in and Audio_out
|
|
|
|
public:
|
2011-05-04 13:41:22 +02:00
|
|
|
Audio (fhandler_dev_dsp *my_fh);
|
2004-08-17 11:52:50 +02:00
|
|
|
~Audio ();
|
2004-03-23 12:05:56 +01:00
|
|
|
|
|
|
|
class queue;
|
|
|
|
|
2004-08-17 11:52:50 +02:00
|
|
|
bool isvalid ();
|
|
|
|
void setconvert (int format);
|
2004-03-23 12:05:56 +01:00
|
|
|
void convert_none (unsigned char *buffer, int size_bytes) { }
|
|
|
|
void convert_U8_S8 (unsigned char *buffer, int size_bytes);
|
|
|
|
void convert_S16LE_U16LE (unsigned char *buffer, int size_bytes);
|
|
|
|
void convert_S16LE_U16BE (unsigned char *buffer, int size_bytes);
|
|
|
|
void convert_S16LE_S16BE (unsigned char *buffer, int size_bytes);
|
|
|
|
void fillFormat (WAVEFORMATEX * format,
|
|
|
|
int rate, int bits, int channels);
|
|
|
|
unsigned blockSize (int rate, int bits, int channels);
|
|
|
|
void (fhandler_dev_dsp::Audio::*convert_)
|
|
|
|
(unsigned char *buffer, int size_bytes);
|
2004-08-17 11:52:50 +02:00
|
|
|
|
|
|
|
enum { MAX_BLOCKS = 12 };
|
|
|
|
int bufferIndex_; // offset into pHdr_->lpData
|
|
|
|
WAVEHDR *pHdr_; // data to be filled by write
|
|
|
|
WAVEHDR wavehdr_[MAX_BLOCKS];
|
|
|
|
char *bigwavebuffer_; // audio samples only
|
|
|
|
// Member variables below must be locked
|
|
|
|
queue *Qisr2app_; // blocks passed from wave callback
|
2011-05-04 13:41:22 +02:00
|
|
|
|
|
|
|
fhandler_dev_dsp *fh;
|
2004-03-23 12:05:56 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
class fhandler_dev_dsp::Audio::queue
|
|
|
|
{ // non-blocking fixed size queues for buffer management
|
|
|
|
public:
|
|
|
|
queue (int depth = 4);
|
|
|
|
~queue ();
|
|
|
|
|
|
|
|
bool send (WAVEHDR *); // queue an item, returns true if successful
|
|
|
|
bool recv (WAVEHDR **); // retrieve an item, returns true if successful
|
2004-08-17 11:52:50 +02:00
|
|
|
void reset ();
|
|
|
|
int query (); // return number of items queued
|
|
|
|
inline void lock () { EnterCriticalSection (&lock_); }
|
|
|
|
inline void unlock () { LeaveCriticalSection (&lock_); }
|
|
|
|
inline void dellock () { debug_printf ("Deleting Critical Section"); DeleteCriticalSection (&lock_); }
|
|
|
|
bool isvalid () { return storage_; }
|
2004-03-23 12:05:56 +01:00
|
|
|
private:
|
2004-08-17 11:52:50 +02:00
|
|
|
CRITICAL_SECTION lock_;
|
2004-03-23 12:05:56 +01:00
|
|
|
int head_;
|
|
|
|
int tail_;
|
2004-08-17 11:52:50 +02:00
|
|
|
int depth_;
|
2004-03-23 12:05:56 +01:00
|
|
|
WAVEHDR **storage_;
|
|
|
|
};
|
|
|
|
|
2013-04-23 11:44:36 +02:00
|
|
|
static void CALLBACK waveOut_callback (HWAVEOUT hWave, UINT msg,
|
|
|
|
DWORD_PTR instance, DWORD_PTR param1,
|
|
|
|
DWORD_PTR param2);
|
2004-03-23 12:05:56 +01:00
|
|
|
|
|
|
|
class fhandler_dev_dsp::Audio_out: public Audio
|
|
|
|
{
|
|
|
|
public:
|
2011-05-04 13:41:22 +02:00
|
|
|
Audio_out (fhandler_dev_dsp *my_fh) : Audio (my_fh) {}
|
|
|
|
|
2004-08-17 11:52:50 +02:00
|
|
|
void fork_fixup (HANDLE parent);
|
2004-03-23 12:05:56 +01:00
|
|
|
bool query (int rate, int bits, int channels);
|
2004-08-17 11:52:50 +02:00
|
|
|
bool start ();
|
2004-04-13 11:38:32 +02:00
|
|
|
void stop (bool immediately = false);
|
2011-05-04 13:41:22 +02:00
|
|
|
int write (const char *pSampleData, int nBytes);
|
2004-03-23 12:05:56 +01:00
|
|
|
void buf_info (audio_buf_info *p, int rate, int bits, int channels);
|
|
|
|
void callback_sampledone (WAVEHDR *pHdr);
|
|
|
|
bool parsewav (const char *&pData, int &nBytes,
|
2004-08-17 11:52:50 +02:00
|
|
|
int rate, int bits, int channels);
|
2004-03-23 12:05:56 +01:00
|
|
|
|
|
|
|
private:
|
|
|
|
void init (unsigned blockSize);
|
|
|
|
void waitforallsent ();
|
2011-05-04 13:41:22 +02:00
|
|
|
bool waitforspace ();
|
2004-03-23 12:05:56 +01:00
|
|
|
bool sendcurrent ();
|
|
|
|
|
|
|
|
enum { MAX_BLOCKS = 12 };
|
|
|
|
HWAVEOUT dev_; // The wave device
|
2004-08-17 11:52:50 +02:00
|
|
|
/* Private copies of audiofreq_, audiobits_, audiochannels_,
|
|
|
|
possibly set from wave file */
|
|
|
|
int freq_;
|
|
|
|
int bits_;
|
|
|
|
int channels_;
|
2004-03-23 12:05:56 +01:00
|
|
|
};
|
|
|
|
|
2013-04-23 11:44:36 +02:00
|
|
|
static void CALLBACK waveIn_callback (HWAVEIN hWave, UINT msg,
|
|
|
|
DWORD_PTR instance, DWORD_PTR param1,
|
|
|
|
DWORD_PTR param2);
|
2004-03-23 12:05:56 +01:00
|
|
|
|
|
|
|
class fhandler_dev_dsp::Audio_in: public Audio
|
2001-04-16 05:27:16 +02:00
|
|
|
{
|
|
|
|
public:
|
2011-05-04 13:41:22 +02:00
|
|
|
Audio_in (fhandler_dev_dsp *my_fh) : Audio (my_fh) {}
|
|
|
|
|
2004-08-17 11:52:50 +02:00
|
|
|
void fork_fixup (HANDLE parent);
|
2004-03-23 12:05:56 +01:00
|
|
|
bool query (int rate, int bits, int channels);
|
|
|
|
bool start (int rate, int bits, int channels);
|
|
|
|
void stop ();
|
|
|
|
bool read (char *pSampleData, int &nBytes);
|
2004-04-13 11:38:32 +02:00
|
|
|
void buf_info (audio_buf_info *p, int rate, int bits, int channels);
|
2004-03-23 12:05:56 +01:00
|
|
|
void callback_blockfull (WAVEHDR *pHdr);
|
2001-04-16 05:27:16 +02:00
|
|
|
|
2004-03-23 12:05:56 +01:00
|
|
|
private:
|
|
|
|
bool init (unsigned blockSize);
|
|
|
|
bool queueblock (WAVEHDR *pHdr);
|
2011-05-04 13:41:22 +02:00
|
|
|
bool waitfordata (); // blocks until we have a good pHdr_ unless O_NONBLOCK
|
2004-03-23 12:05:56 +01:00
|
|
|
|
|
|
|
HWAVEIN dev_;
|
2001-04-16 05:27:16 +02:00
|
|
|
};
|
|
|
|
|
2004-03-23 12:05:56 +01:00
|
|
|
/* --------------------------------------------------------------------
|
|
|
|
Implementation */
|
|
|
|
|
|
|
|
// Simple fixed length FIFO queue implementation for audio buffer management
|
|
|
|
fhandler_dev_dsp::Audio::queue::queue (int depth)
|
|
|
|
{
|
|
|
|
// allow space for one extra object in the queue
|
|
|
|
// so we can distinguish full and empty status
|
2004-08-17 11:52:50 +02:00
|
|
|
depth_ = depth;
|
|
|
|
storage_ = new WAVEHDR *[depth_ + 1];
|
2004-03-23 12:05:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fhandler_dev_dsp::Audio::queue::~queue ()
|
|
|
|
{
|
|
|
|
delete[] storage_;
|
|
|
|
}
|
2001-05-24 07:20:17 +02:00
|
|
|
|
2004-08-17 11:52:50 +02:00
|
|
|
void
|
|
|
|
fhandler_dev_dsp::Audio::queue::reset ()
|
|
|
|
{
|
|
|
|
/* When starting, after reset and after fork */
|
|
|
|
head_ = tail_ = 0;
|
|
|
|
debug_printf ("InitializeCriticalSection");
|
|
|
|
memset (&lock_, 0, sizeof (lock_));
|
|
|
|
InitializeCriticalSection (&lock_);
|
|
|
|
}
|
|
|
|
|
2004-03-23 12:05:56 +01:00
|
|
|
bool
|
|
|
|
fhandler_dev_dsp::Audio::queue::send (WAVEHDR *x)
|
|
|
|
{
|
2004-08-17 11:52:50 +02:00
|
|
|
bool res = false;
|
|
|
|
lock ();
|
2004-03-23 12:05:56 +01:00
|
|
|
if (query () == depth_)
|
2004-08-17 11:52:50 +02:00
|
|
|
system_printf ("Queue overflow");
|
|
|
|
else
|
|
|
|
{
|
|
|
|
storage_[tail_] = x;
|
|
|
|
if (++tail_ > depth_)
|
|
|
|
tail_ = 0;
|
|
|
|
res = true;
|
|
|
|
}
|
|
|
|
unlock ();
|
|
|
|
return res;
|
2004-03-23 12:05:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
fhandler_dev_dsp::Audio::queue::recv (WAVEHDR **x)
|
|
|
|
{
|
2004-08-17 11:52:50 +02:00
|
|
|
bool res = false;
|
|
|
|
lock ();
|
|
|
|
if (query () != 0)
|
|
|
|
{
|
|
|
|
*x = storage_[head_];
|
|
|
|
if (++head_ > depth_)
|
|
|
|
head_ = 0;
|
|
|
|
res = true;
|
|
|
|
}
|
|
|
|
unlock ();
|
|
|
|
return res;
|
2004-03-23 12:05:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
fhandler_dev_dsp::Audio::queue::query ()
|
|
|
|
{
|
|
|
|
int n = tail_ - head_;
|
|
|
|
if (n < 0)
|
2004-08-17 11:52:50 +02:00
|
|
|
n += depth_ + 1;
|
2004-03-23 12:05:56 +01:00
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Audio class implements functionality need for both read and write
|
2011-05-04 13:41:22 +02:00
|
|
|
fhandler_dev_dsp::Audio::Audio (fhandler_dev_dsp *my_fh)
|
2001-04-16 05:27:16 +02:00
|
|
|
{
|
2004-08-17 11:52:50 +02:00
|
|
|
bigwavebuffer_ = NULL;
|
|
|
|
Qisr2app_ = new queue (MAX_BLOCKS);
|
2004-03-23 12:05:56 +01:00
|
|
|
convert_ = &fhandler_dev_dsp::Audio::convert_none;
|
2011-05-04 13:41:22 +02:00
|
|
|
fh = my_fh;
|
2001-04-16 05:27:16 +02:00
|
|
|
}
|
|
|
|
|
2004-03-23 12:05:56 +01:00
|
|
|
fhandler_dev_dsp::Audio::~Audio ()
|
2001-04-16 05:27:16 +02:00
|
|
|
{
|
2004-08-17 11:52:50 +02:00
|
|
|
debug_printf("");
|
|
|
|
delete Qisr2app_;
|
|
|
|
delete[] bigwavebuffer_;
|
2001-04-16 05:27:16 +02:00
|
|
|
}
|
|
|
|
|
2004-08-17 11:52:50 +02:00
|
|
|
inline bool
|
|
|
|
fhandler_dev_dsp::Audio::isvalid ()
|
2001-04-16 05:27:16 +02:00
|
|
|
{
|
2004-08-17 11:52:50 +02:00
|
|
|
return bigwavebuffer_ && Qisr2app_ && Qisr2app_->isvalid ();
|
2004-03-23 12:05:56 +01:00
|
|
|
}
|
2001-05-20 19:31:06 +02:00
|
|
|
|
2004-03-23 12:05:56 +01:00
|
|
|
void
|
2004-08-17 11:52:50 +02:00
|
|
|
fhandler_dev_dsp::Audio::setconvert (int format)
|
2004-03-23 12:05:56 +01:00
|
|
|
{
|
|
|
|
switch (format)
|
2001-04-16 05:27:16 +02:00
|
|
|
{
|
2004-03-23 12:05:56 +01:00
|
|
|
case AFMT_S8:
|
|
|
|
convert_ = &fhandler_dev_dsp::Audio::convert_U8_S8;
|
|
|
|
debug_printf ("U8_S8");
|
|
|
|
break;
|
|
|
|
case AFMT_U16_LE:
|
|
|
|
convert_ = &fhandler_dev_dsp::Audio::convert_S16LE_U16LE;
|
|
|
|
debug_printf ("S16LE_U16LE");
|
|
|
|
break;
|
|
|
|
case AFMT_U16_BE:
|
|
|
|
convert_ = &fhandler_dev_dsp::Audio::convert_S16LE_U16BE;
|
|
|
|
debug_printf ("S16LE_U16BE");
|
|
|
|
break;
|
|
|
|
case AFMT_S16_BE:
|
|
|
|
convert_ = &fhandler_dev_dsp::Audio::convert_S16LE_S16BE;
|
|
|
|
debug_printf ("S16LE_S16BE");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
convert_ = &fhandler_dev_dsp::Audio::convert_none;
|
|
|
|
debug_printf ("none");
|
2001-04-16 05:27:16 +02:00
|
|
|
}
|
2004-03-23 12:05:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
fhandler_dev_dsp::Audio::convert_U8_S8 (unsigned char *buffer,
|
|
|
|
int size_bytes)
|
|
|
|
{
|
|
|
|
while (size_bytes-- > 0)
|
2001-04-16 05:27:16 +02:00
|
|
|
{
|
2004-03-23 12:05:56 +01:00
|
|
|
*buffer ^= (unsigned char)0x80;
|
|
|
|
buffer++;
|
2001-04-16 05:27:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-05-20 19:31:06 +02:00
|
|
|
void
|
2004-03-23 12:05:56 +01:00
|
|
|
fhandler_dev_dsp::Audio::convert_S16LE_U16BE (unsigned char *buffer,
|
|
|
|
int size_bytes)
|
2001-04-16 05:27:16 +02:00
|
|
|
{
|
2004-03-23 12:05:56 +01:00
|
|
|
int size_samples = size_bytes / 2;
|
|
|
|
unsigned char hi, lo;
|
|
|
|
while (size_samples-- > 0)
|
2001-05-20 19:31:06 +02:00
|
|
|
{
|
2004-03-23 12:05:56 +01:00
|
|
|
hi = buffer[0];
|
|
|
|
lo = buffer[1];
|
|
|
|
*buffer++ = lo;
|
|
|
|
*buffer++ = hi ^ (unsigned char)0x80;
|
|
|
|
}
|
|
|
|
}
|
2001-05-20 19:31:06 +02:00
|
|
|
|
2004-03-23 12:05:56 +01:00
|
|
|
void
|
|
|
|
fhandler_dev_dsp::Audio::convert_S16LE_U16LE (unsigned char *buffer,
|
|
|
|
int size_bytes)
|
|
|
|
{
|
|
|
|
int size_samples = size_bytes / 2;
|
|
|
|
while (size_samples-- > 0)
|
|
|
|
{
|
|
|
|
buffer++;
|
|
|
|
*buffer ^= (unsigned char)0x80;
|
|
|
|
buffer++;
|
|
|
|
}
|
|
|
|
}
|
2001-05-20 19:31:06 +02:00
|
|
|
|
2004-03-23 12:05:56 +01:00
|
|
|
void
|
|
|
|
fhandler_dev_dsp::Audio::convert_S16LE_S16BE (unsigned char *buffer,
|
|
|
|
int size_bytes)
|
|
|
|
{
|
|
|
|
int size_samples = size_bytes / 2;
|
|
|
|
unsigned char hi, lo;
|
|
|
|
while (size_samples-- > 0)
|
|
|
|
{
|
|
|
|
hi = buffer[0];
|
|
|
|
lo = buffer[1];
|
|
|
|
*buffer++ = lo;
|
|
|
|
*buffer++ = hi;
|
2001-04-16 05:27:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-03-23 12:05:56 +01:00
|
|
|
void
|
|
|
|
fhandler_dev_dsp::Audio::fillFormat (WAVEFORMATEX * format,
|
|
|
|
int rate, int bits, int channels)
|
2001-04-16 05:27:16 +02:00
|
|
|
{
|
2004-03-23 12:05:56 +01:00
|
|
|
memset (format, 0, sizeof (*format));
|
|
|
|
format->wFormatTag = WAVE_FORMAT_PCM;
|
|
|
|
format->wBitsPerSample = bits;
|
|
|
|
format->nChannels = channels;
|
|
|
|
format->nSamplesPerSec = rate;
|
|
|
|
format->nAvgBytesPerSec = format->nSamplesPerSec * format->nChannels
|
|
|
|
* (bits / 8);
|
|
|
|
format->nBlockAlign = format->nChannels * (bits / 8);
|
2001-04-16 05:27:16 +02:00
|
|
|
}
|
|
|
|
|
2004-03-23 12:05:56 +01:00
|
|
|
// calculate a good block size
|
|
|
|
unsigned
|
|
|
|
fhandler_dev_dsp::Audio::blockSize (int rate, int bits, int channels)
|
2001-04-16 05:27:16 +02:00
|
|
|
{
|
2004-03-23 12:05:56 +01:00
|
|
|
unsigned blockSize;
|
|
|
|
blockSize = ((bits / 8) * channels * rate) / 8; // approx 125ms per block
|
|
|
|
// round up to multiple of 64
|
|
|
|
blockSize += 0x3f;
|
2004-05-28 21:50:07 +02:00
|
|
|
blockSize &= ~0x3f;
|
2004-03-23 12:05:56 +01:00
|
|
|
return blockSize;
|
|
|
|
}
|
2001-05-20 19:31:06 +02:00
|
|
|
|
2004-03-23 12:05:56 +01:00
|
|
|
//=======================================================================
|
2004-08-17 11:52:50 +02:00
|
|
|
void
|
|
|
|
fhandler_dev_dsp::Audio_out::fork_fixup (HANDLE parent)
|
2004-03-23 12:05:56 +01:00
|
|
|
{
|
2004-08-17 11:52:50 +02:00
|
|
|
/* Null dev_.
|
|
|
|
It will be necessary to reset the queue, open the device
|
|
|
|
and create a lock when writing */
|
2013-04-23 11:44:36 +02:00
|
|
|
debug_printf ("parent=%p", parent);
|
2004-08-17 11:52:50 +02:00
|
|
|
dev_ = NULL;
|
2001-04-16 05:27:16 +02:00
|
|
|
}
|
|
|
|
|
2004-03-23 12:05:56 +01:00
|
|
|
|
|
|
|
bool
|
|
|
|
fhandler_dev_dsp::Audio_out::query (int rate, int bits, int channels)
|
2001-04-16 05:27:16 +02:00
|
|
|
{
|
2004-03-23 12:05:56 +01:00
|
|
|
WAVEFORMATEX format;
|
|
|
|
MMRESULT rc;
|
|
|
|
|
|
|
|
fillFormat (&format, rate, bits, channels);
|
|
|
|
rc = waveOutOpen (NULL, WAVE_MAPPER, &format, 0L, 0L, WAVE_FORMAT_QUERY);
|
2013-04-23 11:44:36 +02:00
|
|
|
debug_printf ("%u = waveOutOpen(freq=%d bits=%d channels=%d)", rc, rate, bits, channels);
|
2004-03-23 12:05:56 +01:00
|
|
|
return (rc == MMSYSERR_NOERROR);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2004-08-17 11:52:50 +02:00
|
|
|
fhandler_dev_dsp::Audio_out::start ()
|
2004-03-23 12:05:56 +01:00
|
|
|
{
|
|
|
|
WAVEFORMATEX format;
|
|
|
|
MMRESULT rc;
|
2004-08-17 11:52:50 +02:00
|
|
|
unsigned bSize = blockSize (freq_, bits_, channels_);
|
|
|
|
|
|
|
|
if (dev_)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
/* In case of fork bigwavebuffer may already exist */
|
|
|
|
if (!bigwavebuffer_)
|
|
|
|
bigwavebuffer_ = new char[MAX_BLOCKS * bSize];
|
2004-03-23 12:05:56 +01:00
|
|
|
|
2004-08-17 11:52:50 +02:00
|
|
|
if (!isvalid ())
|
2004-03-23 12:05:56 +01:00
|
|
|
return false;
|
|
|
|
|
2004-08-17 11:52:50 +02:00
|
|
|
fillFormat (&format, freq_, bits_, channels_);
|
2013-04-23 11:44:36 +02:00
|
|
|
rc = waveOutOpen (&dev_, WAVE_MAPPER, &format, (DWORD_PTR) waveOut_callback,
|
|
|
|
(DWORD_PTR) this, CALLBACK_FUNCTION);
|
2004-03-23 12:05:56 +01:00
|
|
|
if (rc == MMSYSERR_NOERROR)
|
2004-08-17 11:52:50 +02:00
|
|
|
init (bSize);
|
2004-03-23 12:05:56 +01:00
|
|
|
|
2013-04-23 11:44:36 +02:00
|
|
|
debug_printf ("%u = waveOutOpen(freq=%d bits=%d channels=%d)", rc, freq_, bits_, channels_);
|
2004-05-28 21:50:07 +02:00
|
|
|
|
2004-03-23 12:05:56 +01:00
|
|
|
return (rc == MMSYSERR_NOERROR);
|
2001-04-16 05:27:16 +02:00
|
|
|
}
|
|
|
|
|
2004-03-23 12:05:56 +01:00
|
|
|
void
|
2004-04-13 11:38:32 +02:00
|
|
|
fhandler_dev_dsp::Audio_out::stop (bool immediately)
|
2001-04-16 05:27:16 +02:00
|
|
|
{
|
2004-03-23 12:05:56 +01:00
|
|
|
MMRESULT rc;
|
|
|
|
WAVEHDR *pHdr;
|
|
|
|
|
2013-04-23 11:44:36 +02:00
|
|
|
debug_printf ("dev_=%p", dev_);
|
2004-08-17 11:52:50 +02:00
|
|
|
if (dev_)
|
2001-04-16 05:27:16 +02:00
|
|
|
{
|
2004-04-13 11:38:32 +02:00
|
|
|
if (!immediately)
|
|
|
|
{
|
2004-05-28 21:50:07 +02:00
|
|
|
sendcurrent (); // force out last block whatever size..
|
|
|
|
waitforallsent (); // block till finished..
|
2004-04-13 11:38:32 +02:00
|
|
|
}
|
2004-03-23 12:05:56 +01:00
|
|
|
|
|
|
|
rc = waveOutReset (dev_);
|
2013-04-23 11:44:36 +02:00
|
|
|
debug_printf ("%u = waveOutReset()", rc);
|
2004-08-17 11:52:50 +02:00
|
|
|
while (Qisr2app_->recv (&pHdr))
|
2001-04-16 05:27:16 +02:00
|
|
|
{
|
2004-08-17 11:52:50 +02:00
|
|
|
rc = waveOutUnprepareHeader (dev_, pHdr, sizeof (WAVEHDR));
|
2013-04-23 11:44:36 +02:00
|
|
|
debug_printf ("%u = waveOutUnprepareHeader(%p)", rc, pHdr);
|
2004-03-23 12:05:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
rc = waveOutClose (dev_);
|
2013-04-23 11:44:36 +02:00
|
|
|
debug_printf ("%u = waveOutClose()", rc);
|
2004-03-23 12:05:56 +01:00
|
|
|
|
2004-08-17 11:52:50 +02:00
|
|
|
Qisr2app_->dellock ();
|
2001-04-16 05:27:16 +02:00
|
|
|
}
|
2004-03-23 12:05:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
fhandler_dev_dsp::Audio_out::init (unsigned blockSize)
|
|
|
|
{
|
|
|
|
int i;
|
2001-04-16 05:27:16 +02:00
|
|
|
|
2004-03-23 12:05:56 +01:00
|
|
|
// internally queue all of our buffer for later use by write
|
2004-08-17 11:52:50 +02:00
|
|
|
Qisr2app_->reset ();
|
2004-03-23 12:05:56 +01:00
|
|
|
for (i = 0; i < MAX_BLOCKS; i++)
|
2001-04-16 05:27:16 +02:00
|
|
|
{
|
2004-03-23 12:05:56 +01:00
|
|
|
wavehdr_[i].lpData = &bigwavebuffer_[i * blockSize];
|
2004-07-19 15:13:48 +02:00
|
|
|
wavehdr_[i].dwUser = (int) blockSize;
|
2004-08-17 11:52:50 +02:00
|
|
|
wavehdr_[i].dwFlags = 0;
|
|
|
|
if (!Qisr2app_->send (&wavehdr_[i]))
|
2004-03-23 12:05:56 +01:00
|
|
|
{
|
2004-08-17 11:52:50 +02:00
|
|
|
system_printf ("Internal Error i=%d", i);
|
2004-03-23 12:05:56 +01:00
|
|
|
break; // should not happen
|
|
|
|
}
|
2001-04-16 05:27:16 +02:00
|
|
|
}
|
2004-03-23 12:05:56 +01:00
|
|
|
pHdr_ = NULL;
|
2001-04-16 05:27:16 +02:00
|
|
|
}
|
|
|
|
|
2011-05-04 13:41:22 +02:00
|
|
|
int
|
2004-03-23 12:05:56 +01:00
|
|
|
fhandler_dev_dsp::Audio_out::write (const char *pSampleData, int nBytes)
|
2001-04-16 05:27:16 +02:00
|
|
|
{
|
2011-05-04 13:41:22 +02:00
|
|
|
int bytes_to_write = nBytes;
|
|
|
|
while (bytes_to_write != 0)
|
2004-03-23 12:05:56 +01:00
|
|
|
{ // Block if all blocks used until at least one is free
|
2011-05-04 13:41:22 +02:00
|
|
|
if (!waitforspace ())
|
|
|
|
{
|
|
|
|
if (bytes_to_write != nBytes)
|
|
|
|
break;
|
|
|
|
return -1;
|
|
|
|
}
|
2004-03-23 12:05:56 +01:00
|
|
|
|
|
|
|
int sizeleft = (int)pHdr_->dwUser - bufferIndex_;
|
2011-05-04 13:41:22 +02:00
|
|
|
if (bytes_to_write < sizeleft)
|
2004-03-23 12:05:56 +01:00
|
|
|
{ // all data fits into the current block, with some space left
|
2011-05-04 13:41:22 +02:00
|
|
|
memcpy (&pHdr_->lpData[bufferIndex_], pSampleData, bytes_to_write);
|
|
|
|
bufferIndex_ += bytes_to_write;
|
2012-02-09 16:23:17 +01:00
|
|
|
bytes_to_write = 0;
|
2004-03-23 12:05:56 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{ // data will fill up the current block
|
|
|
|
memcpy (&pHdr_->lpData[bufferIndex_], pSampleData, sizeleft);
|
|
|
|
bufferIndex_ += sizeleft;
|
|
|
|
sendcurrent ();
|
|
|
|
pSampleData += sizeleft;
|
2011-05-04 13:41:22 +02:00
|
|
|
bytes_to_write -= sizeleft;
|
2004-03-23 12:05:56 +01:00
|
|
|
}
|
2001-04-16 05:27:16 +02:00
|
|
|
}
|
2011-05-04 13:41:22 +02:00
|
|
|
return nBytes - bytes_to_write;
|
2004-03-23 12:05:56 +01:00
|
|
|
}
|
2001-04-16 05:27:16 +02:00
|
|
|
|
2004-03-23 12:05:56 +01:00
|
|
|
void
|
|
|
|
fhandler_dev_dsp::Audio_out::buf_info (audio_buf_info *p,
|
|
|
|
int rate, int bits, int channels)
|
|
|
|
{
|
|
|
|
p->fragstotal = MAX_BLOCKS;
|
2004-08-17 11:52:50 +02:00
|
|
|
if (this && dev_)
|
2001-04-16 05:27:16 +02:00
|
|
|
{
|
2004-08-17 11:52:50 +02:00
|
|
|
/* If the device is running we use the internal values,
|
|
|
|
possibly set from the wave file. */
|
|
|
|
p->fragsize = blockSize (freq_, bits_, channels_);
|
|
|
|
p->fragments = Qisr2app_->query ();
|
2004-03-23 12:05:56 +01:00
|
|
|
if (pHdr_ != NULL)
|
2004-04-13 11:38:32 +02:00
|
|
|
p->bytes = (int)pHdr_->dwUser - bufferIndex_
|
|
|
|
+ p->fragsize * p->fragments;
|
2004-03-23 12:05:56 +01:00
|
|
|
else
|
|
|
|
p->bytes = p->fragsize * p->fragments;
|
2001-04-16 05:27:16 +02:00
|
|
|
}
|
2004-03-23 12:05:56 +01:00
|
|
|
else
|
2001-04-16 05:27:16 +02:00
|
|
|
{
|
2004-08-17 11:52:50 +02:00
|
|
|
p->fragsize = blockSize (rate, bits, channels);
|
2004-03-23 12:05:56 +01:00
|
|
|
p->fragments = MAX_BLOCKS;
|
|
|
|
p->bytes = p->fragsize * p->fragments;
|
2001-04-16 05:27:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-03-23 12:05:56 +01:00
|
|
|
/* This is called on an interupt so use locking.. Note Qisr2app_
|
|
|
|
is used so we should wrap all references to it in locks. */
|
2004-08-17 11:52:50 +02:00
|
|
|
inline void
|
2004-03-23 12:05:56 +01:00
|
|
|
fhandler_dev_dsp::Audio_out::callback_sampledone (WAVEHDR *pHdr)
|
2001-04-16 05:27:16 +02:00
|
|
|
{
|
2004-03-23 12:05:56 +01:00
|
|
|
Qisr2app_->send (pHdr);
|
2001-04-16 05:27:16 +02:00
|
|
|
}
|
|
|
|
|
2011-05-04 13:41:22 +02:00
|
|
|
bool
|
2004-03-23 12:05:56 +01:00
|
|
|
fhandler_dev_dsp::Audio_out::waitforspace ()
|
2001-04-16 05:27:16 +02:00
|
|
|
{
|
2004-03-23 12:05:56 +01:00
|
|
|
WAVEHDR *pHdr;
|
|
|
|
MMRESULT rc = WAVERR_STILLPLAYING;
|
2001-05-20 19:31:06 +02:00
|
|
|
|
2004-03-23 12:05:56 +01:00
|
|
|
if (pHdr_ != NULL)
|
2011-05-04 13:41:22 +02:00
|
|
|
return true;
|
2004-08-17 11:52:50 +02:00
|
|
|
while (!Qisr2app_->recv (&pHdr))
|
2004-03-23 12:05:56 +01:00
|
|
|
{
|
2011-05-04 13:41:22 +02:00
|
|
|
if (fh->is_nonblocking ())
|
|
|
|
{
|
|
|
|
set_errno (EAGAIN);
|
|
|
|
return false;
|
|
|
|
}
|
2004-08-17 11:52:50 +02:00
|
|
|
debug_printf ("100ms");
|
2011-12-09 17:02:56 +01:00
|
|
|
switch (cygwait (100))
|
2011-05-04 13:41:22 +02:00
|
|
|
{
|
2012-06-17 22:50:24 +02:00
|
|
|
case WAIT_SIGNALED:
|
2011-05-04 13:41:22 +02:00
|
|
|
if (!_my_tls.call_signal_handler ())
|
|
|
|
{
|
|
|
|
set_errno (EINTR);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
break;
|
2012-06-17 22:50:24 +02:00
|
|
|
case WAIT_CANCELED:
|
2011-05-04 13:41:22 +02:00
|
|
|
pthread::static_cancel_self ();
|
|
|
|
/*NOTREACHED*/
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2004-08-17 11:52:50 +02:00
|
|
|
}
|
|
|
|
if (pHdr->dwFlags)
|
|
|
|
{
|
|
|
|
/* Errors are ignored here. They will probbaly cause a failure
|
|
|
|
in the subsequent PrepareHeader */
|
|
|
|
rc = waveOutUnprepareHeader (dev_, pHdr, sizeof (WAVEHDR));
|
2013-04-23 11:44:36 +02:00
|
|
|
debug_printf ("%u = waveOutUnprepareHeader(%p)", rc, pHdr);
|
2004-03-23 12:05:56 +01:00
|
|
|
}
|
|
|
|
pHdr_ = pHdr;
|
|
|
|
bufferIndex_ = 0;
|
2011-05-04 13:41:22 +02:00
|
|
|
return true;
|
2001-04-16 05:27:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2004-03-23 12:05:56 +01:00
|
|
|
fhandler_dev_dsp::Audio_out::waitforallsent ()
|
2001-04-16 05:27:16 +02:00
|
|
|
{
|
2004-08-17 11:52:50 +02:00
|
|
|
while (Qisr2app_->query () != MAX_BLOCKS)
|
2001-04-16 05:27:16 +02:00
|
|
|
{
|
2004-08-17 11:52:50 +02:00
|
|
|
debug_printf ("%d blocks in Qisr2app", Qisr2app_->query ());
|
2004-03-23 12:05:56 +01:00
|
|
|
Sleep (100);
|
2001-04-16 05:27:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-03-23 12:05:56 +01:00
|
|
|
// send the block described by pHdr_ and bufferIndex_ to wave device
|
2001-04-16 05:27:16 +02:00
|
|
|
bool
|
2004-03-23 12:05:56 +01:00
|
|
|
fhandler_dev_dsp::Audio_out::sendcurrent ()
|
2001-04-16 05:27:16 +02:00
|
|
|
{
|
2004-03-23 12:05:56 +01:00
|
|
|
WAVEHDR *pHdr = pHdr_;
|
2004-08-17 11:52:50 +02:00
|
|
|
MMRESULT rc;
|
2013-04-23 11:44:36 +02:00
|
|
|
debug_printf ("pHdr=%p bytes=%d", pHdr, bufferIndex_);
|
2004-08-17 11:52:50 +02:00
|
|
|
|
2004-03-23 12:05:56 +01:00
|
|
|
if (pHdr_ == NULL)
|
2001-05-20 19:31:06 +02:00
|
|
|
return false;
|
2004-03-23 12:05:56 +01:00
|
|
|
pHdr_ = NULL;
|
2001-05-20 19:31:06 +02:00
|
|
|
|
2004-03-23 12:05:56 +01:00
|
|
|
// Sample buffer conversion
|
|
|
|
(this->*convert_) ((unsigned char *)pHdr->lpData, bufferIndex_);
|
2001-04-25 09:26:54 +02:00
|
|
|
|
2004-03-23 12:05:56 +01:00
|
|
|
// Send internal buffer out to the soundcard
|
|
|
|
pHdr->dwBufferLength = bufferIndex_;
|
2004-08-17 11:52:50 +02:00
|
|
|
rc = waveOutPrepareHeader (dev_, pHdr, sizeof (WAVEHDR));
|
2013-04-23 11:44:36 +02:00
|
|
|
debug_printf ("%u = waveOutPrepareHeader(%p)", rc, pHdr);
|
2004-08-17 11:52:50 +02:00
|
|
|
if (rc == MMSYSERR_NOERROR)
|
2001-04-16 05:27:16 +02:00
|
|
|
{
|
2004-08-17 11:52:50 +02:00
|
|
|
rc = waveOutWrite (dev_, pHdr, sizeof (WAVEHDR));
|
2013-04-23 11:44:36 +02:00
|
|
|
debug_printf ("%u = waveOutWrite(%p)", rc, pHdr);
|
2001-04-16 05:27:16 +02:00
|
|
|
}
|
2004-08-17 11:52:50 +02:00
|
|
|
if (rc == MMSYSERR_NOERROR)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
/* FIXME: Should we return an error instead ?*/
|
|
|
|
pHdr->dwFlags = 0; /* avoid calling UnprepareHeader again */
|
|
|
|
Qisr2app_->send (pHdr);
|
2001-04-16 05:27:16 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------
|
|
|
|
// Call back routine
|
2001-05-20 19:31:06 +02:00
|
|
|
static void CALLBACK
|
2013-04-23 11:44:36 +02:00
|
|
|
waveOut_callback (HWAVEOUT hWave, UINT msg, DWORD_PTR instance,
|
|
|
|
DWORD_PTR param1, DWORD_PTR param2)
|
2001-04-16 05:27:16 +02:00
|
|
|
{
|
|
|
|
if (msg == WOM_DONE)
|
|
|
|
{
|
2004-03-23 12:05:56 +01:00
|
|
|
fhandler_dev_dsp::Audio_out *ptr =
|
|
|
|
(fhandler_dev_dsp::Audio_out *) instance;
|
|
|
|
ptr->callback_sampledone ((WAVEHDR *) param1);
|
2001-04-16 05:27:16 +02:00
|
|
|
}
|
|
|
|
}
|
2001-05-20 19:31:06 +02:00
|
|
|
|
2001-04-16 05:27:16 +02:00
|
|
|
//------------------------------------------------------------------------
|
|
|
|
// wav file detection..
|
|
|
|
#pragma pack(1)
|
|
|
|
struct wavchunk
|
2001-05-20 19:31:06 +02:00
|
|
|
{
|
2001-04-16 05:27:16 +02:00
|
|
|
char id[4];
|
|
|
|
unsigned int len;
|
|
|
|
};
|
|
|
|
struct wavformat
|
|
|
|
{
|
|
|
|
unsigned short wFormatTag;
|
|
|
|
unsigned short wChannels;
|
|
|
|
unsigned int dwSamplesPerSec;
|
|
|
|
unsigned int dwAvgBytesPerSec;
|
|
|
|
unsigned short wBlockAlign;
|
|
|
|
unsigned short wBitsPerSample;
|
|
|
|
};
|
|
|
|
#pragma pack()
|
|
|
|
|
|
|
|
bool
|
2004-03-23 12:05:56 +01:00
|
|
|
fhandler_dev_dsp::Audio_out::parsewav (const char * &pData, int &nBytes,
|
2004-08-17 11:52:50 +02:00
|
|
|
int dev_freq, int dev_bits, int dev_channels)
|
2001-04-16 05:27:16 +02:00
|
|
|
{
|
|
|
|
int len;
|
|
|
|
const char *end = pData + nBytes;
|
2004-03-23 12:05:56 +01:00
|
|
|
const char *pDat;
|
|
|
|
int skip = 0;
|
2004-08-17 11:52:50 +02:00
|
|
|
|
|
|
|
/* Start with default values from the device handler */
|
|
|
|
freq_ = dev_freq;
|
|
|
|
bits_ = dev_bits;
|
|
|
|
channels_ = dev_channels;
|
|
|
|
setconvert (bits_ == 8 ? AFMT_U8 : AFMT_S16_LE);
|
|
|
|
|
2004-04-13 11:38:32 +02:00
|
|
|
// Check alignment first: A lot of the code below depends on it
|
2013-04-23 11:44:36 +02:00
|
|
|
if (((uintptr_t)pData & 0x3) != 0)
|
2004-04-13 11:38:32 +02:00
|
|
|
return false;
|
2004-03-23 12:05:56 +01:00
|
|
|
if (!(pData[0] == 'R' && pData[1] == 'I'
|
|
|
|
&& pData[2] == 'F' && pData[3] == 'F'))
|
2001-04-16 05:27:16 +02:00
|
|
|
return false;
|
2004-03-23 12:05:56 +01:00
|
|
|
if (!(pData[8] == 'W' && pData[9] == 'A'
|
|
|
|
&& pData[10] == 'V' && pData[11] == 'E'))
|
2001-04-16 05:27:16 +02:00
|
|
|
return false;
|
2001-05-20 19:31:06 +02:00
|
|
|
|
|
|
|
len = *(int *) &pData[4];
|
2004-03-23 12:05:56 +01:00
|
|
|
len -= 12;
|
|
|
|
pDat = pData + 12;
|
|
|
|
skip = 12;
|
|
|
|
while ((len > 0) && (pDat + sizeof (wavchunk) < end))
|
|
|
|
{ /* We recognize two kinds of wavchunk:
|
|
|
|
"fmt " for the PCM parameters (only PCM supported here)
|
|
|
|
"data" for the start of PCM data */
|
|
|
|
wavchunk * pChunk = (wavchunk *) pDat;
|
2001-05-20 19:31:06 +02:00
|
|
|
int blklen = pChunk-> len;
|
2004-03-23 12:05:56 +01:00
|
|
|
if (pChunk->id[0] == 'f' && pChunk->id[1] == 'm'
|
|
|
|
&& pChunk->id[2] == 't' && pChunk->id[3] == ' ')
|
2001-04-16 05:27:16 +02:00
|
|
|
{
|
2001-05-20 19:31:06 +02:00
|
|
|
wavformat *format = (wavformat *) (pChunk + 1);
|
2004-03-23 12:05:56 +01:00
|
|
|
if ((char *) (format + 1) >= end)
|
2001-04-16 05:27:16 +02:00
|
|
|
return false;
|
2004-03-23 12:05:56 +01:00
|
|
|
// We have found the parameter chunk
|
|
|
|
if (format->wFormatTag == 0x0001)
|
|
|
|
{ // Micr*s*ft PCM; check if parameters work with our device
|
|
|
|
if (query (format->dwSamplesPerSec, format->wBitsPerSample,
|
|
|
|
format->wChannels))
|
|
|
|
{ // return the parameters we found
|
2004-08-17 11:52:50 +02:00
|
|
|
freq_ = format->dwSamplesPerSec;
|
|
|
|
bits_ = format->wBitsPerSample;
|
|
|
|
channels_ = format->wChannels;
|
2004-03-23 12:05:56 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (pChunk->id[0] == 'd' && pChunk->id[1] == 'a'
|
|
|
|
&& pChunk->id[2] == 't' && pChunk->id[3] == 'a')
|
|
|
|
{ // throw away all the header & not output it to the soundcard.
|
|
|
|
skip += sizeof (wavchunk);
|
|
|
|
debug_printf ("Discard %d bytes wave header", skip);
|
|
|
|
pData += skip;
|
|
|
|
nBytes -= skip;
|
2004-08-17 11:52:50 +02:00
|
|
|
setconvert (bits_ == 8 ? AFMT_U8 : AFMT_S16_LE);
|
2004-03-23 12:05:56 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pDat += blklen + sizeof (wavchunk);
|
|
|
|
skip += blklen + sizeof (wavchunk);
|
|
|
|
len -= blklen + sizeof (wavchunk);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ========================================================================
|
|
|
|
Buffering concept for Audio_in:
|
|
|
|
On the first read, we queue all blocks of our bigwavebuffer
|
|
|
|
for reception and start the wave-in device.
|
|
|
|
We manage queues of pointers to WAVEHDR
|
|
|
|
When a block has been filled, the callback puts the corresponding
|
2004-08-17 11:52:50 +02:00
|
|
|
WAVEHDR pointer into a queue.
|
2004-03-23 12:05:56 +01:00
|
|
|
The function read() blocks (polled, sigh) until at least one good buffer
|
|
|
|
has arrived, then the data is copied into the buffer provided to read().
|
|
|
|
After a buffer has been fully used by read(), it is queued again
|
|
|
|
to the wave-in device immediately.
|
|
|
|
The function read() iterates until all data requested has been
|
|
|
|
received, there is no way to interrupt it */
|
|
|
|
|
2004-08-17 11:52:50 +02:00
|
|
|
void
|
|
|
|
fhandler_dev_dsp::Audio_in::fork_fixup (HANDLE parent)
|
2004-03-23 12:05:56 +01:00
|
|
|
{
|
2004-08-17 11:52:50 +02:00
|
|
|
/* Null dev_.
|
|
|
|
It will be necessary to reset the queue, open the device
|
|
|
|
and create a lock when reading */
|
2013-04-23 11:44:36 +02:00
|
|
|
debug_printf ("parent=%p", parent);
|
2004-08-17 11:52:50 +02:00
|
|
|
dev_ = NULL;
|
2004-03-23 12:05:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
fhandler_dev_dsp::Audio_in::query (int rate, int bits, int channels)
|
|
|
|
{
|
|
|
|
WAVEFORMATEX format;
|
|
|
|
MMRESULT rc;
|
|
|
|
|
|
|
|
fillFormat (&format, rate, bits, channels);
|
|
|
|
rc = waveInOpen (NULL, WAVE_MAPPER, &format, 0L, 0L, WAVE_FORMAT_QUERY);
|
2013-04-23 11:44:36 +02:00
|
|
|
debug_printf ("%u = waveInOpen(freq=%d bits=%d channels=%d)", rc, rate, bits, channels);
|
2004-03-23 12:05:56 +01:00
|
|
|
return (rc == MMSYSERR_NOERROR);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
fhandler_dev_dsp::Audio_in::start (int rate, int bits, int channels)
|
|
|
|
{
|
|
|
|
WAVEFORMATEX format;
|
|
|
|
MMRESULT rc;
|
|
|
|
unsigned bSize = blockSize (rate, bits, channels);
|
|
|
|
|
2004-08-17 11:52:50 +02:00
|
|
|
if (dev_)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
/* In case of fork bigwavebuffer may already exist */
|
|
|
|
if (!bigwavebuffer_)
|
|
|
|
bigwavebuffer_ = new char[MAX_BLOCKS * bSize];
|
|
|
|
|
|
|
|
if (!isvalid ())
|
2004-03-23 12:05:56 +01:00
|
|
|
return false;
|
|
|
|
|
|
|
|
fillFormat (&format, rate, bits, channels);
|
2013-04-23 11:44:36 +02:00
|
|
|
rc = waveInOpen (&dev_, WAVE_MAPPER, &format, (DWORD_PTR) waveIn_callback,
|
|
|
|
(DWORD_PTR) this, CALLBACK_FUNCTION);
|
|
|
|
debug_printf ("%u = waveInOpen(rate=%d bits=%d channels=%d)", rc, rate, bits, channels);
|
2004-08-17 11:52:50 +02:00
|
|
|
|
2004-03-23 12:05:56 +01:00
|
|
|
if (rc == MMSYSERR_NOERROR)
|
|
|
|
{
|
|
|
|
if (!init (bSize))
|
2004-08-17 11:52:50 +02:00
|
|
|
return false;
|
2004-03-23 12:05:56 +01:00
|
|
|
}
|
|
|
|
return (rc == MMSYSERR_NOERROR);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
fhandler_dev_dsp::Audio_in::stop ()
|
|
|
|
{
|
|
|
|
MMRESULT rc;
|
|
|
|
WAVEHDR *pHdr;
|
|
|
|
|
2013-04-23 11:44:36 +02:00
|
|
|
debug_printf ("dev_=%p", dev_);
|
2004-08-17 11:52:50 +02:00
|
|
|
if (dev_)
|
2004-03-23 12:05:56 +01:00
|
|
|
{
|
|
|
|
/* Note that waveInReset calls our callback for all incomplete buffers.
|
|
|
|
Since all the win32 wave functions appear to use a common lock,
|
|
|
|
we must not call into the wave API from the callback.
|
|
|
|
Otherwise we end up in a deadlock. */
|
2004-08-17 11:52:50 +02:00
|
|
|
rc = waveInReset (dev_);
|
2013-04-23 11:44:36 +02:00
|
|
|
debug_printf ("%u = waveInReset()", rc);
|
2004-03-23 12:05:56 +01:00
|
|
|
|
2004-08-17 11:52:50 +02:00
|
|
|
while (Qisr2app_->recv (&pHdr))
|
2004-03-23 12:05:56 +01:00
|
|
|
{
|
2004-08-17 11:52:50 +02:00
|
|
|
rc = waveInUnprepareHeader (dev_, pHdr, sizeof (WAVEHDR));
|
2013-04-23 11:44:36 +02:00
|
|
|
debug_printf ("%u = waveInUnprepareHeader(%p)", rc, pHdr);
|
2004-03-23 12:05:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
rc = waveInClose (dev_);
|
2013-04-23 11:44:36 +02:00
|
|
|
debug_printf ("%u = waveInClose()", rc);
|
2004-03-23 12:05:56 +01:00
|
|
|
|
2004-08-17 11:52:50 +02:00
|
|
|
Qisr2app_->dellock ();
|
2004-03-23 12:05:56 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
fhandler_dev_dsp::Audio_in::queueblock (WAVEHDR *pHdr)
|
|
|
|
{
|
|
|
|
MMRESULT rc;
|
|
|
|
rc = waveInPrepareHeader (dev_, pHdr, sizeof (WAVEHDR));
|
2013-04-23 11:44:36 +02:00
|
|
|
debug_printf ("%u = waveInPrepareHeader(%p)", rc, pHdr);
|
2004-03-23 12:05:56 +01:00
|
|
|
if (rc == MMSYSERR_NOERROR)
|
2004-08-17 11:52:50 +02:00
|
|
|
{
|
|
|
|
rc = waveInAddBuffer (dev_, pHdr, sizeof (WAVEHDR));
|
2013-04-23 11:44:36 +02:00
|
|
|
debug_printf ("%u = waveInAddBuffer(%p)", rc, pHdr);
|
2004-08-17 11:52:50 +02:00
|
|
|
}
|
|
|
|
if (rc == MMSYSERR_NOERROR)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
/* FIXME: Should the calling function return an error instead ?*/
|
|
|
|
pHdr->dwFlags = 0; /* avoid calling UnprepareHeader again */
|
|
|
|
pHdr->dwBytesRecorded = 0; /* no data will have been read */
|
|
|
|
Qisr2app_->send (pHdr);
|
|
|
|
return false;
|
2004-03-23 12:05:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
fhandler_dev_dsp::Audio_in::init (unsigned blockSize)
|
|
|
|
{
|
|
|
|
MMRESULT rc;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
// try to queue all of our buffer for reception
|
2004-08-17 11:52:50 +02:00
|
|
|
Qisr2app_->reset ();
|
2004-03-23 12:05:56 +01:00
|
|
|
for (i = 0; i < MAX_BLOCKS; i++)
|
|
|
|
{
|
|
|
|
wavehdr_[i].lpData = &bigwavebuffer_[i * blockSize];
|
|
|
|
wavehdr_[i].dwBufferLength = blockSize;
|
2004-08-17 11:52:50 +02:00
|
|
|
wavehdr_[i].dwFlags = 0;
|
2004-03-23 12:05:56 +01:00
|
|
|
if (!queueblock (&wavehdr_[i]))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
pHdr_ = NULL;
|
|
|
|
rc = waveInStart (dev_);
|
2013-04-23 11:44:36 +02:00
|
|
|
debug_printf ("%u = waveInStart(), queued=%d", rc, i);
|
2004-03-23 12:05:56 +01:00
|
|
|
return (rc == MMSYSERR_NOERROR);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
fhandler_dev_dsp::Audio_in::read (char *pSampleData, int &nBytes)
|
|
|
|
{
|
|
|
|
int bytes_to_read = nBytes;
|
|
|
|
nBytes = 0;
|
2013-04-23 11:44:36 +02:00
|
|
|
debug_printf ("pSampleData=%p nBytes=%d", pSampleData, bytes_to_read);
|
2004-03-23 12:05:56 +01:00
|
|
|
while (bytes_to_read != 0)
|
|
|
|
{ // Block till next sound has been read
|
2011-05-04 13:41:22 +02:00
|
|
|
if (!waitfordata ())
|
2011-06-06 07:02:13 +02:00
|
|
|
{
|
2011-05-04 13:41:22 +02:00
|
|
|
if (nBytes)
|
|
|
|
return true;
|
|
|
|
nBytes = -1;
|
|
|
|
return false;
|
|
|
|
}
|
2004-03-23 12:05:56 +01:00
|
|
|
|
|
|
|
// Handle gathering our blocks into smaller or larger buffer
|
|
|
|
int sizeleft = pHdr_->dwBytesRecorded - bufferIndex_;
|
|
|
|
if (bytes_to_read < sizeleft)
|
|
|
|
{ // The current buffer holds more data than requested
|
|
|
|
memcpy (pSampleData, &pHdr_->lpData[bufferIndex_], bytes_to_read);
|
|
|
|
(this->*convert_) ((unsigned char *)pSampleData, bytes_to_read);
|
|
|
|
nBytes += bytes_to_read;
|
|
|
|
bufferIndex_ += bytes_to_read;
|
2004-05-28 21:50:07 +02:00
|
|
|
debug_printf ("got %d", bytes_to_read);
|
2004-03-23 12:05:56 +01:00
|
|
|
break; // done; use remaining data in next call to read
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{ // not enough or exact amount in the current buffer
|
|
|
|
if (sizeleft)
|
|
|
|
{ // use up what we have
|
|
|
|
memcpy (pSampleData, &pHdr_->lpData[bufferIndex_], sizeleft);
|
|
|
|
(this->*convert_) ((unsigned char *)pSampleData, sizeleft);
|
|
|
|
nBytes += sizeleft;
|
|
|
|
bytes_to_read -= sizeleft;
|
|
|
|
pSampleData += sizeleft;
|
|
|
|
debug_printf ("got %d", sizeleft);
|
2001-04-16 05:27:16 +02:00
|
|
|
}
|
2004-03-23 12:05:56 +01:00
|
|
|
queueblock (pHdr_); // re-queue this block to ISR
|
|
|
|
pHdr_ = NULL; // need to wait for a new block
|
|
|
|
// if more samples are needed, we need a new block now
|
|
|
|
}
|
|
|
|
}
|
|
|
|
debug_printf ("end nBytes=%d", nBytes);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-05-04 13:41:22 +02:00
|
|
|
bool
|
2004-03-23 12:05:56 +01:00
|
|
|
fhandler_dev_dsp::Audio_in::waitfordata ()
|
|
|
|
{
|
|
|
|
WAVEHDR *pHdr;
|
|
|
|
MMRESULT rc;
|
|
|
|
|
|
|
|
if (pHdr_ != NULL)
|
2011-05-04 13:41:22 +02:00
|
|
|
return true;
|
2004-08-17 11:52:50 +02:00
|
|
|
while (!Qisr2app_->recv (&pHdr))
|
2004-03-23 12:05:56 +01:00
|
|
|
{
|
2011-05-04 13:41:22 +02:00
|
|
|
if (fh->is_nonblocking ())
|
|
|
|
{
|
|
|
|
set_errno (EAGAIN);
|
|
|
|
return false;
|
|
|
|
}
|
2004-08-17 11:52:50 +02:00
|
|
|
debug_printf ("100ms");
|
2011-12-09 17:02:56 +01:00
|
|
|
switch (cygwait (100))
|
2011-05-04 13:41:22 +02:00
|
|
|
{
|
2012-06-17 22:50:24 +02:00
|
|
|
case WAIT_SIGNALED:
|
2011-05-04 13:41:22 +02:00
|
|
|
if (!_my_tls.call_signal_handler ())
|
|
|
|
{
|
|
|
|
set_errno (EINTR);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
break;
|
2012-06-17 22:50:24 +02:00
|
|
|
case WAIT_CANCELED:
|
2011-05-04 13:41:22 +02:00
|
|
|
pthread::static_cancel_self ();
|
|
|
|
/*NOTREACHED*/
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2004-08-17 11:52:50 +02:00
|
|
|
}
|
|
|
|
if (pHdr->dwFlags) /* Zero if queued following error in queueblock */
|
|
|
|
{
|
|
|
|
/* Errors are ignored here. They will probbaly cause a failure
|
2004-09-12 05:47:57 +02:00
|
|
|
in the subsequent PrepareHeader */
|
2004-08-17 11:52:50 +02:00
|
|
|
rc = waveInUnprepareHeader (dev_, pHdr, sizeof (WAVEHDR));
|
2013-04-23 11:44:36 +02:00
|
|
|
debug_printf ("%u = waveInUnprepareHeader(%p)", rc, pHdr);
|
2004-03-23 12:05:56 +01:00
|
|
|
}
|
|
|
|
pHdr_ = pHdr;
|
2004-05-28 21:50:07 +02:00
|
|
|
bufferIndex_ = 0;
|
2011-05-04 13:41:22 +02:00
|
|
|
return true;
|
2004-03-23 12:05:56 +01:00
|
|
|
}
|
2001-04-16 05:27:16 +02:00
|
|
|
|
2004-04-13 11:38:32 +02:00
|
|
|
void
|
|
|
|
fhandler_dev_dsp::Audio_in::buf_info (audio_buf_info *p,
|
|
|
|
int rate, int bits, int channels)
|
|
|
|
{
|
|
|
|
p->fragstotal = MAX_BLOCKS;
|
|
|
|
p->fragsize = blockSize (rate, bits, channels);
|
2004-08-17 11:52:50 +02:00
|
|
|
if (this && dev_)
|
2004-04-13 11:38:32 +02:00
|
|
|
{
|
|
|
|
p->fragments = Qisr2app_->query ();
|
|
|
|
if (pHdr_ != NULL)
|
|
|
|
p->bytes = pHdr_->dwBytesRecorded - bufferIndex_
|
|
|
|
+ p->fragsize * p->fragments;
|
|
|
|
else
|
|
|
|
p->bytes = p->fragsize * p->fragments;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
p->fragments = 0;
|
|
|
|
p->bytes = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-08-17 11:52:50 +02:00
|
|
|
inline void
|
2004-03-23 12:05:56 +01:00
|
|
|
fhandler_dev_dsp::Audio_in::callback_blockfull (WAVEHDR *pHdr)
|
|
|
|
{
|
|
|
|
Qisr2app_->send (pHdr);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void CALLBACK
|
2013-04-23 11:44:36 +02:00
|
|
|
waveIn_callback (HWAVEIN hWave, UINT msg, DWORD_PTR instance, DWORD_PTR param1,
|
|
|
|
DWORD_PTR param2)
|
2004-03-23 12:05:56 +01:00
|
|
|
{
|
|
|
|
if (msg == WIM_DATA)
|
|
|
|
{
|
|
|
|
fhandler_dev_dsp::Audio_in *ptr =
|
|
|
|
(fhandler_dev_dsp::Audio_in *) instance;
|
|
|
|
ptr->callback_blockfull ((WAVEHDR *) param1);
|
2001-04-16 05:27:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-03-23 12:05:56 +01:00
|
|
|
|
|
|
|
/* ------------------------------------------------------------------------
|
|
|
|
/dev/dsp handler
|
2004-08-17 11:52:50 +02:00
|
|
|
------------------------------------------------------------------------ */
|
2001-10-13 19:23:35 +02:00
|
|
|
fhandler_dev_dsp::fhandler_dev_dsp ():
|
* devices.cc: New file.
* devices.gperf: New file.
* devices.shilka: New file.
* cygwin-gperf: New file.
* cygwin-shilka: New file.
* fhandler_fifo.cc: New file.
* fhandler_nodevice.cc : New file. Reorganize headers so that path.h precedes
fhandler.h throughout. Remove device argument and unit arguments from fhandler
constructors throughout. Remove pc arguments to fhandler functions and use
internal pc element instead, throughout. Use dev element in pc throughout.
Use major/minor elements rather than units and device numbers previously in
fhandler class. Use correct methods for fhandler file names rather than
directly accessing file name variables, throughout.
* Makefile.in (DLL_OFILES): Add devices.o, fhandler_fifo.o
* dcrt0.cc (dll_crt0_1): Call device::init.
* devices.h: Renumber devices based on more Linux-like major/minor numbers.
Add more devices. Declare standard device storage.
(device): Declare struct.
* dir.cc (opendir): Use new 'build_fh_name' to construct a fhandler_* type.
* dtable.cc (dtable::get_debugger_info): Ditto.
(cygwin_attach_handle_to_fd): Ditto.
(dtable::release): Remove special FH_SOCKET case in favor of generic
"need_fixup_before" test.
(dtable::init_std_file_from_handle): Use either build_fh_dev or build_fh_name
to build standard fhandler.
(dtable::build_fh_name): Renamed from dtable::build_fhandler_from_name. Move
out of dtable class. Don't accept a path_conv argument. Just build it here
and pass it to:
(build_fh_pc): Renamed from dtable::build_fhandler. Move out of dtable class.
Use intrinsic device type in path_conv to create new fhandler.
(build_fh_dev): Renamed from dtable::build_fhandler. Move out of dtable class.
Simplify arguments to just take new 'device' type and a name. Just return
pointer to fhandler rather than trying to insert into dtable.
(dtable::dup_worker): Accommodate above build_fh name changes.
(dtable::find_fifo): New (currently broken) function.
(handle_to_fn): Use strechr for efficiency.
* dtable.h: Reflect above build_fh name changes and argument differences.
(fhandler_base *&operator []): Return self rather than copy of self.
* fhandler.cc (fhandler_base::operator =): Use pc element to set normalized
path.
(fhandler_base::set_name): Ditto.
(fhandler_base::raw_read): Use method to access name.
(fhandler_base::write): Correctly use get_output_handle rather than get_handle.
(handler_base::device_access_denied): New function.
(fhandler_base::open): Eliminate pc argument and use pc element of
fhandler_base throughout.
(fhandler_base::fstat): Detect if device is based in filesystem and use
fstat_fs to calculate stat, if so.
(fhandler_base::fhandler_base): Eliminate handling of file names and, instead,
just free appropriate component from pc.
(fhandler_base::opendir): Remove path_conv parameter.
* fhandler.h: Remove all device flags.
(fhandler_base::pc): New element.
(fhandler_base::set_name): Change argument to path_conv.
(fhandler_base::error): New function.
(fhandler_base::exists): New function.
(fhandler_base::pc_binmode): New function.
(fhandler_base::dev): New function.
(fhandler_base::open_fs): New function.
(fhandler_base::fstat_fs): New function.
(fhandler_base::fstat_by_name): New function.
(fhandler_base::fstat_by_handle): New function.
(fhandler_base::isfifo): New function.
(fhandler_base::is_slow): New function.
(fhandler_base::is_auto_device): New function.
(fhandler_base::is_fs_special): New function.
(fhandler_base::device_access_denied): New function.
(fhandler_base::operator DWORD&): New operator.
(fhandler_base::get_name): Return normalized path from pc.
(fhandler_base::get_win32_name): Return windows path from pc.
(fhandler_base::isdevice): Renamed from is_device.
(fhandler_base::get_native_name): Return device format.
(fhandler_fifo): New class.
(fhandler_nodevice): New class.
(select_stuff::device_specific): Remove array.
(select_stuff::device_specific_pipe): New class element.
(select_stuff::device_specific_socket): New class element.
(select_stuff::device_specific_serial): New class element.
(select_stuff::select_stuff): Initialize new elements.
* fhandler_disk_file.cc (fhandler_base::fstat_by_handle): Move to base class
from fhandler_disk_file.
(fhandler_base::fstat_by_name): Ditto.
(fhandler_base::fstat_by_name): Ditto.
(fhandler_disk_file::open): Move most functionality into
fhandler_base::open_fs.
(fhandler_base::open_fs): New function.
(fhandler_disk_file::close): Move most functionality into
fhandler_base::close_fs.
(fhandler_base::close_fs): New function.
* fhandler_mem.cc (fhandler_dev_mem::open): Use device name in debugging
output.
* fhandler_socket.cc (fhandler_socket::set_connect_secret): Copy standard
urandom device into appropriate place.
(fhandler_socket::accept): Reflect change in fdsock return value.
* fhandler_tty.cc: See "throughouts" above.
* net.cc: Accommodate fdsock change throughout.
(fdsock): Return success or failure, accept fd argument and device argument.
* path.cc (symlink_info::major): New element.
(symlink_info::minor): New element.
(symlink_info::parse_device): Declare new function.
(fs_info::update): Accommodate changes in path_conv class.
(path_conv::fillin): Ditto.
(path_conv::return_and_clear_normalized_path): Eliminate.
(path_conv::set_normalized_path): New function.
(path_conv::path_conv): Set info in dev element. Use path_conv methods Check
for FH_FS rather than FH_BAD to indicate when to fill in filesystem stuff.
where appropriate rather than direct access. Use set_normalized_path to set
normalized path.
(windows_device_names): Eliminate.
(get_dev): Ditto.
(get_raw_device_number): Ditto.
(get_device_number): Ditto.
(win32_device_name): Call new device name parser to do most of the heavy
lifting.
(mount_info::conv_to_win32_path): Fill in dev field as appropriate.
(symlink_worker): Handle new device files.
(symlink_info::check): Ditto.
(symlink_info::parse_device): Define new function.
* path.h (executable_states): Move here from fhandler.h.
(fs_info): Rename variables to *_storage and create methods for accessing same.
(path_conv): Add dev element, remove devn and unit and adjust inline methods to
accommodate.
(set_normalized_path): Declare new function.
* pinfo.cc (_pinfo::commune_recv): Add broken support for handling fifos.
(_pinfo::commune_send): Ditto.
* pipe.cc (fhandler_pipe::close): check for existence of handle before closing
it.
(handler_pipe::create): Rename from make_pipe. Change arguments to accept
fhandler_pipe array. Accommodate fifos.
(pipe): Rework to deal with fhandler_pipe::create changes.
(_pipe): Ditto.
* select.cc: Use individual device_specific types throughout rather than
indexing with obsolete device number.
(set_bits): Use is_socket call rather than checking device number.
* shared_info.h (CURR_MOUNT_MAGIC): Update.
(conv_to_win32_path): Reflect addition of device argument.
* syscalls.cc (mknod_worker): New function.
(open): Use build_fh_name to build fhandler.
(chown_worker): Detect if this is an 'auto' device rather than an on-filesystem
device and handle appropriately.
(chmod_device): New function.
(chmod): Detect if this is an 'auto' device rather than an on-filesystem device
and handle appropriately. Use chmod_device to set mode of in-filesystem
devices.
(stat_worker): Eliminate path_conv argument. Call build_fh_name to construct
fhandler. Use fh->error() rather than pc->error to detect errors in fhandler
construction.
(access_worker): New function pulled from access. Accommodate in-filesystem
devices.
(access): Use access_worker.
(fpathconf): Detect if this is an 'auto' device rather than an on-filesystem
device and handle appropriately.
(mknod_worker): New function.
(mknod32): New function.
(chroot): Free normalized path -- assuming it was actually cmalloced.
* tty.cc (create_tty_master): Tweak for new device class.
(tty::common_init): Ditto.
* winsup.h (stat_worker): Remove.
(symlink_worker): Declare.
* exceptions.cc (set_process_mask): Just call sig_dispatch_pending and don't
worry about pending_signals since sig_dispatch_pending should always do the
right thing now.
(sig_handle): Reorganize SIGCONT handling to more closely conform to SUSv3.
* pinfo.h: Move __SIG enum to sigproc.h.
(PICOM_FIFO): New enum element.
(_pinfo): Remove 'thread2signal' stuff throughout class.
(_pinfo::commune_send): Make varargs.
(_pinfo::sigtodo): Eliminate.
(_pinfo::thread2signal): Ditto.
* signal.cc (kill_worker): Eliminate call to setthread2signal.
* sigproc.cc (local_sigtodo): Eliminate.
(getlocal_sigtodo): Ditto.
(sigelem): New class.
(pending_signals): New class.
(sigqueue): New variable, start of sigqueue linked list.
(sigcatch_nonmain): Eliminate.
(sigcatch_main): Eliminate.
(sigcatch_nosync): Eliminate.
(sigcomplete_nonmain): Eliminate.
(pending_signals): Eliminate.
(sig_clear): Call signal thread to clear pending signals, unless already in
signal thread.
(sigpending): Call signal thread to get pending signals.
(sig_dispatch_pending): Eliminate use of pending_signals and just check
sigqueue.
(sigproc_terminate): Eliminate all of the obsolete semaphore stuff. Close
signal pipe handle.
(sig_send): Eliminate all of the obsolete semaphore stuff and use pipe to send
signals.
(getevent): Eliminate.
(pending_signals::add): New function.
(pending_signals::del): New function.
(pending_signals::next): New function.
(wait_sig): Eliminate all of the obsolete semaphore stuff. Use pipe to
communicate and maintain a linked list of signals.
* sigproc.h: Move __SIG defines here. Add __SIGPENDING.
(sig_dispatch_pending): Remove "C" specifier.
(sig_handle): Accept a mask argument.
* thread.cc: Remove signal handling considerations throughout.
2003-09-25 02:37:18 +02:00
|
|
|
fhandler_base ()
|
2001-04-16 05:27:16 +02:00
|
|
|
{
|
2004-03-23 12:05:56 +01:00
|
|
|
audio_in_ = NULL;
|
|
|
|
audio_out_ = NULL;
|
2011-12-25 05:05:31 +01:00
|
|
|
dev ().parse (FH_OSS_DSP);
|
2001-04-16 05:27:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
* devices.cc: New file.
* devices.gperf: New file.
* devices.shilka: New file.
* cygwin-gperf: New file.
* cygwin-shilka: New file.
* fhandler_fifo.cc: New file.
* fhandler_nodevice.cc : New file. Reorganize headers so that path.h precedes
fhandler.h throughout. Remove device argument and unit arguments from fhandler
constructors throughout. Remove pc arguments to fhandler functions and use
internal pc element instead, throughout. Use dev element in pc throughout.
Use major/minor elements rather than units and device numbers previously in
fhandler class. Use correct methods for fhandler file names rather than
directly accessing file name variables, throughout.
* Makefile.in (DLL_OFILES): Add devices.o, fhandler_fifo.o
* dcrt0.cc (dll_crt0_1): Call device::init.
* devices.h: Renumber devices based on more Linux-like major/minor numbers.
Add more devices. Declare standard device storage.
(device): Declare struct.
* dir.cc (opendir): Use new 'build_fh_name' to construct a fhandler_* type.
* dtable.cc (dtable::get_debugger_info): Ditto.
(cygwin_attach_handle_to_fd): Ditto.
(dtable::release): Remove special FH_SOCKET case in favor of generic
"need_fixup_before" test.
(dtable::init_std_file_from_handle): Use either build_fh_dev or build_fh_name
to build standard fhandler.
(dtable::build_fh_name): Renamed from dtable::build_fhandler_from_name. Move
out of dtable class. Don't accept a path_conv argument. Just build it here
and pass it to:
(build_fh_pc): Renamed from dtable::build_fhandler. Move out of dtable class.
Use intrinsic device type in path_conv to create new fhandler.
(build_fh_dev): Renamed from dtable::build_fhandler. Move out of dtable class.
Simplify arguments to just take new 'device' type and a name. Just return
pointer to fhandler rather than trying to insert into dtable.
(dtable::dup_worker): Accommodate above build_fh name changes.
(dtable::find_fifo): New (currently broken) function.
(handle_to_fn): Use strechr for efficiency.
* dtable.h: Reflect above build_fh name changes and argument differences.
(fhandler_base *&operator []): Return self rather than copy of self.
* fhandler.cc (fhandler_base::operator =): Use pc element to set normalized
path.
(fhandler_base::set_name): Ditto.
(fhandler_base::raw_read): Use method to access name.
(fhandler_base::write): Correctly use get_output_handle rather than get_handle.
(handler_base::device_access_denied): New function.
(fhandler_base::open): Eliminate pc argument and use pc element of
fhandler_base throughout.
(fhandler_base::fstat): Detect if device is based in filesystem and use
fstat_fs to calculate stat, if so.
(fhandler_base::fhandler_base): Eliminate handling of file names and, instead,
just free appropriate component from pc.
(fhandler_base::opendir): Remove path_conv parameter.
* fhandler.h: Remove all device flags.
(fhandler_base::pc): New element.
(fhandler_base::set_name): Change argument to path_conv.
(fhandler_base::error): New function.
(fhandler_base::exists): New function.
(fhandler_base::pc_binmode): New function.
(fhandler_base::dev): New function.
(fhandler_base::open_fs): New function.
(fhandler_base::fstat_fs): New function.
(fhandler_base::fstat_by_name): New function.
(fhandler_base::fstat_by_handle): New function.
(fhandler_base::isfifo): New function.
(fhandler_base::is_slow): New function.
(fhandler_base::is_auto_device): New function.
(fhandler_base::is_fs_special): New function.
(fhandler_base::device_access_denied): New function.
(fhandler_base::operator DWORD&): New operator.
(fhandler_base::get_name): Return normalized path from pc.
(fhandler_base::get_win32_name): Return windows path from pc.
(fhandler_base::isdevice): Renamed from is_device.
(fhandler_base::get_native_name): Return device format.
(fhandler_fifo): New class.
(fhandler_nodevice): New class.
(select_stuff::device_specific): Remove array.
(select_stuff::device_specific_pipe): New class element.
(select_stuff::device_specific_socket): New class element.
(select_stuff::device_specific_serial): New class element.
(select_stuff::select_stuff): Initialize new elements.
* fhandler_disk_file.cc (fhandler_base::fstat_by_handle): Move to base class
from fhandler_disk_file.
(fhandler_base::fstat_by_name): Ditto.
(fhandler_base::fstat_by_name): Ditto.
(fhandler_disk_file::open): Move most functionality into
fhandler_base::open_fs.
(fhandler_base::open_fs): New function.
(fhandler_disk_file::close): Move most functionality into
fhandler_base::close_fs.
(fhandler_base::close_fs): New function.
* fhandler_mem.cc (fhandler_dev_mem::open): Use device name in debugging
output.
* fhandler_socket.cc (fhandler_socket::set_connect_secret): Copy standard
urandom device into appropriate place.
(fhandler_socket::accept): Reflect change in fdsock return value.
* fhandler_tty.cc: See "throughouts" above.
* net.cc: Accommodate fdsock change throughout.
(fdsock): Return success or failure, accept fd argument and device argument.
* path.cc (symlink_info::major): New element.
(symlink_info::minor): New element.
(symlink_info::parse_device): Declare new function.
(fs_info::update): Accommodate changes in path_conv class.
(path_conv::fillin): Ditto.
(path_conv::return_and_clear_normalized_path): Eliminate.
(path_conv::set_normalized_path): New function.
(path_conv::path_conv): Set info in dev element. Use path_conv methods Check
for FH_FS rather than FH_BAD to indicate when to fill in filesystem stuff.
where appropriate rather than direct access. Use set_normalized_path to set
normalized path.
(windows_device_names): Eliminate.
(get_dev): Ditto.
(get_raw_device_number): Ditto.
(get_device_number): Ditto.
(win32_device_name): Call new device name parser to do most of the heavy
lifting.
(mount_info::conv_to_win32_path): Fill in dev field as appropriate.
(symlink_worker): Handle new device files.
(symlink_info::check): Ditto.
(symlink_info::parse_device): Define new function.
* path.h (executable_states): Move here from fhandler.h.
(fs_info): Rename variables to *_storage and create methods for accessing same.
(path_conv): Add dev element, remove devn and unit and adjust inline methods to
accommodate.
(set_normalized_path): Declare new function.
* pinfo.cc (_pinfo::commune_recv): Add broken support for handling fifos.
(_pinfo::commune_send): Ditto.
* pipe.cc (fhandler_pipe::close): check for existence of handle before closing
it.
(handler_pipe::create): Rename from make_pipe. Change arguments to accept
fhandler_pipe array. Accommodate fifos.
(pipe): Rework to deal with fhandler_pipe::create changes.
(_pipe): Ditto.
* select.cc: Use individual device_specific types throughout rather than
indexing with obsolete device number.
(set_bits): Use is_socket call rather than checking device number.
* shared_info.h (CURR_MOUNT_MAGIC): Update.
(conv_to_win32_path): Reflect addition of device argument.
* syscalls.cc (mknod_worker): New function.
(open): Use build_fh_name to build fhandler.
(chown_worker): Detect if this is an 'auto' device rather than an on-filesystem
device and handle appropriately.
(chmod_device): New function.
(chmod): Detect if this is an 'auto' device rather than an on-filesystem device
and handle appropriately. Use chmod_device to set mode of in-filesystem
devices.
(stat_worker): Eliminate path_conv argument. Call build_fh_name to construct
fhandler. Use fh->error() rather than pc->error to detect errors in fhandler
construction.
(access_worker): New function pulled from access. Accommodate in-filesystem
devices.
(access): Use access_worker.
(fpathconf): Detect if this is an 'auto' device rather than an on-filesystem
device and handle appropriately.
(mknod_worker): New function.
(mknod32): New function.
(chroot): Free normalized path -- assuming it was actually cmalloced.
* tty.cc (create_tty_master): Tweak for new device class.
(tty::common_init): Ditto.
* winsup.h (stat_worker): Remove.
(symlink_worker): Declare.
* exceptions.cc (set_process_mask): Just call sig_dispatch_pending and don't
worry about pending_signals since sig_dispatch_pending should always do the
right thing now.
(sig_handle): Reorganize SIGCONT handling to more closely conform to SUSv3.
* pinfo.h: Move __SIG enum to sigproc.h.
(PICOM_FIFO): New enum element.
(_pinfo): Remove 'thread2signal' stuff throughout class.
(_pinfo::commune_send): Make varargs.
(_pinfo::sigtodo): Eliminate.
(_pinfo::thread2signal): Ditto.
* signal.cc (kill_worker): Eliminate call to setthread2signal.
* sigproc.cc (local_sigtodo): Eliminate.
(getlocal_sigtodo): Ditto.
(sigelem): New class.
(pending_signals): New class.
(sigqueue): New variable, start of sigqueue linked list.
(sigcatch_nonmain): Eliminate.
(sigcatch_main): Eliminate.
(sigcatch_nosync): Eliminate.
(sigcomplete_nonmain): Eliminate.
(pending_signals): Eliminate.
(sig_clear): Call signal thread to clear pending signals, unless already in
signal thread.
(sigpending): Call signal thread to get pending signals.
(sig_dispatch_pending): Eliminate use of pending_signals and just check
sigqueue.
(sigproc_terminate): Eliminate all of the obsolete semaphore stuff. Close
signal pipe handle.
(sig_send): Eliminate all of the obsolete semaphore stuff and use pipe to send
signals.
(getevent): Eliminate.
(pending_signals::add): New function.
(pending_signals::del): New function.
(pending_signals::next): New function.
(wait_sig): Eliminate all of the obsolete semaphore stuff. Use pipe to
communicate and maintain a linked list of signals.
* sigproc.h: Move __SIG defines here. Add __SIGPENDING.
(sig_dispatch_pending): Remove "C" specifier.
(sig_handle): Accept a mask argument.
* thread.cc: Remove signal handling considerations throughout.
2003-09-25 02:37:18 +02:00
|
|
|
fhandler_dev_dsp::open (int flags, mode_t mode)
|
2001-04-16 05:27:16 +02:00
|
|
|
{
|
2004-08-17 11:52:50 +02:00
|
|
|
int err = 0;
|
|
|
|
UINT num_in = 0, num_out = 0;
|
2002-07-03 05:20:50 +02:00
|
|
|
set_flags ((flags & ~O_TEXT) | O_BINARY);
|
2004-03-23 12:05:56 +01:00
|
|
|
// Work out initial sample format & frequency, /dev/dsp defaults
|
|
|
|
audioformat_ = AFMT_U8;
|
2001-10-04 04:34:20 +02:00
|
|
|
audiofreq_ = 8000;
|
|
|
|
audiobits_ = 8;
|
|
|
|
audiochannels_ = 1;
|
2004-03-23 12:05:56 +01:00
|
|
|
switch (flags & O_ACCMODE)
|
2001-04-24 04:07:58 +02:00
|
|
|
{
|
2004-08-17 11:52:50 +02:00
|
|
|
case O_RDWR:
|
|
|
|
if ((num_in = waveInGetNumDevs ()) == 0)
|
|
|
|
err = ENXIO;
|
|
|
|
/* Fall through */
|
2004-03-23 12:05:56 +01:00
|
|
|
case O_WRONLY:
|
2004-08-17 11:52:50 +02:00
|
|
|
if ((num_out = waveOutGetNumDevs ()) == 0)
|
|
|
|
err = ENXIO;
|
2004-03-23 12:05:56 +01:00
|
|
|
break;
|
|
|
|
case O_RDONLY:
|
2004-08-17 11:52:50 +02:00
|
|
|
if ((num_in = waveInGetNumDevs ()) == 0)
|
|
|
|
err = ENXIO;
|
2004-03-23 12:05:56 +01:00
|
|
|
break;
|
|
|
|
default:
|
2004-08-17 11:52:50 +02:00
|
|
|
err = EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!err)
|
|
|
|
{
|
2001-04-24 04:07:58 +02:00
|
|
|
set_open_status ();
|
2004-04-10 15:45:10 +02:00
|
|
|
need_fork_fixup (true);
|
2004-08-17 11:52:50 +02:00
|
|
|
nohandle (true);
|
2001-04-24 04:07:58 +02:00
|
|
|
}
|
2004-03-23 12:05:56 +01:00
|
|
|
else
|
2004-08-17 11:52:50 +02:00
|
|
|
set_errno (err);
|
|
|
|
|
2013-04-23 11:44:36 +02:00
|
|
|
debug_printf ("ACCMODE=%y audio_in=%d audio_out=%d, err=%d",
|
2004-08-17 11:52:50 +02:00
|
|
|
flags & O_ACCMODE, num_in, num_out, err);
|
|
|
|
return !err;
|
2001-04-16 05:27:16 +02:00
|
|
|
}
|
|
|
|
|
2004-08-17 11:52:50 +02:00
|
|
|
#define IS_WRITE() ((get_flags() & O_ACCMODE) != O_RDONLY)
|
|
|
|
#define IS_READ() ((get_flags() & O_ACCMODE) != O_WRONLY)
|
2004-03-23 12:05:56 +01:00
|
|
|
|
2009-07-24 22:54:33 +02:00
|
|
|
ssize_t __stdcall
|
2001-04-16 05:27:16 +02:00
|
|
|
fhandler_dev_dsp::write (const void *ptr, size_t len)
|
|
|
|
{
|
2013-04-23 11:44:36 +02:00
|
|
|
debug_printf ("ptr=%p len=%ld", ptr, len);
|
2004-03-24 09:57:17 +01:00
|
|
|
int len_s = len;
|
|
|
|
const char *ptr_s = static_cast <const char *> (ptr);
|
|
|
|
|
2008-09-11 06:34:24 +02:00
|
|
|
if (audio_out_)
|
|
|
|
/* nothing to do */;
|
|
|
|
else if (IS_WRITE ())
|
|
|
|
{
|
|
|
|
debug_printf ("Allocating");
|
2011-05-04 13:41:22 +02:00
|
|
|
if (!(audio_out_ = new Audio_out (this)))
|
2004-08-17 11:52:50 +02:00
|
|
|
return -1;
|
2008-09-11 06:34:24 +02:00
|
|
|
|
|
|
|
/* check for wave file & get parameters & skip header if possible. */
|
|
|
|
|
|
|
|
if (audio_out_->parsewav (ptr_s, len_s,
|
|
|
|
audiofreq_, audiobits_, audiochannels_))
|
2013-04-23 11:44:36 +02:00
|
|
|
debug_printf ("=> ptr_s=%p len_s=%d", ptr_s, len_s);
|
2008-09-11 06:34:24 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
set_errno (EBADF); // device was opened for read?
|
|
|
|
return -1;
|
|
|
|
}
|
2004-08-17 11:52:50 +02:00
|
|
|
|
|
|
|
/* Open audio device properly with callbacks.
|
|
|
|
Private parameters were set in call to parsewav.
|
|
|
|
This is a no-op when there are successive writes in the same process */
|
|
|
|
if (!audio_out_->start ())
|
2001-04-16 05:27:16 +02:00
|
|
|
{
|
2004-08-17 11:52:50 +02:00
|
|
|
set_errno (EIO);
|
2004-03-23 12:05:56 +01:00
|
|
|
return -1;
|
|
|
|
}
|
2001-05-20 19:31:06 +02:00
|
|
|
|
2011-05-04 13:41:22 +02:00
|
|
|
int written = audio_out_->write (ptr_s, len_s);
|
|
|
|
if (written < 0)
|
|
|
|
{
|
|
|
|
if (len - len_s > 0)
|
|
|
|
return len - len_s;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return len - len_s + written;
|
2001-04-16 05:27:16 +02:00
|
|
|
}
|
|
|
|
|
2013-05-01 03:20:37 +02:00
|
|
|
void __reg3
|
2002-12-14 05:01:32 +01:00
|
|
|
fhandler_dev_dsp::read (void *ptr, size_t& len)
|
2001-04-16 05:27:16 +02:00
|
|
|
{
|
2013-04-23 11:44:36 +02:00
|
|
|
debug_printf ("ptr=%p len=%ld", ptr, len);
|
2004-08-17 11:52:50 +02:00
|
|
|
|
2008-09-11 06:34:24 +02:00
|
|
|
if (audio_in_)
|
|
|
|
/* nothing to do */;
|
|
|
|
else if (IS_READ ())
|
|
|
|
{
|
|
|
|
debug_printf ("Allocating");
|
2011-05-04 13:41:22 +02:00
|
|
|
if (!(audio_in_ = new Audio_in (this)))
|
2008-09-11 06:34:24 +02:00
|
|
|
{
|
|
|
|
len = (size_t)-1;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
audio_in_->setconvert (audioformat_);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
len = (size_t)-1;
|
|
|
|
set_errno (EBADF); // device was opened for write?
|
|
|
|
return;
|
|
|
|
}
|
2004-08-17 11:52:50 +02:00
|
|
|
|
|
|
|
/* Open audio device properly with callbacks.
|
|
|
|
This is a noop when there are successive reads in the same process */
|
|
|
|
if (!audio_in_->start (audiofreq_, audiobits_, audiochannels_))
|
2004-03-23 12:05:56 +01:00
|
|
|
{
|
|
|
|
len = (size_t)-1;
|
2004-08-17 11:52:50 +02:00
|
|
|
set_errno (EIO);
|
2004-03-23 12:05:56 +01:00
|
|
|
return;
|
|
|
|
}
|
2004-08-17 11:52:50 +02:00
|
|
|
|
2004-03-23 12:05:56 +01:00
|
|
|
audio_in_->read ((char *)ptr, (int&)len);
|
2001-04-16 05:27:16 +02:00
|
|
|
}
|
|
|
|
|
2013-04-23 11:44:36 +02:00
|
|
|
off_t
|
|
|
|
fhandler_dev_dsp::lseek (off_t offset, int whence)
|
2001-04-16 05:27:16 +02:00
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-08-17 11:52:50 +02:00
|
|
|
void
|
|
|
|
fhandler_dev_dsp::close_audio_in ()
|
2001-04-16 05:27:16 +02:00
|
|
|
{
|
2004-03-23 12:05:56 +01:00
|
|
|
if (audio_in_)
|
|
|
|
{
|
2004-08-17 11:52:50 +02:00
|
|
|
audio_in_->stop ();
|
2004-03-23 12:05:56 +01:00
|
|
|
delete audio_in_;
|
|
|
|
audio_in_ = NULL;
|
|
|
|
}
|
2004-08-17 11:52:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
fhandler_dev_dsp::close_audio_out (bool immediately)
|
|
|
|
{
|
2004-03-23 12:05:56 +01:00
|
|
|
if (audio_out_)
|
|
|
|
{
|
2004-08-17 11:52:50 +02:00
|
|
|
audio_out_->stop (immediately);
|
2004-03-23 12:05:56 +01:00
|
|
|
delete audio_out_;
|
|
|
|
audio_out_ = NULL;
|
|
|
|
}
|
2004-08-17 11:52:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2005-07-05 05:16:46 +02:00
|
|
|
fhandler_dev_dsp::close ()
|
2004-08-17 11:52:50 +02:00
|
|
|
{
|
2013-04-23 11:44:36 +02:00
|
|
|
debug_printf ("audio_in=%p audio_out=%p", audio_in_, audio_out_);
|
2011-05-06 00:30:53 +02:00
|
|
|
close_audio_in ();
|
|
|
|
close_audio_out (exit_state != ES_NOT_EXITING);
|
2001-04-16 05:27:16 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2001-05-20 19:31:06 +02:00
|
|
|
int
|
2011-07-21 22:21:46 +02:00
|
|
|
fhandler_dev_dsp::ioctl (unsigned int cmd, void *buf)
|
2001-04-16 05:27:16 +02:00
|
|
|
{
|
2013-04-23 11:44:36 +02:00
|
|
|
debug_printf ("audio_in=%p audio_out=%p", audio_in_, audio_out_);
|
2011-07-21 22:21:46 +02:00
|
|
|
int *intbuf = (int *) buf;
|
2001-04-16 05:27:16 +02:00
|
|
|
switch (cmd)
|
|
|
|
{
|
2004-03-23 12:05:56 +01:00
|
|
|
#define CASE(a) case a : debug_printf ("/dev/dsp: ioctl %s", #a);
|
2001-04-16 05:27:16 +02:00
|
|
|
|
2001-05-20 19:31:06 +02:00
|
|
|
CASE (SNDCTL_DSP_RESET)
|
2004-08-17 11:52:50 +02:00
|
|
|
close_audio_in ();
|
|
|
|
close_audio_out (true);
|
2004-09-12 05:47:57 +02:00
|
|
|
return 0;
|
2004-03-23 12:05:56 +01:00
|
|
|
break;
|
2001-04-16 05:27:16 +02:00
|
|
|
|
2001-05-20 19:31:06 +02:00
|
|
|
CASE (SNDCTL_DSP_GETBLKSIZE)
|
2004-08-17 11:52:50 +02:00
|
|
|
/* This is valid even if audio_X is NULL */
|
|
|
|
if (IS_WRITE ())
|
2004-03-23 12:05:56 +01:00
|
|
|
{
|
2011-07-21 22:21:46 +02:00
|
|
|
*intbuf = audio_out_->blockSize (audiofreq_,
|
2004-03-23 12:05:56 +01:00
|
|
|
audiobits_,
|
|
|
|
audiochannels_);
|
|
|
|
}
|
|
|
|
else
|
2004-08-17 11:52:50 +02:00
|
|
|
{ // I am very sure that IS_READ is valid
|
2011-07-21 22:21:46 +02:00
|
|
|
*intbuf = audio_in_->blockSize (audiofreq_,
|
2004-03-23 12:05:56 +01:00
|
|
|
audiobits_,
|
|
|
|
audiochannels_);
|
|
|
|
}
|
2001-11-05 07:09:15 +01:00
|
|
|
return 0;
|
2001-04-16 05:27:16 +02:00
|
|
|
|
2001-05-20 19:31:06 +02:00
|
|
|
CASE (SNDCTL_DSP_SETFMT)
|
|
|
|
{
|
2004-03-23 12:05:56 +01:00
|
|
|
int nBits;
|
2011-07-21 22:21:46 +02:00
|
|
|
switch (*intbuf)
|
2004-03-23 12:05:56 +01:00
|
|
|
{
|
|
|
|
case AFMT_QUERY:
|
2011-07-21 22:21:46 +02:00
|
|
|
*intbuf = audioformat_;
|
2004-03-23 12:05:56 +01:00
|
|
|
return 0;
|
|
|
|
break;
|
|
|
|
case AFMT_U16_BE:
|
|
|
|
case AFMT_U16_LE:
|
|
|
|
case AFMT_S16_BE:
|
|
|
|
case AFMT_S16_LE:
|
|
|
|
nBits = 16;
|
|
|
|
break;
|
|
|
|
case AFMT_U8:
|
|
|
|
case AFMT_S8:
|
|
|
|
nBits = 8;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
nBits = 0;
|
|
|
|
}
|
2004-08-17 11:52:50 +02:00
|
|
|
if (nBits && IS_WRITE ())
|
2001-05-20 19:31:06 +02:00
|
|
|
{
|
2004-08-17 11:52:50 +02:00
|
|
|
close_audio_out ();
|
2004-03-23 12:05:56 +01:00
|
|
|
if (audio_out_->query (audiofreq_, nBits, audiochannels_))
|
2001-05-20 19:31:06 +02:00
|
|
|
{
|
|
|
|
audiobits_ = nBits;
|
2011-07-21 22:21:46 +02:00
|
|
|
audioformat_ = *intbuf;
|
2001-05-20 19:31:06 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-07-21 22:21:46 +02:00
|
|
|
*intbuf = audiobits_;
|
2001-05-20 19:31:06 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
2004-08-17 11:52:50 +02:00
|
|
|
if (nBits && IS_READ ())
|
2004-03-23 12:05:56 +01:00
|
|
|
{
|
2004-08-17 11:52:50 +02:00
|
|
|
close_audio_in ();
|
2004-03-23 12:05:56 +01:00
|
|
|
if (audio_in_->query (audiofreq_, nBits, audiochannels_))
|
|
|
|
{
|
|
|
|
audiobits_ = nBits;
|
2011-07-21 22:21:46 +02:00
|
|
|
audioformat_ = *intbuf;
|
2004-03-23 12:05:56 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-07-21 22:21:46 +02:00
|
|
|
*intbuf = audiobits_;
|
2004-03-23 12:05:56 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
2001-05-20 19:31:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
CASE (SNDCTL_DSP_SPEED)
|
2004-08-17 11:52:50 +02:00
|
|
|
if (IS_WRITE ())
|
2004-05-28 21:50:07 +02:00
|
|
|
{
|
2004-08-17 11:52:50 +02:00
|
|
|
close_audio_out ();
|
2011-07-21 22:21:46 +02:00
|
|
|
if (audio_out_->query (*intbuf, audiobits_, audiochannels_))
|
|
|
|
audiofreq_ = *intbuf;
|
2004-03-23 12:05:56 +01:00
|
|
|
else
|
|
|
|
{
|
2011-07-21 22:21:46 +02:00
|
|
|
*intbuf = audiofreq_;
|
2004-03-23 12:05:56 +01:00
|
|
|
return -1;
|
|
|
|
}
|
2001-05-20 19:31:06 +02:00
|
|
|
}
|
2004-08-17 11:52:50 +02:00
|
|
|
if (IS_READ ())
|
2001-05-20 19:31:06 +02:00
|
|
|
{
|
2004-08-17 11:52:50 +02:00
|
|
|
close_audio_in ();
|
2011-07-21 22:21:46 +02:00
|
|
|
if (audio_in_->query (*intbuf, audiobits_, audiochannels_))
|
|
|
|
audiofreq_ = *intbuf;
|
2004-03-23 12:05:56 +01:00
|
|
|
else
|
|
|
|
{
|
2011-07-21 22:21:46 +02:00
|
|
|
*intbuf = audiofreq_;
|
2004-03-23 12:05:56 +01:00
|
|
|
return -1;
|
|
|
|
}
|
2001-05-20 19:31:06 +02:00
|
|
|
}
|
2004-03-23 12:05:56 +01:00
|
|
|
return 0;
|
2001-05-20 19:31:06 +02:00
|
|
|
|
|
|
|
CASE (SNDCTL_DSP_STEREO)
|
|
|
|
{
|
2011-07-21 22:21:46 +02:00
|
|
|
int nChannels = *intbuf + 1;
|
2004-08-17 11:52:50 +02:00
|
|
|
int res = ioctl (SNDCTL_DSP_CHANNELS, &nChannels);
|
2011-07-21 22:21:46 +02:00
|
|
|
*intbuf = nChannels - 1;
|
2004-08-17 11:52:50 +02:00
|
|
|
return res;
|
2001-05-20 19:31:06 +02:00
|
|
|
}
|
|
|
|
|
2004-04-05 10:30:41 +02:00
|
|
|
CASE (SNDCTL_DSP_CHANNELS)
|
|
|
|
{
|
2011-07-21 22:21:46 +02:00
|
|
|
int nChannels = *intbuf;
|
2004-04-05 10:30:41 +02:00
|
|
|
|
2004-08-17 11:52:50 +02:00
|
|
|
if (IS_WRITE ())
|
2004-05-28 21:50:07 +02:00
|
|
|
{
|
2004-08-17 11:52:50 +02:00
|
|
|
close_audio_out ();
|
2004-04-05 10:30:41 +02:00
|
|
|
if (audio_out_->query (audiofreq_, audiobits_, nChannels))
|
|
|
|
audiochannels_ = nChannels;
|
|
|
|
else
|
|
|
|
{
|
2011-07-21 22:21:46 +02:00
|
|
|
*intbuf = audiochannels_;
|
2004-04-05 10:30:41 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
2004-08-17 11:52:50 +02:00
|
|
|
if (IS_READ ())
|
2004-04-05 10:30:41 +02:00
|
|
|
{
|
2004-08-17 11:52:50 +02:00
|
|
|
close_audio_in ();
|
2004-04-05 10:30:41 +02:00
|
|
|
if (audio_in_->query (audiofreq_, audiobits_, nChannels))
|
|
|
|
audiochannels_ = nChannels;
|
|
|
|
else
|
|
|
|
{
|
2011-07-21 22:21:46 +02:00
|
|
|
*intbuf = audiochannels_;
|
2004-04-05 10:30:41 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2001-05-20 19:31:06 +02:00
|
|
|
CASE (SNDCTL_DSP_GETOSPACE)
|
|
|
|
{
|
2004-08-17 11:52:50 +02:00
|
|
|
if (!IS_WRITE ())
|
2004-03-23 12:05:56 +01:00
|
|
|
{
|
2004-08-17 11:52:50 +02:00
|
|
|
set_errno(EBADF);
|
|
|
|
return -1;
|
2004-03-23 12:05:56 +01:00
|
|
|
}
|
2011-07-21 22:21:46 +02:00
|
|
|
audio_buf_info *p = (audio_buf_info *) buf;
|
2004-08-17 11:52:50 +02:00
|
|
|
audio_out_->buf_info (p, audiofreq_, audiobits_, audiochannels_);
|
2011-07-21 22:21:46 +02:00
|
|
|
debug_printf ("buf=%p frags=%d fragsize=%d bytes=%d",
|
|
|
|
buf, p->fragments, p->fragsize, p->bytes);
|
2001-06-29 04:20:01 +02:00
|
|
|
return 0;
|
2001-05-20 19:31:06 +02:00
|
|
|
}
|
|
|
|
|
2004-04-13 11:38:32 +02:00
|
|
|
CASE (SNDCTL_DSP_GETISPACE)
|
|
|
|
{
|
2004-08-17 11:52:50 +02:00
|
|
|
if (!IS_READ ())
|
2004-04-13 11:38:32 +02:00
|
|
|
{
|
2004-08-17 11:52:50 +02:00
|
|
|
set_errno(EBADF);
|
|
|
|
return -1;
|
2004-04-13 11:38:32 +02:00
|
|
|
}
|
2011-07-21 22:21:46 +02:00
|
|
|
audio_buf_info *p = (audio_buf_info *) buf;
|
2004-08-17 11:52:50 +02:00
|
|
|
audio_in_->buf_info (p, audiofreq_, audiobits_, audiochannels_);
|
2011-07-21 22:21:46 +02:00
|
|
|
debug_printf ("buf=%p frags=%d fragsize=%d bytes=%d",
|
|
|
|
buf, p->fragments, p->fragsize, p->bytes);
|
2004-04-13 11:38:32 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2001-05-20 19:31:06 +02:00
|
|
|
CASE (SNDCTL_DSP_SETFRAGMENT)
|
|
|
|
// Fake!! esound & mikmod require this on non PowerPC platforms.
|
|
|
|
//
|
2001-06-29 04:20:01 +02:00
|
|
|
return 0;
|
2001-05-20 19:31:06 +02:00
|
|
|
|
2003-01-14 03:08:35 +01:00
|
|
|
CASE (SNDCTL_DSP_GETFMTS)
|
2011-07-21 22:21:46 +02:00
|
|
|
*intbuf = AFMT_S16_LE | AFMT_U8; // only native formats returned here
|
2004-04-05 10:30:41 +02:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
CASE (SNDCTL_DSP_GETCAPS)
|
2011-07-21 22:21:46 +02:00
|
|
|
*intbuf = DSP_CAP_BATCH | DSP_CAP_DUPLEX;
|
2004-03-23 12:05:56 +01:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
CASE (SNDCTL_DSP_POST)
|
|
|
|
CASE (SNDCTL_DSP_SYNC)
|
2004-08-17 11:52:50 +02:00
|
|
|
// Stop audio out device
|
|
|
|
close_audio_out ();
|
2004-09-12 05:47:57 +02:00
|
|
|
// Stop audio in device
|
|
|
|
close_audio_in ();
|
2003-01-14 03:08:35 +01:00
|
|
|
return 0;
|
|
|
|
|
2001-05-20 19:31:06 +02:00
|
|
|
default:
|
2011-07-21 22:21:46 +02:00
|
|
|
return fhandler_base::ioctl (cmd, buf);
|
2001-05-20 19:31:06 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
#undef CASE
|
2011-07-21 22:21:46 +02:00
|
|
|
}
|
2001-04-16 05:27:16 +02:00
|
|
|
}
|
|
|
|
|
2004-03-23 12:05:56 +01:00
|
|
|
void
|
|
|
|
fhandler_dev_dsp::fixup_after_fork (HANDLE parent)
|
|
|
|
{ // called from new child process
|
2013-04-23 11:44:36 +02:00
|
|
|
debug_printf ("audio_in=%p audio_out=%p",
|
|
|
|
audio_in_, audio_out_);
|
2004-08-17 11:52:50 +02:00
|
|
|
|
|
|
|
if (audio_in_)
|
2011-05-06 00:30:53 +02:00
|
|
|
audio_in_->fork_fixup (parent);
|
2004-03-23 12:05:56 +01:00
|
|
|
if (audio_out_)
|
|
|
|
audio_out_->fork_fixup (parent);
|
2001-04-16 05:27:16 +02:00
|
|
|
}
|
2001-05-24 07:20:17 +02:00
|
|
|
|
|
|
|
void
|
2004-02-02 22:00:07 +01:00
|
|
|
fhandler_dev_dsp::fixup_after_exec ()
|
2001-05-24 07:20:17 +02:00
|
|
|
{
|
2013-04-23 11:44:36 +02:00
|
|
|
debug_printf ("audio_in=%p audio_out=%p, close_on_exec %d",
|
|
|
|
audio_in_, audio_out_, close_on_exec ());
|
2005-11-14 05:28:45 +01:00
|
|
|
if (!close_on_exec ())
|
|
|
|
{
|
|
|
|
audio_in_ = NULL;
|
|
|
|
audio_out_ = NULL;
|
|
|
|
}
|
2004-08-17 11:52:50 +02:00
|
|
|
}
|