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 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
|
/* tape2wav.c: Convert tape files (tzx, tap, etc.) to .wav files
Copyright (c) 2007 Fredrick Meunier
Copyright (c) 2014-2015 Sergio Baldovi
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
Author contact information:
E-mail: fredm@spamcop.net
*/
#include <config.h>
#include <errno.h>
#include <fcntl.h>
#include <getopt.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <libspectrum.h>
#include <audiofile.h>
#include "utils.h"
#define PROGRAM_NAME "tape2wav"
static void show_help( void );
static void show_version( void );
static int read_tape( char *filename, libspectrum_tape **tape );
static int write_tape( char *filename, libspectrum_tape *tape );
char *progname;
int sample_rate = 44100;
int
main( int argc, char **argv )
{
int c, error = 0;
libspectrum_tape *tzx;
progname = argv[0];
struct option long_options[] = {
{ "help", 0, NULL, 'h' },
{ "version", 0, NULL, 'V' },
{ 0, 0, 0, 0 }
};
while( ( c = getopt_long( argc, argv, "r:hV", long_options, NULL ) ) != -1 ) {
switch( c ) {
case 'r': sample_rate = abs( atoi( optarg ) ); break;
case 'h': show_help(); return 0;
case 'V': show_version(); return 0;
case '?':
/* getopt prints an error message to stderr */
error = 1;
break;
default:
error = 1;
fprintf( stderr, "%s: unknown option `%c'\n", progname, (char) c );
break;
}
}
argc -= optind;
argv += optind;
if( error ) {
fprintf( stderr, "Try `%s --help' for more information.\n", progname );
return error;
}
if( argc < 2 ) {
fprintf( stderr,
"%s: usage: %s [-r rate] <infile> <outfile>\n",
progname,
progname );
fprintf( stderr, "Try `%s --help' for more information.\n", progname );
return 1;
}
error = init_libspectrum(); if( error ) return error;
if( read_tape( argv[0], &tzx ) ) return 1;
if( write_tape( argv[1], tzx ) ) {
libspectrum_tape_free( tzx );
return 1;
}
libspectrum_tape_free( tzx );
return 0;
}
static void
show_version( void )
{
printf(
PROGRAM_NAME " (" PACKAGE ") " PACKAGE_VERSION "\n"
"Copyright (c) 2007 Fredrick Meunier\n"
"License GPLv2+: GNU GPL version 2 or later "
"<http://gnu.org/licenses/gpl.html>\n"
"This is free software: you are free to change and redistribute it.\n"
"There is NO WARRANTY, to the extent permitted by law.\n" );
}
static void
show_help( void )
{
printf(
"Usage: %s [OPTION] <infile> <outfile>\n"
"Converts ZX Spectrum tape images to audio files.\n"
"\n"
"Options:\n"
" -r <sample rate> Set the sample rate for the target wav file. Defaults to\n"
" 44100 Hz.\n"
" -h, --help Display this help and exit.\n"
" -V, --version Output version information and exit.\n"
"\n"
"Report %s bugs to <%s>\n"
"%s home page: <%s>\n"
"For complete documentation, see the manual page of %s.\n",
progname,
PROGRAM_NAME, PACKAGE_BUGREPORT, PACKAGE_NAME, PACKAGE_URL, PROGRAM_NAME
);
}
static int
read_tape( char *filename, libspectrum_tape **tape )
{
libspectrum_byte *buffer; size_t length;
if( read_file( filename, &buffer, &length ) ) return 1;
*tape = libspectrum_tape_alloc();
if( libspectrum_tape_read( *tape, buffer, length, LIBSPECTRUM_ID_UNKNOWN,
filename ) ) {
free( buffer );
return 1;
}
free( buffer );
return 0;
}
static int
write_tape( char *filename, libspectrum_tape *tape )
{
libspectrum_byte *buffer, *ptr; size_t length;
size_t tape_length = 0;
libspectrum_error error;
short level = 0; /* The last level output to this block */
libspectrum_dword pulse_tstates = 0;
libspectrum_dword balance_tstates = 0;
int flags = 0;
int framesWritten;
AFfilehandle file;
AFfilesetup setup = afNewFileSetup();
unsigned int scale = 3500000/sample_rate;
length = 8192;
buffer = malloc( length );
if( !buffer ) {
fprintf( stderr, "%s: unable to allocate memory for conversion buffer\n",
progname );
return 1;
}
while( !(flags & LIBSPECTRUM_TAPE_FLAGS_TAPE) ) {
libspectrum_dword pulse_length = 0;
size_t i;
error = libspectrum_tape_get_next_edge( &pulse_tstates, &flags, tape );
if( error != LIBSPECTRUM_ERROR_NONE ) {
free( buffer );
return 1;
}
/* Invert the microphone state */
if( pulse_tstates ||
!( flags & LIBSPECTRUM_TAPE_FLAGS_NO_EDGE ) ||
( flags & ( LIBSPECTRUM_TAPE_FLAGS_STOP |
LIBSPECTRUM_TAPE_FLAGS_LEVEL_LOW |
LIBSPECTRUM_TAPE_FLAGS_LEVEL_HIGH ) ) ) {
if( flags & LIBSPECTRUM_TAPE_FLAGS_NO_EDGE ) {
/* Do nothing */
} else if( flags & LIBSPECTRUM_TAPE_FLAGS_LEVEL_LOW ) {
level = 0;
} else if( flags & LIBSPECTRUM_TAPE_FLAGS_LEVEL_HIGH ) {
level = 1;
} else {
level = !level;
}
}
balance_tstates += pulse_tstates;
if( flags & LIBSPECTRUM_TAPE_FLAGS_NO_EDGE ) continue;
pulse_length = balance_tstates / scale;
balance_tstates = balance_tstates % scale;
/* TZXs produced by snap2tzx have very tight tolerances, err on the side of
producing a pulse that is too long rather than too short */
if( balance_tstates > scale>>1 ) {
pulse_length++;
balance_tstates = 0;
}
while( tape_length + pulse_length > length ) {
length *= 2;
ptr = buffer;
buffer = realloc( buffer, length );
if( !buffer ) {
free( ptr );
fprintf( stderr, "%s: unable to allocate memory for conversion buffer\n",
progname );
return 1;
}
}
for( i = 0; i < pulse_length; i++ ) {
buffer[ tape_length++ ] = level ? 0xff : 0x00;
}
}
afInitFileFormat( setup, AF_FILE_WAVE );
afInitChannels( setup, AF_DEFAULT_TRACK, 1 );
afInitSampleFormat( setup, AF_DEFAULT_TRACK, AF_SAMPFMT_UNSIGNED, 8 );
afInitRate( setup, AF_DEFAULT_TRACK, sample_rate );
if( strncmp( filename, "-", 1 ) == 0 ) {
int fd = fileno( stdout );
if( isatty( fd ) ) {
fprintf( stderr, "%s: won't output binary data to a terminal\n",
progname );
free( buffer );
afFreeFileSetup( setup );
return 1;
}
#ifdef WIN32
setmode( fd, O_BINARY );
#endif /* #ifdef WIN32 */
file = afOpenFD( fd, "w", setup );
} else {
file = afOpenFile( filename, "w", setup );
}
if( file == AF_NULL_FILEHANDLE ) {
fprintf( stderr, "%s: unable to open file '%s' for writing\n", progname,
filename );
free( buffer );
afFreeFileSetup( setup );
return 1;
}
framesWritten = afWriteFrames( file, AF_DEFAULT_TRACK, buffer, tape_length );
if(framesWritten != tape_length ) {
fprintf( stderr,
"%s: number of frames written does not match number of frames"
" requested\n",
progname );
free( buffer );
afFreeFileSetup( setup );
return 1;
}
if( afCloseFile( file ) ) {
fprintf( stderr, "%s: error closing wav file\n", progname );
free( buffer );
afFreeFileSetup( setup );
return 1;
}
free( buffer );
afFreeFileSetup( setup );
return 0;
}
|