1 // --------------------------------------------------------------------------
5 // Author: Guilhem Lavaux <lavaux@easynet.fr> (C) 1999, 2000
7 // --------------------------------------------------------------------------
9 #pragma implementation "sndwin.cpp"
12 #include "wx/wxprec.h"
19 #include "wx/string.h"
22 #include "wx/module.h"
23 #include "wx/msw/private.h"
25 // -------------------------------------------------------------------------
27 // -------------------------------------------------------------------------
29 #include "wx/mmedia/sndbase.h"
30 #include "wx/mmedia/sndwin.h"
31 #include "wx/mmedia/sndpcm.h"
33 // -------------------------------------------------------------------------
35 // -------------------------------------------------------------------------
40 // -------------------------------------------------------------------------
41 // External definitions, forward, ...
42 // -------------------------------------------------------------------------
44 typedef struct _wxSoundInternal wxSoundInternal
;
45 typedef struct _wxSoundInfoHeader wxSoundInfoHeader
;
47 extern const wxChar
*wxCanvasClassName
;
49 wxList
*wxSoundHandleList
= NULL
;
51 static inline wxSoundStreamWin
*wxFindSoundFromHandle(WXHWND hWnd
)
53 wxNode
*node
= wxSoundHandleList
->Find((long)hWnd
);
56 return (wxSoundStreamWin
*)node
->Data();
59 struct _wxSoundInternal
{
63 bool m_output_enabled
, m_input_enabled
;
66 struct _wxSoundInfoHeader
{
67 HGLOBAL m_h_header
, m_h_data
;
71 bool m_playing
, m_recording
;
72 wxUint32 m_position
, m_size
;
74 wxSoundStreamWin
*m_driver
;
77 #define WXSOUND_MAX_QUEUE 10
79 wxSoundStreamWin::wxSoundStreamWin()
83 m_production_started
= FALSE
;
84 m_internal
= new wxSoundInternal
;
86 m_snderror
= wxSOUND_MEMERROR
;
90 m_snderror
= wxSOUND_NOERROR
;
96 m_internal
->m_input_enabled
= FALSE
;
97 m_internal
->m_output_enabled
= FALSE
;
99 m_waiting_for
= FALSE
;
101 if (!OpenDevice(wxSOUND_OUTPUT
)) {
102 m_snderror
= wxSOUND_NOERROR
; //next call to OpenDevice won't do this
103 if (!OpenDevice(wxSOUND_INPUT
))
110 wxSoundStreamWin::~wxSoundStreamWin()
113 if (m_production_started
)
121 // -----------------------------------------------------------------------
122 // _wxSoundHandlerWndProc: Window callback to handle buffer completion
123 // -----------------------------------------------------------------------
124 LRESULT APIENTRY _EXPORT
126 _wxSoundHandlerWndProc(HWND hWnd
, UINT message
,
127 WPARAM wParam
, LPARAM lParam
)
129 wxSoundStreamWin
*sndwin
;
131 sndwin
= wxFindSoundFromHandle((WXHWND
)hWnd
);
137 sndwin
->NotifyDoneBuffer(wParam
, wxSOUND_OUTPUT
);
140 sndwin
->NotifyDoneBuffer(wParam
, wxSOUND_INPUT
);
148 // -----------------------------------------------------------------------
149 // CreateSndWindow() creates an hidden window which will receive the sound
151 // -----------------------------------------------------------------------
153 void wxSoundStreamWin::CreateSndWindow()
155 FARPROC proc
= MakeProcInstance((FARPROC
)_wxSoundHandlerWndProc
,
159 // NB: class name must be kept in sync with wxCanvasClassName in
161 m_internal
->m_sndWin
= ::CreateWindow(wxT("wxWindowClass"), NULL
, 0,
162 0, 0, 0, 0, NULL
, (HMENU
) NULL
,
163 wxGetInstance(), NULL
);
165 error
= GetLastError();
167 ::SetWindowLong(m_internal
->m_sndWin
, GWL_WNDPROC
, (LONG
)proc
);
169 // Add this window to the sound handle list so we'll be able to redecode
170 // the "magic" number.
171 wxSoundHandleList
->Append((long)m_internal
->m_sndWin
, (wxObject
*)this);
174 // -----------------------------------------------------------------------
175 // DestroySndWindow() destroys the hidden window
176 // -----------------------------------------------------------------------
178 void wxSoundStreamWin::DestroySndWindow()
180 if (m_internal
->m_sndWin
) {
181 ::DestroyWindow(m_internal
->m_sndWin
);
182 wxSoundHandleList
->DeleteObject((wxObject
*)this);
186 // -------------------------------------------------------------------------
187 // OpenDevice(int mode) initializes the windows driver for a "mode"
188 // operation. mode is a bit mask: if the bit "wxSOUND_OUTPUT" is set,
189 // the driver is opened for output operation, and if the bit "wxSOUND_INPUT"
190 // is set, then the driver is opened for input operation. The two modes
192 // The initialization parameters (sample rate, ...) are taken from the
193 // m_sndformat object.
194 // At the end, OpenDevice() calls AllocHeaders() to initialize the Sound IO
196 // -------------------------------------------------------------------------
197 bool wxSoundStreamWin::OpenDevice(int mode
)
199 wxSoundFormatPcm
*pcm
;
200 WAVEFORMATEX wformat
;
203 m_snderror
= wxSOUND_INVFRMT
;
207 pcm
= (wxSoundFormatPcm
*)m_sndformat
;
209 wformat
.wFormatTag
= WAVE_FORMAT_PCM
;
210 wformat
.nChannels
= pcm
->GetChannels();
211 wformat
.nBlockAlign
= wformat
.nChannels
* pcm
->GetBPS() / 8;
212 wformat
.nSamplesPerSec
= pcm
->GetSampleRate();
213 wformat
.nAvgBytesPerSec
= wformat
.nSamplesPerSec
* wformat
.nBlockAlign
;
214 wformat
.wBitsPerSample
= pcm
->GetBPS();
217 // -----------------------------------
218 // Open the driver for Output operation
219 // -----------------------------------
220 if (mode
& wxSOUND_OUTPUT
) {
223 result
= waveOutOpen(&m_internal
->m_devout
,
224 WAVE_MAPPER
, &wformat
,
225 (DWORD
)m_internal
->m_sndWin
, 0,
228 if (result
!= MMSYSERR_NOERROR
) {
229 m_snderror
= wxSOUND_INVDEV
;
233 m_output_frag_out
= WXSOUND_MAX_QUEUE
-1;
234 m_current_frag_out
= 0;
236 m_internal
->m_output_enabled
= TRUE
;
238 // -----------------------------------
239 // Open the driver for Input operation
240 // -----------------------------------
241 if (mode
& wxSOUND_INPUT
) {
244 result
= waveInOpen(&m_internal
->m_devin
,
245 WAVE_MAPPER
, &wformat
,
246 (DWORD
)m_internal
->m_sndWin
, 0,
249 if (result
!= MMSYSERR_NOERROR
) {
250 m_snderror
= wxSOUND_INVDEV
;
254 m_current_frag_in
= WXSOUND_MAX_QUEUE
-1;
257 m_internal
->m_input_enabled
= TRUE
;
260 if (mode
& wxSOUND_OUTPUT
) {
261 if (!AllocHeaders(wxSOUND_OUTPUT
)) {
266 if (mode
& wxSOUND_INPUT
) {
267 if (!AllocHeaders(wxSOUND_INPUT
)) {
276 // -------------------------------------------------------------------------
277 // CloseDevice() closes the driver handles and frees memory allocated for
279 // -------------------------------------------------------------------------
280 void wxSoundStreamWin::CloseDevice()
282 if (m_internal
->m_output_enabled
) {
283 FreeHeaders(wxSOUND_OUTPUT
);
284 m_internal
->m_output_enabled
= FALSE
;
285 waveOutClose(m_internal
->m_devout
);
288 if (m_internal
->m_input_enabled
) {
289 FreeHeaders(wxSOUND_INPUT
);
290 m_internal
->m_input_enabled
= FALSE
;
291 waveInClose(m_internal
->m_devin
);
295 // -------------------------------------------------------------------------
296 // AllocHeader(int mode)
298 // mode has the same mean as in OpenDevice() except that here the two flags
299 // must be exclusive.
300 // AllocHeader() initializes an element of an operation (this can be input
301 // or output). It means it allocates the sound header's memory block
302 // and "prepares" it (It is needed by Windows). At the same time, it sets
303 // private datas so we can the header's owner (See callback).
305 // It returns the new allocated block or NULL.
306 // -------------------------------------------------------------------------
307 wxSoundInfoHeader
*wxSoundStreamWin::AllocHeader(int mode
)
309 wxSoundInfoHeader
*info
;
312 // Some memory allocation
313 info
= new wxSoundInfoHeader
;
314 info
->m_h_data
= GlobalAlloc(GMEM_MOVEABLE
| GMEM_SHARE
, GetBestSize());
315 info
->m_h_header
= GlobalAlloc(GMEM_MOVEABLE
| GMEM_SHARE
, sizeof(WAVEHDR
));
316 if (!info
->m_h_data
|| !info
->m_h_header
) {
318 m_snderror
= wxSOUND_MEMERROR
;
322 // Get the two pointers from the system
323 info
->m_data
= (char *)GlobalLock(info
->m_h_data
);
324 info
->m_header
= (WAVEHDR
*)GlobalLock(info
->m_h_header
);
325 // Set the header's mode
327 // Set the parent of the header
328 info
->m_driver
= this;
332 header
= info
->m_header
;
333 // Initialize Windows variables
334 header
->lpData
= info
->m_data
;
335 header
->dwBufferLength
= GetBestSize();
336 header
->dwUser
= (DWORD
)info
;
337 header
->dwFlags
= WHDR_DONE
;
339 // "Prepare" the header
340 if (mode
== wxSOUND_INPUT
) {
343 result
= waveInPrepareHeader(m_internal
->m_devin
, header
,
346 if (result
!= MMSYSERR_NOERROR
) {
347 // If something goes wrong, free everything.
348 GlobalUnlock(info
->m_data
);
349 GlobalUnlock(info
->m_header
);
350 GlobalFree(info
->m_h_data
);
351 GlobalFree(info
->m_h_header
);
354 m_snderror
= wxSOUND_IOERROR
;
357 } else if (mode
== wxSOUND_OUTPUT
) {
360 result
= waveOutPrepareHeader(m_internal
->m_devout
, header
,
363 if (result
!= MMSYSERR_NOERROR
) {
364 // If something goes wrong, free everything.
365 GlobalUnlock(info
->m_data
);
366 GlobalUnlock(info
->m_header
);
367 GlobalFree(info
->m_h_data
);
368 GlobalFree(info
->m_h_header
);
371 m_snderror
= wxSOUND_IOERROR
;
378 // -------------------------------------------------------------------------
379 // AllocHeaders(int mode)
381 // "mode" has the same mean as for OpenDevice() except that the two flags must
383 // AllocHeaders() allocates WXSOUND_MAX_QUEUE (= 128) blocks for an operation
384 // queue. It uses AllocHeader() for each element.
386 // Once it has allocated all blocks, it returns TRUE and if an error occured
388 // -------------------------------------------------------------------------
389 bool wxSoundStreamWin::AllocHeaders(int mode
)
392 wxSoundInfoHeader
**headers
;
394 if (mode
== wxSOUND_OUTPUT
)
395 headers
= m_headers_play
= new wxSoundInfoHeader
*[WXSOUND_MAX_QUEUE
];
397 headers
= m_headers_rec
= new wxSoundInfoHeader
*[WXSOUND_MAX_QUEUE
];
399 memset(headers
, 0, WXSOUND_MAX_QUEUE
*sizeof(wxSoundInfoHeader
*));
401 for (i
=0;i
<WXSOUND_MAX_QUEUE
;i
++) {
402 headers
[i
] = AllocHeader(mode
);
411 // -------------------------------------------------------------------------
412 // FreeHeader(int mode)
414 // "mode" has the same mean as for OpenDevice() except that the two flags must
416 // FreeHeader() frees a memory block and "unprepares" it.
417 // -------------------------------------------------------------------------
418 void wxSoundStreamWin::FreeHeader(wxSoundInfoHeader
*header
, int mode
)
420 if (mode
== wxSOUND_OUTPUT
)
421 waveOutUnprepareHeader(m_internal
->m_devout
, header
->m_header
, sizeof(WAVEHDR
));
423 waveInUnprepareHeader(m_internal
->m_devin
, header
->m_header
, sizeof(WAVEHDR
));
425 GlobalUnlock(header
->m_data
);
426 GlobalUnlock(header
->m_header
);
427 GlobalFree(header
->m_h_header
);
428 GlobalFree(header
->m_h_data
);
432 // -------------------------------------------------------------------------
433 // FreeHeaders(int mode)
435 // "mode" has the same mean as for OpenDevice() except that the two flags must
437 // FreeHeaders() frees all an operation queue once it has checked that
438 // all buffers have been terminated.
439 // -------------------------------------------------------------------------
440 void wxSoundStreamWin::FreeHeaders(int mode
)
443 wxSoundInfoHeader
***headers
;
445 if (mode
== wxSOUND_OUTPUT
)
446 headers
= &m_headers_play
;
448 headers
= &m_headers_rec
;
450 for (i
=0;i
<WXSOUND_MAX_QUEUE
;i
++) {
452 // We wait for the end of the buffer
453 WaitFor((*headers
)[i
]);
454 // Then, we free the header
455 FreeHeader((*headers
)[i
], mode
);
462 // -------------------------------------------------------------------------
463 // WaitFor(wxSoundInfoHeader *info)
465 // "info" is one element of an IO queue
466 // WaitFor() checks whether the specified block has been terminated.
467 // If it hasn't been terminated, it waits for its termination.
469 // NB: if it's a partially filled buffer it adds it to the Windows queue
470 // -------------------------------------------------------------------------
471 void wxSoundStreamWin::WaitFor(wxSoundInfoHeader
*info
)
473 // If the buffer is finished, we return immediately
474 if (!info
->m_playing
) {
476 // We begun filling it: we must send it to the Windows queue
477 if (info
->m_position
!= 0) {
478 memset(info
->m_data
+ info
->m_position
, 0, info
->m_size
);
487 m_waiting_for
= TRUE
;
488 // Else, we wait for its termination
489 while (info
->m_playing
|| info
->m_recording
)
491 m_waiting_for
= FALSE
;
494 // -------------------------------------------------------------------------
495 // AddToQueue(wxSoundInfoHeader *info)
497 // For "info", see WaitFor()
498 // AddToQueue() sends the IO queue element to the Windows queue.
500 // Warning: in the current implementation, it partially assume we send the
501 // element in the right order. This is true in that implementation but if
502 // you use it elsewhere, be careful: it may shuffle all your sound datas.
503 // -------------------------------------------------------------------------
504 bool wxSoundStreamWin::AddToQueue(wxSoundInfoHeader
*info
)
508 if (info
->m_mode
== wxSOUND_INPUT
) {
509 // Increment the input fragment pointer
510 result
= waveInAddBuffer(m_internal
->m_devin
,
511 info
->m_header
, sizeof(WAVEHDR
));
512 if (result
== MMSYSERR_NOERROR
)
513 info
->m_recording
= TRUE
;
516 } else if (info
->m_mode
== wxSOUND_OUTPUT
) {
517 result
= waveOutWrite(m_internal
->m_devout
,
518 info
->m_header
, sizeof(WAVEHDR
));
519 if (result
== MMSYSERR_NOERROR
)
520 info
->m_playing
= TRUE
;
527 // -------------------------------------------------------------------------
528 // ClearHeader(wxSoundInfoHeader *info)
530 // ClearHeader() reinitializes the parameters of "info" to their default
532 // -------------------------------------------------------------------------
533 void wxSoundStreamWin::ClearHeader(wxSoundInfoHeader
*info
)
535 info
->m_playing
= FALSE
;
536 info
->m_recording
= FALSE
;
537 info
->m_position
= 0;
538 info
->m_size
= GetBestSize();
541 // -------------------------------------------------------------------------
542 // wxSoundInfoHeader *NextFragmentOutput()
544 // NextFragmentOutput() looks for a free output block. It will always
545 // return you a non-NULL pointer but it may waits for an empty buffer a long
547 // -------------------------------------------------------------------------
548 wxSoundInfoHeader
*wxSoundStreamWin::NextFragmentOutput()
550 if (m_headers_play
[m_current_frag_out
]->m_playing
) {
551 m_current_frag_out
= (m_current_frag_out
+ 1) % WXSOUND_MAX_QUEUE
;
553 if (m_headers_play
[m_current_frag_out
]->m_playing
)
554 WaitFor(m_headers_play
[m_current_frag_out
]);
556 if (m_current_frag_out
== m_output_frag_out
)
557 m_queue_filled
= TRUE
;
558 return m_headers_play
[m_current_frag_out
];
561 // -------------------------------------------------------------------------
562 // The behaviour of Write is documented in the global documentation.
563 // -------------------------------------------------------------------------
564 wxSoundStream
& wxSoundStreamWin::Write(const void *buffer
, wxUint32 len
)
567 if (!m_internal
->m_output_enabled
) {
568 m_snderror
= wxSOUND_NOTSTARTED
;
574 wxSoundInfoHeader
*header
;
577 // Get a new output fragment
578 header
= NextFragmentOutput();
580 to_copy
= (len
> header
->m_size
) ? header
->m_size
: len
;
581 memcpy(header
->m_data
+ header
->m_position
, buffer
, to_copy
);
583 header
->m_position
+= to_copy
;
584 header
->m_size
-= to_copy
;
585 buffer
= (((const char *)buffer
) + to_copy
);
587 m_lastcount
+= to_copy
;
589 // If the fragment is full, we send it to the Windows queue.
590 if (header
->m_size
== 0)
591 if (!AddToQueue(header
)) {
592 m_snderror
= wxSOUND_IOERROR
;
599 // -------------------------------------------------------------------------
600 // NextFragmentInput is not functional.
601 // -------------------------------------------------------------------------
602 wxSoundInfoHeader
*wxSoundStreamWin::NextFragmentInput()
604 wxSoundInfoHeader
*header
;
606 // Queue pointer: reader
607 m_current_frag_in
= (m_current_frag_in
+ 1) % WXSOUND_MAX_QUEUE
;
609 header
= m_headers_rec
[m_current_frag_in
];
610 // If the current buffer is in recording mode, we must wait for its
612 if (header
->m_recording
)
615 // We reached the writer position: the queue is full.
616 if (m_current_frag_in
== m_input_frag_in
)
617 m_queue_filled
= TRUE
;
622 // -------------------------------------------------------------------------
623 // The behaviour of Read is documented in the global documentation.
624 // -------------------------------------------------------------------------
625 wxSoundStream
& wxSoundStreamWin::Read(void *buffer
, wxUint32 len
)
627 wxSoundInfoHeader
*header
;
631 if (!m_internal
->m_input_enabled
)
635 header
= NextFragmentInput();
637 to_copy
= (len
> header
->m_size
) ? header
->m_size
: len
;
638 memcpy(buffer
, header
->m_data
+ header
->m_position
, to_copy
);
640 header
->m_position
+= to_copy
;
641 header
->m_size
-= to_copy
;
642 buffer
= (((char *)buffer
) + to_copy
);
644 m_lastcount
+= to_copy
;
646 if (header
->m_size
== 0) {
648 if (!AddToQueue(header
)) {
649 m_snderror
= wxSOUND_IOERROR
;
657 // -------------------------------------------------------------------------
658 // NotifyDoneBuffer(wxUint32 dev_handle)
660 // NotifyDoneBuffer() is called by wxSoundHandlerProc each time a sound
661 // fragment finished. It reinitializes the parameters of the fragment and
662 // sends an event to the clients.
663 // -------------------------------------------------------------------------
664 void wxSoundStreamWin::NotifyDoneBuffer(wxUint32 dev_handle
, int flag
)
666 wxSoundInfoHeader
*info
;
668 if (flag
== wxSOUND_OUTPUT
) {
669 if (!m_internal
->m_output_enabled
)
672 // Queue pointer: reader
673 m_output_frag_out
= (m_output_frag_out
+ 1) % WXSOUND_MAX_QUEUE
;
674 info
= m_headers_play
[m_output_frag_out
];
675 // Clear header to tell the system the buffer is free now
677 m_queue_filled
= FALSE
;
679 // Try to requeue a new buffer.
680 OnSoundEvent(wxSOUND_OUTPUT
);
682 if (!m_internal
->m_input_enabled
)
685 // Recording completed
686 m_headers_rec
[m_input_frag_in
]->m_recording
= FALSE
;
687 // Queue pointer: writer
688 m_input_frag_in
= (m_input_frag_in
+ 1) % WXSOUND_MAX_QUEUE
;
690 OnSoundEvent(wxSOUND_INPUT
);
691 m_queue_filled
= FALSE
;
695 // -------------------------------------------------------------------------
697 // -------------------------------------------------------------------------
698 bool wxSoundStreamWin::SetSoundFormat(wxSoundFormatBase
& base
)
700 // TODO: detect best format
701 return wxSoundStream::SetSoundFormat(base
);
704 // -------------------------------------------------------------------------
706 // -------------------------------------------------------------------------
707 bool wxSoundStreamWin::StartProduction(int evt
)
712 if ((m_internal
->m_output_enabled
&& (evt
& wxSOUND_OUTPUT
)) ||
713 (m_internal
->m_input_enabled
&& (evt
& wxSOUND_INPUT
)))
716 if (!OpenDevice(evt
))
719 m_production_started
= TRUE
;
720 m_queue_filled
= FALSE
;
721 // Send a dummy event to start.
722 if (evt
& wxSOUND_OUTPUT
)
723 OnSoundEvent(wxSOUND_OUTPUT
);
725 if (evt
& wxSOUND_INPUT
) {
727 for (i
=0;i
<WXSOUND_MAX_QUEUE
;i
++)
728 AddToQueue(m_headers_rec
[i
]);
730 waveInStart(m_internal
->m_devin
);
736 // -------------------------------------------------------------------------
738 // ------------------------------------------------------------------------
739 bool wxSoundStreamWin::StopProduction()
741 if (!m_production_started
) {
742 m_snderror
= wxSOUND_NOTSTARTED
;
746 m_snderror
= wxSOUND_NOERROR
;
747 m_production_started
= FALSE
;
752 // -------------------------------------------------------------------------
754 // -------------------------------------------------------------------------
755 bool wxSoundStreamWin::QueueFilled() const
757 return (!m_production_started
|| m_queue_filled
);
761 // --------------------------------------------------------------------------
763 // --------------------------------------------------------------------------
765 class wxSoundWinModule
: public wxModule
{
766 DECLARE_DYNAMIC_CLASS(wxSoundWinModule
)
772 IMPLEMENT_DYNAMIC_CLASS(wxSoundWinModule
, wxModule
)
774 bool wxSoundWinModule::OnInit() {
775 wxSoundHandleList
= new wxList(wxKEY_INTEGER
);
779 void wxSoundWinModule::OnExit() {
780 delete wxSoundHandleList
;