forked from libxmp/xmp-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsound_ahi.c
More file actions
188 lines (161 loc) · 3.98 KB
/
sound_ahi.c
File metadata and controls
188 lines (161 loc) · 3.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/* Amiga AHI driver for Extended Module Player
* Based on a MikMod driver written by Szilard Biro, which was loosely
* based on an old AmigaOS4 version by Fredrik Wikstrom.
*
* Extended Module Player
* Copyright (C) 1996-2026 Claudio Matsuoka and Hipolito Carraro Jr
*
* This file is part of the Extended Module Player and is distributed
* under the terms of the GNU General Public License. See the COPYING
* file for more information.
*/
#ifdef __amigaos4__
#define SHAREDMEMFLAG MEMF_SHARED
#define __USE_INLINE__
#else
#define SHAREDMEMFLAG MEMF_PUBLIC
#endif
#include <stdlib.h>
#include <string.h>
#include "sound.h"
#include <proto/exec.h>
#include <proto/dos.h>
#include <devices/ahi.h>
#define BUFFERSIZE (4 << 10)
static struct MsgPort *AHImp = NULL;
static struct AHIRequest *AHIReq[2] = { NULL, NULL };
static int active = 0;
static signed char *AHIBuf[2] = { NULL, NULL };
static void close_libs(void)
{
if (AHIReq[1]) {
/* in case req[1] is linked to req[0] */
AHIReq[0]->ahir_Link = NULL;
if (CheckIO((struct IORequest *) AHIReq[1]) == NULL) {
AbortIO((struct IORequest *) AHIReq[1]);
WaitIO((struct IORequest *) AHIReq[1]);
}
FreeVec(AHIReq[1]);
AHIReq[1] = NULL;
}
if (AHIReq[0]) {
if (CheckIO((struct IORequest *) AHIReq[0]) == NULL) {
AbortIO((struct IORequest *) AHIReq[0]);
WaitIO((struct IORequest *) AHIReq[0]);
}
if (AHIReq[0]->ahir_Std.io_Device) {
CloseDevice((struct IORequest *) AHIReq[0]);
AHIReq[0]->ahir_Std.io_Device = NULL;
}
DeleteIORequest((struct IORequest *) AHIReq[0]);
AHIReq[0] = NULL;
}
if (AHImp) {
DeleteMsgPort(AHImp);
AHImp = NULL;
}
if (AHIBuf[0]) {
FreeVec(AHIBuf[0]);
AHIBuf[0] = NULL;
}
if (AHIBuf[1]) {
FreeVec(AHIBuf[1]);
AHIBuf[1] = NULL;
}
}
static int init(struct options *options)
{
unsigned long fmt;
AHImp = CreateMsgPort();
if (AHImp == NULL) {
return -1;
}
AHIReq[0] = (struct AHIRequest *)
CreateIORequest(AHImp, sizeof(struct AHIRequest));
if (AHIReq[0] == NULL) {
goto err;
}
AHIReq[0]->ahir_Version = 4;
AHIReq[1] = AllocVec(sizeof(struct AHIRequest), SHAREDMEMFLAG);
if (AHIReq[1] == NULL) {
goto err;
}
if (OpenDevice((CONST_STRPTR)AHINAME, AHI_DEFAULT_UNIT,
(struct IORequest *)AHIReq[0], 0) != 0) {
goto err;
}
if (options->format & XMP_FORMAT_8BIT) {
fmt = options->format & XMP_FORMAT_MONO ? AHIST_M8S : AHIST_S8S;
} else {
fmt = options->format & XMP_FORMAT_MONO ? AHIST_M16S : AHIST_S16S;
}
AHIReq[0]->ahir_Std.io_Command = CMD_WRITE;
AHIReq[0]->ahir_Std.io_Data = NULL;
AHIReq[0]->ahir_Std.io_Offset = 0;
AHIReq[0]->ahir_Frequency = options->rate;
AHIReq[0]->ahir_Type = fmt;
AHIReq[0]->ahir_Volume = 0x10000;
AHIReq[0]->ahir_Position = 0x8000;
CopyMem(AHIReq[0], AHIReq[1], sizeof(struct AHIRequest));
AHIBuf[0] = AllocVec(BUFFERSIZE, SHAREDMEMFLAG | MEMF_CLEAR);
AHIBuf[1] = AllocVec(BUFFERSIZE, SHAREDMEMFLAG | MEMF_CLEAR);
if (AHIBuf[0] == NULL || AHIBuf[1] == NULL) {
goto err;
}
options->format &= ~XMP_FORMAT_UNSIGNED;
options->format &= ~XMP_FORMAT_32BIT;
active = 0;
return 0;
err:
close_libs();
return -1;
}
static void deinit(void)
{
close_libs();
}
static void play(void *b, int len)
{
signed char *in = (signed char *)b;
int chunk;
while (len > 0) {
if (AHIReq[active]->ahir_Std.io_Data) {
WaitIO((struct IORequest *) AHIReq[active]);
}
chunk = (len < BUFFERSIZE) ? len : BUFFERSIZE;
memcpy(AHIBuf[active], in, chunk);
len -= chunk;
in += chunk;
AHIReq[active]->ahir_Std.io_Data = AHIBuf[active];
AHIReq[active]->ahir_Std.io_Length = chunk;
AHIReq[active]->ahir_Link =
CheckIO((struct IORequest *) AHIReq[active ^ 1]) == NULL ?
AHIReq[active ^ 1] : NULL;
SendIO((struct IORequest *) AHIReq[active]);
active ^= 1;
}
}
static void flush(void)
{
}
static void onpause(void)
{
}
static void onresume(void)
{
}
static const char *description(void)
{
return "Amiga AHI audio";
}
const struct sound_driver sound_ahi = {
"ahi",
NULL,
description,
init,
deinit,
play,
flush,
onpause,
onresume
};