1 |
cebix |
1.1 |
/* |
2 |
|
|
* main_sdl.cpp - SIDPlayer SDL main program |
3 |
|
|
* |
4 |
cebix |
1.6 |
* SIDPlayer (C) Copyright 1996-2001 Christian Bauer |
5 |
cebix |
1.1 |
* |
6 |
|
|
* This program is free software; you can redistribute it and/or modify |
7 |
|
|
* it under the terms of the GNU General Public License as published by |
8 |
|
|
* the Free Software Foundation; either version 2 of the License, or |
9 |
|
|
* (at your option) any later version. |
10 |
|
|
* |
11 |
|
|
* This program is distributed in the hope that it will be useful, |
12 |
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 |
|
|
* GNU General Public License for more details. |
15 |
|
|
* |
16 |
|
|
* You should have received a copy of the GNU General Public License |
17 |
|
|
* along with this program; if not, write to the Free Software |
18 |
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
19 |
|
|
*/ |
20 |
|
|
|
21 |
|
|
#include "sys.h" |
22 |
|
|
|
23 |
|
|
#include <SDL.h> |
24 |
cebix |
1.9 |
#include <SDL/SDL_endian.h> |
25 |
|
|
|
26 |
cebix |
1.1 |
#include <stdio.h> |
27 |
|
|
#include <stdlib.h> |
28 |
cebix |
1.9 |
#include <errno.h> |
29 |
cebix |
1.1 |
|
30 |
|
|
#include "main.h" |
31 |
cebix |
1.4 |
#include "prefs.h" |
32 |
|
|
#include "sid.h" |
33 |
cebix |
1.1 |
|
34 |
|
|
|
35 |
|
|
/* |
36 |
|
|
* Main program |
37 |
|
|
*/ |
38 |
|
|
|
39 |
|
|
static void usage(const char *prg_name) |
40 |
|
|
{ |
41 |
cebix |
1.7 |
printf("Usage: %s [OPTION...] FILE [song_number]\n", prg_name); |
42 |
cebix |
1.5 |
PrefsPrintUsage(); |
43 |
cebix |
1.1 |
exit(0); |
44 |
|
|
} |
45 |
|
|
|
46 |
|
|
static void quit(void) |
47 |
|
|
{ |
48 |
|
|
ExitAll(); |
49 |
|
|
SDL_Quit(); |
50 |
|
|
} |
51 |
|
|
|
52 |
|
|
int main(int argc, char **argv) |
53 |
|
|
{ |
54 |
|
|
// Print banner |
55 |
|
|
printf( |
56 |
|
|
PACKAGE " Version " VERSION "\n\n" |
57 |
cebix |
1.5 |
"Copyright (C) 1996-2001 Christian Bauer\n" |
58 |
cebix |
1.1 |
"E-mail: Christian.Bauer@uni-mainz.de\n" |
59 |
|
|
"http://www.uni-mainz.de/~bauec002/\n\n" |
60 |
|
|
"This is free software with ABSOLUTELY NO WARRANTY.\n" |
61 |
|
|
"You are welcome to redistribute it under certain conditions.\n" |
62 |
|
|
"For details, see the file COPYING.\n\n" |
63 |
|
|
); |
64 |
|
|
|
65 |
cebix |
1.5 |
// Initialize everything |
66 |
|
|
if (SDL_Init(SDL_INIT_AUDIO) < 0) { |
67 |
|
|
fprintf(stderr, "Couldn't initialize SDL (%s)\n", SDL_GetError()); |
68 |
|
|
exit(1); |
69 |
|
|
} |
70 |
|
|
atexit(quit); |
71 |
|
|
InitAll(argc, argv); |
72 |
|
|
|
73 |
|
|
// Parse non-option arguments |
74 |
cebix |
1.8 |
const char *file_name = NULL; |
75 |
cebix |
1.1 |
int song = 0; |
76 |
cebix |
1.8 |
for (int i=1; i<argc; i++) { |
77 |
|
|
if (strcmp(argv[i], "--help") == 0) |
78 |
|
|
usage(argv[0]); |
79 |
|
|
else if (argv[i][0] == '-') { |
80 |
|
|
fprintf(stderr, "Unrecognized option '%s'\n", argv[i]); |
81 |
|
|
usage(argv[0]); |
82 |
|
|
} else { |
83 |
|
|
if (file_name == NULL) |
84 |
|
|
file_name = argv[i]; // First non-option argument is file name |
85 |
|
|
else |
86 |
|
|
song = atoi(argv[i]); // Second non-option argument is song number |
87 |
cebix |
1.3 |
} |
88 |
|
|
} |
89 |
cebix |
1.8 |
if (file_name == NULL) |
90 |
|
|
usage(argv[0]); |
91 |
cebix |
1.1 |
|
92 |
|
|
// Load given PSID file |
93 |
cebix |
1.3 |
if (!LoadPSIDFile(file_name)) { |
94 |
|
|
fprintf(stderr, "Couldn't load '%s' (not a PSID file?)\n", file_name); |
95 |
cebix |
1.1 |
exit(1); |
96 |
|
|
} |
97 |
|
|
|
98 |
|
|
// Select song |
99 |
|
|
if (song > 0) { |
100 |
|
|
if (song > number_of_songs) |
101 |
|
|
song = number_of_songs; |
102 |
|
|
SelectSong(song - 1); |
103 |
cebix |
1.9 |
SIDAdjustSpeed(PrefsFindInt32("speed")); // SelectSong resets this to 100% |
104 |
cebix |
1.1 |
} |
105 |
|
|
|
106 |
|
|
// Print file information |
107 |
|
|
printf("Module Name: %s\n", module_name); |
108 |
|
|
printf("Author : %s\n", author_name); |
109 |
|
|
printf("Copyright : %s\n\n", copyright_info); |
110 |
|
|
printf("Playing song %d/%d\n", current_song + 1, number_of_songs); |
111 |
|
|
|
112 |
cebix |
1.9 |
// Replay or output to file? |
113 |
|
|
const char *outfile = PrefsFindString("outfile"); |
114 |
|
|
if (outfile) { |
115 |
|
|
|
116 |
|
|
// Open file |
117 |
|
|
SDL_RWops *f = SDL_RWFromFile(outfile, "wb"); |
118 |
|
|
if (f == NULL) { |
119 |
|
|
fprintf(stderr, "Can't open '%s' for writing (%s)\n", outfile, strerror(errno)); |
120 |
|
|
exit(1); |
121 |
|
|
} |
122 |
|
|
|
123 |
|
|
// Get format information |
124 |
|
|
int32 channels = (PrefsFindBool("stereo") ? 2 : 1); |
125 |
|
|
int32 bits = (PrefsFindBool("audio16bit") ? 16 : 8); |
126 |
|
|
int32 rate = PrefsFindInt32("samplerate"); |
127 |
|
|
int32 time = PrefsFindInt32("time"); |
128 |
|
|
int32 bytes_per_frame = channels * (bits / 8); |
129 |
|
|
int32 bytes_per_second = rate * bytes_per_frame; |
130 |
|
|
int32 data_length = time * bytes_per_second; |
131 |
|
|
|
132 |
|
|
// Write header |
133 |
|
|
SDL_WriteLE32(f, 0x46464952); // "RIFF" |
134 |
|
|
SDL_WriteLE32(f, 36 + data_length); |
135 |
|
|
SDL_WriteLE32(f, 0x45564157); // "WAVE" |
136 |
|
|
SDL_WriteLE32(f, 0x20746D66); // "fmt " |
137 |
|
|
SDL_WriteLE32(f, 16); |
138 |
|
|
SDL_WriteLE16(f, 1); |
139 |
|
|
SDL_WriteLE16(f, channels); |
140 |
|
|
SDL_WriteLE32(f, rate); |
141 |
|
|
SDL_WriteLE32(f, bytes_per_second); |
142 |
|
|
SDL_WriteLE16(f, bytes_per_frame); |
143 |
|
|
SDL_WriteLE16(f, bits); |
144 |
|
|
SDL_WriteLE32(f, 0x61746164); // "data" |
145 |
|
|
SDL_WriteLE32(f, data_length); |
146 |
|
|
|
147 |
|
|
// Calculate sound and write to file |
148 |
|
|
uint8 *buf = new uint8[bytes_per_second]; |
149 |
|
|
for (int i=0; i<time; i++) { |
150 |
|
|
SIDCalcBuffer(buf, bytes_per_second); |
151 |
|
|
#if SDL_BYTEORDER == SDL_BIG_ENDIAN |
152 |
|
|
if (bits > 8) { |
153 |
|
|
// WAV file is little-endian, swap audio data |
154 |
|
|
for (int b=0; b<bytes_per_second; b+=2) { |
155 |
|
|
uint8 tmp = buf[b]; |
156 |
|
|
buf[b] = buf[b+1]; |
157 |
|
|
buf[b+1] = tmp; |
158 |
|
|
} |
159 |
|
|
} |
160 |
|
|
#endif |
161 |
|
|
SDL_RWwrite(f, buf, bytes_per_second, 1); |
162 |
|
|
} |
163 |
|
|
delete[] buf; |
164 |
|
|
|
165 |
|
|
// Close file |
166 |
|
|
SDL_RWclose(f); |
167 |
|
|
printf("Output written to '%s'\n", outfile); |
168 |
|
|
|
169 |
|
|
} else { |
170 |
|
|
|
171 |
|
|
// Start replay and enter main loop |
172 |
|
|
SDL_PauseAudio(false); |
173 |
|
|
while (true) { |
174 |
|
|
SDL_Event e; |
175 |
|
|
if (SDL_WaitEvent(&e)) { |
176 |
|
|
if (e.type == SDL_QUIT) |
177 |
|
|
break; |
178 |
|
|
} |
179 |
cebix |
1.1 |
} |
180 |
|
|
} |
181 |
|
|
|
182 |
|
|
return 0; |
183 |
|
|
} |