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_play_stream(ESD_PLAY
| ESD_STREAM
| ESD_MONO
| ESD_BITS8
, 22050,
32 // hostname.mb_str(), MY_ESD_NAME);
36 m_snderror
= wxSOUND_INVDEV
;
42 m_hostname
= hostname
;
44 SetSoundFormat(pcm_default
);
46 m_snderror
= wxSOUND_NOERR
;
51 wxSoundStreamESD::~wxSoundStreamESD()
57 wxSoundStream
& wxSoundStreamESD::Read(void *buffer
, wxUint32 len
)
61 m_lastcount
= (wxUint32
)ret
= read(m_fd
, buffer
, len
);
64 m_snderror
= wxSOUND_IOERR
;
66 m_snderror
= wxSOUND_NOERR
;
71 wxSoundStream
& wxSoundStreamESD::Write(const void *buffer
, wxUint32 len
)
75 m_lastcount
= (wxUint32
)ret
= write(m_fd
, buffer
, len
);
78 m_snderror
= wxSOUND_IOERR
;
80 m_snderror
= wxSOUND_NOERR
;
87 bool wxSoundStreamESD::SetSoundFormat(const wxSoundFormatBase
& format
)
89 wxSoundFormatPcm
*pcm_format
;
91 if (format
.GetType() != wxSOUND_PCM
) {
92 m_snderror
= wxSOUND_INVFRMT
;
97 m_snderror
= wxSOUND_INVDEV
;
104 m_sndformat
= format
.Clone();
106 m_snderror
= wxSOUND_MEMERR
;
109 pcm_format
= (wxSoundFormatPcm
*)m_sndformat
;
111 // Detect the best format
112 DetectBest(pcm_format
);
114 m_snderror
= wxSOUND_NOERR
;
115 if (*pcm_format
!= format
) {
116 m_snderror
= wxSOUND_NOTEXACT
;
123 static void _wxSound_OSS_CBack(gpointer data
, int source
,
124 GdkInputCondition condition
)
126 wxSoundStreamESD
*esd
= (wxSoundStreamESD
*)data
;
130 esd
->WakeUpEvt(wxSOUND_INPUT
);
132 case GDK_INPUT_WRITE
:
133 esd
->WakeUpEvt(wxSOUND_OUTPUT
);
141 void wxSoundStreamESD::WakeUpEvt(int evt
)
147 bool wxSoundStreamESD::StartProduction(int evt
)
149 wxSoundFormatPcm
*pcm
;
155 pcm
= (wxSoundFormatPcm
*)m_sndformat
;
157 flag
|= (pcm
->GetBPS() == 16) ? ESD_BITS16
: ESD_BITS8
;
158 flag
|= (pcm
->GetChannels() == 2) ? ESD_STEREO
: ESD_MONO
;
160 if (evt
== wxSOUND_OUTPUT
) {
161 flag
|= ESD_PLAY
| ESD_STREAM
;
162 m_fd
= esd_play_stream(flag
, pcm
->GetSampleRate(), NULL
,
165 flag
|= ESD_RECORD
| ESD_STREAM
;
166 m_fd
= esd_record_stream(flag
, pcm
->GetSampleRate(), NULL
,
171 if (evt
== wxSOUND_OUTPUT
)
172 m_tag
= gdk_input_add(m_fd
, GDK_INPUT_WRITE
, _wxSound_OSS_CBack
, (gpointer
)this);
174 m_tag
= gdk_input_add(m_fd
, GDK_INPUT_READ
, _wxSound_OSS_CBack
, (gpointer
)this);
183 bool wxSoundStreamESD::StopProduction()
188 gdk_input_remove(m_tag
);
196 // Detect the closest format (The best).
198 void wxSoundStreamESD::DetectBest(wxSoundFormatPcm
*pcm
)
200 wxSoundFormatPcm best_pcm
;
202 // We change neither the number of channels nor the sample rate
204 best_pcm
.SetSampleRate(pcm
->GetSampleRate());
205 best_pcm
.SetChannels(pcm
->GetChannels());
207 // It supports 16 bits
208 if (pcm
->GetBPS() == 16)
211 best_pcm
.SetOrder(wxLITTLE_ENDIAN
);
212 best_pcm
.Signed(TRUE
);
214 // Finally recopy the new format