1 // --------------------------------------------------------------------------
5 // Author: Guilhem Lavaux <lavaux@easynet.fr> (C) 1999
7 // --------------------------------------------------------------------------
9 #pragma implementation "sndesd.cpp"
12 #include <sys/types.h>
16 #include <wx/string.h>
25 #define MY_ESD_NAME "wxWindows/wxSoundStreamESD"
27 wxSoundStreamESD::wxSoundStreamESD(const wxString
& hostname
)
29 wxSoundFormatPcm pcm_default
;
31 m_fd
= esd_monitor_stream(ESD_MONO
| ESD_BITS8
| ESD_RECORD
, 22050,
32 hostname
.mb_str(), MY_ESD_NAME
);
35 m_snderror
= wxSOUND_INVDEV
;
41 m_hostname
= hostname
;
43 SetSoundFormat(pcm_default
);
45 m_snderror
= wxSOUND_NOERR
;
49 wxSoundStreamESD::~wxSoundStreamESD()
55 wxSoundStream
& wxSoundStreamESD::Read(void *buffer
, size_t len
)
59 m_lastcount
= (size_t)ret
= read(m_fd
, buffer
, len
);
62 m_snderror
= wxSOUND_IOERR
;
64 m_snderror
= wxSOUND_NOERR
;
69 wxSoundStream
& wxSoundStreamESD::Write(const void *buffer
, size_t len
)
73 m_lastcount
= (size_t)ret
= write(m_fd
, buffer
, len
);
76 m_snderror
= wxSOUND_IOERR
;
78 m_snderror
= wxSOUND_NOERR
;
83 bool wxSoundStreamESD::SetSoundFormat(const wxSoundFormatBase
& format
)
85 wxSoundFormatPcm
*pcm_format
;
87 if (format
.GetType() != wxSOUND_PCM
) {
88 m_snderror
= wxSOUND_INVFRMT
;
93 m_snderror
= wxSOUND_INVDEV
;
100 m_sndformat
= format
.Clone();
102 m_snderror
= wxSOUND_MEMERR
;
105 pcm_format
= (wxSoundFormatPcm
*)m_sndformat
;
107 // Detect the best format
108 DetectBest(pcm_format
);
110 m_snderror
= wxSOUND_NOERR
;
111 if (*pcm_format
!= format
) {
112 m_snderror
= wxSOUND_NOTEXACT
;
119 static void _wxSound_OSS_CBack(gpointer data
, int source
,
120 GdkInputCondition condition
)
122 wxSoundStreamESD
*esd
= (wxSoundStreamESD
*)data
;
126 esd
->WakeUpEvt(wxSOUND_INPUT
);
128 case GDK_INPUT_WRITE
:
129 esd
->WakeUpEvt(wxSOUND_OUTPUT
);
137 void wxSoundStreamESD::WakeUpEvt(int evt
)
142 bool wxSoundStreamESD::StartProduction(int evt
)
144 wxSoundFormatPcm
*pcm
;
150 pcm
= (wxSoundFormatPcm
*)m_sndformat
;
152 flag
|= (pcm
->GetBPS() == 16) ? ESD_BITS16
: ESD_BITS8
;
153 flag
|= (pcm
->GetChannels() == 2) ? ESD_STEREO
: ESD_MONO
;
155 if (evt
== wxSOUND_OUTPUT
) {
156 flag
|= ESD_PLAY
| ESD_STREAM
;
157 m_fd
= esd_play_stream(flag
, pcm
->GetSampleRate(), m_hostname
.mb_str(),
160 flag
|= ESD_RECORD
| ESD_STREAM
;
161 m_fd
= esd_record_stream(flag
, pcm
->GetSampleRate(), m_hostname
.mb_str(),
166 if (evt
== wxSOUND_OUTPUT
)
167 m_tag
= gdk_input_add(m_fd
, GDK_INPUT_WRITE
, _wxSound_OSS_CBack
, (gpointer
)this);
169 m_tag
= gdk_input_add(m_fd
, GDK_INPUT_READ
, _wxSound_OSS_CBack
, (gpointer
)this);
177 bool wxSoundStreamESD::StopProduction()
182 gdk_input_remove(m_tag
);
189 // Detect the closest format (The best).
191 void wxSoundStreamESD::DetectBest(wxSoundFormatPcm
*pcm
)
193 wxSoundFormatPcm best_pcm
;
195 // We change neither the number of channels nor the sample rate
197 best_pcm
.SetSampleRate(pcm
->GetSampleRate());
198 best_pcm
.SetChannels(pcm
->GetChannels());
200 // It supports 16 bits
201 if (pcm
->GetBPS() == 16)
204 best_pcm
.SetOrder(wxLITTLE_ENDIAN
);
205 best_pcm
.Signed(TRUE
);
207 // Finally recopy the new format