-
Notifications
You must be signed in to change notification settings - Fork 1
/
Main_x86.cpp
189 lines (166 loc) · 4.74 KB
/
Main_x86.cpp
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
189
#include <stdio.h>
#include <fstream>
#include <limits.h>
#include <signal.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <fstream>
#include "FC.h"
#include "MyTypes.h"
#include <alsa/asoundlib.h>
using namespace std;
const udword extraFileBufLen = 8 + 1; // see FC.cpp
bool exitFlag;
void (*oldSigHupHandler) (int);
void (*oldSigIntHandler) (int);
void (*oldSigQuitHandler) (int);
void (*oldSigTermHandler) (int);
void mysighandler(int signum)
{
switch (signum) {
case SIGHUP:
{
exitFlag = true;
break;
}
case SIGINT:
{
exitFlag = true;
break;
}
case SIGQUIT:
{
exitFlag = true;
break;
}
case SIGTERM:
{
exitFlag = true;
break;
}
default:
break;
}
}
int main(int argc, char *argv[])
{
int inFileArgN = 0;
int startStep = 0;
int endStep = 0;
int i;
int err;
snd_pcm_t *playback_handle;
snd_pcm_hw_params_t *hw_params;
for (int a = 1; a < argc; a++) {
if (argv[a][0] == '-') {
if (strncasecmp(&argv[a][0], "--start=", 8) == 0) {
startStep = atoi(argv[a] + 8);
} else if (strncasecmp(&argv[a][0], "--end=", 6) == 0) {
endStep = atoi(argv[a] + 6);
}
} else {
if (inFileArgN == 0) {
inFileArgN = a;
} else {
cerr << argv[0] << ": Missings argument(s)." << endl;
}
}
}
if (inFileArgN == 0) {
cerr << "Missing file name argument." << endl;
exit(-1);
}
ubyte *buffer = 0;
streampos fileLen = 0;
ifstream myIn(argv[inFileArgN], ios::in | ios::binary | ios::ate);
myIn.seekg(0, ios::end);
fileLen = myIn.tellg();
buffer = (ubyte *) malloc((int) fileLen + extraFileBufLen);
myIn.seekg(0, ios::beg);
cout << "Loading" << flush;
streampos restFileLen = fileLen;
while (restFileLen > INT_MAX) {
myIn.read((char *) buffer + (fileLen - restFileLen), INT_MAX);
restFileLen -= INT_MAX;
cout << "." << flush;
}
if (restFileLen > 0) {
myIn.read((char *) buffer + (fileLen - restFileLen), restFileLen);
cout << "." << flush;
}
cout << endl << flush;
if (myIn.bad()) {
cerr << argv[0] << ": ERROR: Cannot read file: " << argv[inFileArgN] << endl;
return (-1);
}
myIn.close();
udword pcmFreq = 44100;
ubyte *sampleBuffer = new ubyte[pcmFreq];
if ((err = snd_pcm_open(&playback_handle, "default", SND_PCM_STREAM_PLAYBACK, 0)) < 0) {
fprintf(stderr, "cannot open audio device %s (%s)\n", argv[1], snd_strerror(err));
exit(1);
}
if ((err = snd_pcm_hw_params_malloc(&hw_params)) < 0) {
fprintf(stderr, "cannot allocate hardware parameter structure (%s)\n", snd_strerror(err));
exit(1);
}
if ((err = snd_pcm_hw_params_any(playback_handle, hw_params)) < 0) {
fprintf(stderr, "cannot initialize hardware parameter structure (%s)\n", snd_strerror(err));
exit(1);
}
if ((err = snd_pcm_hw_params_set_access(playback_handle, hw_params, SND_PCM_ACCESS_RW_INTERLEAVED)) < 0) {
fprintf(stderr, "cannot set access type (%s)\n", snd_strerror(err));
exit(1);
}
if ((err = snd_pcm_hw_params_set_format(playback_handle, hw_params, SND_PCM_FORMAT_S16_LE)) < 0) {
fprintf(stderr, "cannot set sample format (%s)\n", snd_strerror(err));
exit(1);
}
if ((err = snd_pcm_hw_params_set_rate_near(playback_handle, hw_params, (unsigned int *) &pcmFreq, 0)) < 0) {
fprintf(stderr, "cannot set sample rate (%s)\n", snd_strerror(err));
exit(1);
}
if ((err = snd_pcm_hw_params_set_channels(playback_handle, hw_params, 2)) < 0) {
fprintf(stderr, "cannot set channel count (%s)\n", snd_strerror(err));
exit(1);
}
if ((err = snd_pcm_hw_params(playback_handle, hw_params)) < 0) {
fprintf(stderr, "cannot set parameters (%s)\n", snd_strerror(err));
exit(1);
}
snd_pcm_hw_params_free(hw_params);
if ((err = snd_pcm_prepare(playback_handle)) < 0) {
fprintf(stderr, "cannot prepare audio interface for use (%s)\n", snd_strerror(err));
exit(1);
}
exitFlag = false;
if ((signal(SIGHUP, &mysighandler) == SIG_ERR)
|| (signal(SIGINT, &mysighandler) == SIG_ERR)
|| (signal(SIGQUIT, &mysighandler) == SIG_ERR) || (signal(SIGTERM, &mysighandler) == SIG_ERR)) {
cerr << argv[0] << ": ERROR: Cannot set signal handlers." << endl;
exit(-1);
}
if (FC_init(buffer, fileLen, startStep, endStep)) {
cout << "Module format: " << mixerFormatName << endl;
extern void mixerInit(udword freq, int bits, int channels, uword zero);
mixerInit(pcmFreq, 16, 2, 0);
extern void mixerSetReplayingSpeed();
mixerSetReplayingSpeed();
while (true) {
extern void mixerFillBuffer(void *, udword);
mixerFillBuffer(sampleBuffer, 1024);
if ((err = snd_pcm_writei(playback_handle, sampleBuffer, 256)) != 256) {
fprintf(stderr, "write to audio interface failed (%s)\n", snd_strerror(err));
exit(1);
}
if (exitFlag)
break;
}
} else {
cout << "File format not recognized." << endl;
}
snd_pcm_close(playback_handle);
delete[]sampleBuffer;
return (0);
}