ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/SIDPlayer/src/main_sdl.cpp
(Generate patch)

Comparing SIDPlayer/src/main_sdl.cpp (file contents):
Revision 1.8 by cebix, 2001-04-01T12:13:49Z vs.
Revision 1.9 by cebix, 2001-04-01T17:04:43Z

# Line 21 | Line 21
21   #include "sys.h"
22  
23   #include <SDL.h>
24 + #include <SDL/SDL_endian.h>
25 +
26   #include <stdio.h>
27   #include <stdlib.h>
28 + #include <errno.h>
29  
30   #include "main.h"
31   #include "prefs.h"
# Line 66 | Line 69 | int main(int argc, char **argv)
69          }
70          atexit(quit);
71          InitAll(argc, argv);
69        int32 selected_speed = PrefsFindInt32("speed");
72  
73          // Parse non-option arguments
74          const char *file_name = NULL;
# Line 98 | Line 100 | int main(int argc, char **argv)
100                  if (song > number_of_songs)
101                          song = number_of_songs;
102                  SelectSong(song - 1);
103 +                SIDAdjustSpeed(PrefsFindInt32("speed")); // SelectSong resets this to 100%
104          }
105  
106          // Print file information
# Line 106 | Line 109 | int main(int argc, char **argv)
109          printf("Copyright  : %s\n\n", copyright_info);
110          printf("Playing song %d/%d\n", current_song + 1, number_of_songs);
111  
112 <        // Start replay and enter main loop
113 <        SIDAdjustSpeed(selected_speed); // SelectSong resets this to 100%
114 <        SDL_PauseAudio(false);
115 <        while (true) {
116 <                SDL_Event e;
117 <                if (SDL_WaitEvent(&e)) {
118 <                        if (e.type == SDL_QUIT)
119 <                                break;
112 >        // 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                  }
180          }
181  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines