chiark / gitweb /
Imported Upstream version 1.0.0
[e16] / src / sound_esd.c
1 /*
2  * Copyright (C) 2000-2007 Carsten Haitzler, Geoff Harrison and various contributors
3  * Copyright (C) 2004-2008 Kim Woelders
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a copy
6  * of this software and associated documentation files (the "Software"), to
7  * deal in the Software without restriction, including without limitation the
8  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
9  * sell copies of the Software, and to permit persons to whom the Software is
10  * furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in
13  * all copies of the Software, its documentation and marketing & publicity
14  * materials, and acknowledgment shall be given in the documentation, materials
15  * and software packages that this Software was used.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20  * THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
21  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  */
24 #include "E.h"
25 #if defined(HAVE_SOUND) && defined(HAVE_SOUND_ESD)
26 #include "sound.h"
27 #include <esd.h>
28
29 #ifdef USE_MODULES
30 #define Estrdup strdup
31 #endif
32
33 struct _sample {
34    SoundSampleData     ssd;
35    int                 id;
36 };
37
38 static int          sound_fd = -1;
39
40 static Sample      *
41 _sound_esd_Load(const char *file)
42 {
43    Sample             *s;
44    int                 err, format;
45
46    if (sound_fd < 0)
47       return NULL;
48
49    s = ECALLOC(Sample, 1);
50    if (!s)
51       return NULL;
52
53    err = SoundSampleGetData(file, &s->ssd);
54    if (err)
55      {
56         Efree(s);
57         return NULL;
58      }
59
60    format = ESD_STREAM | ESD_PLAY;
61    if (s->ssd.bit_per_sample == 8)
62       format |= ESD_BITS8;
63    else if (s->ssd.bit_per_sample == 16)
64       format |= ESD_BITS16;
65    if (s->ssd.channels == 1)
66       format |= ESD_MONO;
67    else if (s->ssd.channels == 2)
68       format |= ESD_STEREO;
69
70    s->id = esd_sample_getid(sound_fd, file);
71    if (s->id < 0)
72      {
73         int                 confirm;
74
75         s->id = esd_sample_cache(sound_fd, format, s->ssd.rate, s->ssd.size,
76                                  file);
77         write(sound_fd, s->ssd.data, s->ssd.size);
78         confirm = esd_confirm_sample_cache(sound_fd);
79         if (confirm != s->id)
80            s->id = 0;
81      }
82
83    _EFREE(s->ssd.data);
84    if (s->id <= 0)
85       _EFREE(s);
86
87    return s;
88 }
89
90 static void
91 _sound_esd_Destroy(Sample * s)
92 {
93    if (!s)
94       return;
95
96    if (s->id && sound_fd >= 0)
97      {
98 /*      Why the hell is this symbol not in esd? */
99 /*      it's in esd.h - evil evil evil */
100 /*      esd_sample_kill(sound_fd,s->id); */
101         esd_sample_free(sound_fd, s->id);
102      }
103    _EFREE(s->ssd.data);
104    Efree(s);
105 }
106
107 static void
108 _sound_esd_Play(Sample * s)
109 {
110    if (sound_fd < 0 || !s)
111       return;
112
113    if (s->id > 0)
114       esd_sample_play(sound_fd, s->id);
115 }
116
117 static int
118 _sound_esd_Init(void)
119 {
120    if (sound_fd >= 0)
121       return 0;
122
123    sound_fd = esd_open_sound(NULL);
124
125    return sound_fd < 0;
126 }
127
128 static void
129 _sound_esd_Exit(void)
130 {
131    if (sound_fd < 0)
132       return;
133
134    close(sound_fd);
135    sound_fd = -1;
136 }
137
138 __EXPORT__ extern const SoundOps SoundOps_esd;
139 const SoundOps      SoundOps_esd = {
140    _sound_esd_Init, _sound_esd_Exit, _sound_esd_Load, _sound_esd_Destroy,
141    _sound_esd_Play,
142 };
143
144 #endif /* HAVE_SOUND && HAVE_SOUND_ESD */