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 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355
|
/* audio2tape.cc: Convert audio files (wav, voc, etc.) to tape files (.tzx,.csw)
Copyright (c) 2007-2008 Fredrick Meunier
Copyright (c) 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 <exception>
#include <iostream>
#include <map>
#include <sstream>
#include <errno.h>
#include <getopt.h>
#include <math.h>
#include <string.h>
#include <unistd.h>
#include <libspectrum.h>
extern "C" {
#include "utils.h"
}
#include "audio2tape.h"
#include "importer/schmitt.h"
#include "importer/simple.h"
#include "importer/soundfile.h"
#include "converter/romloader.h"
static void write_tape( char *filename, libspectrum_tape *tape );
static void get_pause_block( libspectrum_tape *tape, double source_machine_hz,
double start_tstates, double end_tstates );
static trigger*
set_trigger_type_from_string( const std::string trigger_str,
libspectrum_byte simple_crossover_point,
libspectrum_byte schmitt_low2high_crossover_point,
libspectrum_byte schmitt_high2low_crossover_point
);
static void
show_version( void );
static void
show_help( void );
extern "C" {
char *progname;
}
const std::string schmitt_str("schmitt");
const std::string simple_str("simple");
static trigger*
set_trigger_type_from_string( const std::string trigger_str,
libspectrum_byte simple_crossover_point,
libspectrum_byte schmitt_low2high_crossover_point,
libspectrum_byte schmitt_high2low_crossover_point
)
{
if( trigger_str == simple_str ) {
return new simple(simple_crossover_point);
} else if( trigger_str == schmitt_str ) {
return new schmitt( schmitt_high2low_crossover_point,
schmitt_low2high_crossover_point );
} else {
std::ostringstream err;
err << "unrecognised trigger type: " << trigger_str;
throw audio2tape_exception(err.str().c_str());
}
}
audio2tape_exception::~audio2tape_exception() throw()
{
}
int
main( int argc, char **argv )
{
int c, error;
libspectrum_tape *tzx = 0;
bool standard_rom_timings = false;
bool keep_unrecognised_blocks = false;
libspectrum_byte zero_point = 0x7f;
libspectrum_byte schmitt_noise_threshold = 8;
bool show_stats = false;
double source_machine_hz = 3500000.0;
struct option long_options[] = {
{ "version", 0, NULL, 'V' },
{ "help", 0, NULL, 'h' },
{ 0, 0, 0, 0 }
};
progname = argv[0];
error = init_libspectrum(); if( error ) return error;
std::string trigger_type_string = schmitt_str;
while( ( c = getopt_long( argc, argv, "t:srkz:c:hV", long_options,
NULL ) ) != -1 ) {
switch( c ) {
case 't':
trigger_type_string = optarg;
break;
case 'r':
standard_rom_timings = true;
break;
case 'k':
keep_unrecognised_blocks = true;
break;
case 's':
show_stats = true;
break;
case 'z':
zero_point = atol( optarg );
break;
case 'c':
schmitt_noise_threshold = atol( 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:
std::cerr << progname << ": unknown option `" << (char) c << "'\n";
error = 1;
break;
}
}
argc -= optind;
argv += optind;
if( error ) {
std::cerr << "Try `audio2tape --help' for more information.\n";
return error;
}
if( argc < 2 ) {
std::cerr << progname << ": usage: " << progname <<
" [-r] [-k] [-s] [-t <trigger type>] [-z <zero level>] "
"[-c <schmitt noise threshold>] <infile> <outfile>\n";
std::cerr << "Try `audio2tape --help' for more information.\n";
return 1;
}
try {
trigger* t =
set_trigger_type_from_string( trigger_type_string,
zero_point,
zero_point+schmitt_noise_threshold,
zero_point-schmitt_noise_threshold
);
soundfile sf(argv[0], t, source_machine_hz, show_stats);
const pulse_list& pulses(sf.get_pulse_list());
romloader rl( source_machine_hz, show_stats );
double tstates = 0;
for( pulse_list::const_iterator i = pulses.begin();
i != pulses.end(); i++ ) {
rl.handle_pulse( tstates, *i );
tstates += *i;
}
// get blocks from ROMLoader, filling gaps with data from original tape
std::cout << "found " << rl.get_block_count() << " ROM blocks\n";
tzx = libspectrum_tape_alloc();
double tstate_start = 0;
double tstate_end = 0;
for( size_t i = 0; i < rl.get_block_count(); i++ ) {
tstate_end = rl.get_block_start( i );
if( tstate_start != tstate_end ) {
if( keep_unrecognised_blocks || tstate_start ) {
if( keep_unrecognised_blocks ) {
// get some original tape pulses and pop it in the tape
sf.get_tape_block( tzx, tstate_start, tstate_end );
} else {
get_pause_block( tzx, source_machine_hz, tstate_start,
tstate_end );
}
}
// and now the ROM block
rl.get_block( tzx, i, standard_rom_timings );
tstate_start = rl.get_block_end( i );
}
}
// last tape tstates
if( keep_unrecognised_blocks && tstate_start != tstates ) {
// the last original tape pulses and pop it in the tape
sf.get_tape_block( tzx, tstate_start, tstates );
}
write_tape( argv[1], tzx );
libspectrum_tape_free( tzx );
}
catch ( std::exception& e ) {
if( tzx ) libspectrum_tape_free( tzx );
std::cerr << progname << ": Fatal error: " << e.what() << "\n";
return 1;
}
return 0;
}
static void
write_tape( char *filename, libspectrum_tape *tape )
{
libspectrum_byte *buffer; size_t length;
FILE *f;
libspectrum_id_t type;
libspectrum_class_t cls;
int error;
/* Work out what sort of file we want from the filename; default to
.tzx if we couldn't guess */
error = libspectrum_identify_file_with_class( &type, &cls, filename, NULL,
0 );
if( error )
throw audio2tape_exception("Couldn't identify type of output file");
if( cls != LIBSPECTRUM_CLASS_TAPE || type == LIBSPECTRUM_ID_UNKNOWN )
type = LIBSPECTRUM_ID_TAPE_TZX;
length = 0;
if( libspectrum_tape_write( &buffer, &length, tape, type ) ) {
std::string err("error writing tape buffer");
throw audio2tape_exception(err.c_str());
}
f = fopen( filename, "wb" );
if( !f ) {
std::ostringstream err;
err << "couldn't open '" << filename << "': " << strerror( errno );
free( buffer );
throw audio2tape_exception(err.str().c_str());
}
if( fwrite( buffer, 1, length, f ) != length ) {
std::ostringstream err;
err << "error writing to '" << filename << "'";
free( buffer );
fclose( f );
throw audio2tape_exception(err.str().c_str());
}
free( buffer );
if( fclose( f ) ) {
std::ostringstream err;
err << "couldn't close '" << filename << "': " << strerror( errno );
throw audio2tape_exception(err.str().c_str());
}
}
void
get_pause_block( libspectrum_tape *tape, double source_machine_hz,
double start_tstates, double end_tstates )
{
if( start_tstates >= end_tstates ) return;
libspectrum_tape_block *block =
libspectrum_tape_block_alloc( LIBSPECTRUM_TAPE_BLOCK_PAUSE );
double pause_ms = ceil( ( end_tstates - start_tstates ) /
( source_machine_hz * 1000 ) );
libspectrum_tape_block_set_pause( block, pause_ms );
libspectrum_tape_append_block( tape, block );
}
static void
show_version( void )
{
std::cout <<
"audio2tape (" PACKAGE ") " PACKAGE_VERSION "\n"
"Copyright (c) 2007-2008 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 )
{
std::cout <<
"Usage: audio2tape [OPTION]... <infile> <outfile>\n"
"Converts audio files to ZX Spectrum tape images.\n"
"\n"
"Options:\n"
" -t <type> Set the level detection trigger: 'simple' or 'schmitt';\n"
" defaults to 'schmitt'.\n"
" -s Display frequency graph showing the sound levels.\n"
" -r Use idealised timing figures for ROM blocks that are\n"
" recognised.\n"
" -k Keep any audio data from the original audio file that isn't\n"
" recognised.\n"
" -z <level> Set the level to be used as zero point in the input file.\n"
" Defaults to 127 from a range of 0-255.\n"
" -c <level> Set the level to be used as as the Schmitt trigger noise\n"
" threshold. Defaults to 8 from a range of 0-127.\n"
" -h, --help Display this help and exit.\n"
" -V, --version Output version information and exit.\n"
"\n"
"Report audio2tape bugs to <" PACKAGE_BUGREPORT ">\n"
"fuse-utils home page: <" PACKAGE_URL ">\n"
"For complete documentation, see the manual page of audio2tape.\n";
}
|