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
|
/* csw.c: Routines for handling CSW raw audio files
Copyright (c) 2002-2015 Darren Salt, Fredrick Meunier
Based on tap.c, copyright (c) 2001 Philip Kendall
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: linux@youmustbejoking.demon.co.uk
*/
#include "config.h"
#include <string.h>
#include "internals.h"
#include "tape_block.h"
/* The .csw file signature (first 23 bytes) */
static const char * const csw_signature = "Compressed Square Wave\x1a";
libspectrum_error
libspectrum_csw_read( libspectrum_tape *tape,
const libspectrum_byte *buffer, size_t length )
{
libspectrum_tape_block *block = NULL;
libspectrum_tape_rle_pulse_block *csw_block;
int compressed;
size_t signature_length = strlen( csw_signature );
if( length < signature_length + 2 ) goto csw_short;
if( memcmp( csw_signature, buffer, signature_length ) ) {
libspectrum_print_error( LIBSPECTRUM_ERROR_SIGNATURE,
"libspectrum_csw_read: wrong signature" );
return LIBSPECTRUM_ERROR_SIGNATURE;
}
/* Claim memory for the block */
block = libspectrum_new( libspectrum_tape_block, 1 );
/* Set the block type */
block->type = LIBSPECTRUM_TAPE_BLOCK_RLE_PULSE;
csw_block = &block->types.rle_pulse;
buffer += signature_length;
length -= signature_length;
switch( buffer[0] ) {
case 1:
if( length < 9 ) goto csw_short;
csw_block->scale = buffer[2] | buffer[3] << 8;
if( buffer[4] != 1 ) goto csw_bad_compress;
compressed = 0;
buffer += 9;
length -= 9;
break;
case 2:
if( length < 29 ) goto csw_short;
csw_block->scale =
buffer[2] |
buffer[3] << 8 |
buffer[4] << 16 |
buffer[5] << 24;
compressed = buffer[10] - 1;
if( compressed != 0 && compressed != 1 ) goto csw_bad_compress;
if( length < 29 - buffer[12] ) goto csw_short;
length -= 29 - buffer[12];
buffer += 29 + buffer[12];
break;
default:
libspectrum_print_error( LIBSPECTRUM_ERROR_MEMORY,
"libspectrum_csw_read: unknown CSW version" );
return LIBSPECTRUM_ERROR_SIGNATURE;
}
if (csw_block->scale)
csw_block->scale = 3500000 / csw_block->scale; /* approximate CPU speed */
if( csw_block->scale < 0 || csw_block->scale >= 0x80000 ) {
libspectrum_print_error (LIBSPECTRUM_ERROR_MEMORY,
"libspectrum_csw_read: bad sample rate" );
return LIBSPECTRUM_ERROR_UNKNOWN;
}
if( !length ) goto csw_empty;
if( compressed ) {
/* Compressed data... */
#ifdef HAVE_ZLIB_H
libspectrum_error error;
csw_block->data = NULL;
csw_block->length = 0;
error = libspectrum_zlib_inflate( buffer, length, &csw_block->data,
&csw_block->length );
if( error != LIBSPECTRUM_ERROR_NONE ) return error;
#else
libspectrum_print_error( LIBSPECTRUM_ERROR_UNKNOWN,
"zlib not available to decompress gzipped file" );
return LIBSPECTRUM_ERROR_UNKNOWN;
#endif
} else {
/* Claim memory for the data (it's one big lump) */
csw_block->length = length;
csw_block->data = libspectrum_new( libspectrum_byte, length );
/* Copy the data across */
memcpy( csw_block->data, buffer, length );
}
libspectrum_tape_append_block( tape, block );
/* Successful completion */
return LIBSPECTRUM_ERROR_NONE;
/* Error returns */
csw_bad_compress:
libspectrum_free( block );
libspectrum_print_error( LIBSPECTRUM_ERROR_MEMORY,
"libspectrum_csw_read: unknown compression type" );
return LIBSPECTRUM_ERROR_CORRUPT;
csw_short:
libspectrum_free( block );
libspectrum_print_error( LIBSPECTRUM_ERROR_CORRUPT,
"libspectrum_csw_read: not enough data in buffer" );
return LIBSPECTRUM_ERROR_CORRUPT;
csw_empty:
libspectrum_free( block );
/* Successful completion */
return LIBSPECTRUM_ERROR_NONE;
}
static libspectrum_dword
find_sample_rate( libspectrum_tape *tape )
{
libspectrum_tape_iterator iterator;
libspectrum_tape_block *block;
libspectrum_dword sample_rate = 44100;
int found = 0;
/* FIXME: If tape has only one block that is a sampled type, just use it's rate
and if it RLE, we should just zlib and write */
for( block = libspectrum_tape_iterator_init( &iterator, tape );
block;
block = libspectrum_tape_iterator_next( &iterator ) )
{
switch( libspectrum_tape_block_type( block ) ) {
case LIBSPECTRUM_TAPE_BLOCK_RAW_DATA:
case LIBSPECTRUM_TAPE_BLOCK_RLE_PULSE:
{
libspectrum_dword block_rate =
3500000 / libspectrum_tape_block_bit_length( block );
if( found ) {
if( block_rate != sample_rate ) {
libspectrum_print_error(
LIBSPECTRUM_ERROR_WARNING,
"find_sample_rate: converting tape with mixed sample rates; conversion may well not work"
);
}
}
sample_rate = block_rate;
found = 1;
}
break;
case LIBSPECTRUM_TAPE_BLOCK_ROM:
case LIBSPECTRUM_TAPE_BLOCK_TURBO:
case LIBSPECTRUM_TAPE_BLOCK_PURE_TONE:
case LIBSPECTRUM_TAPE_BLOCK_PULSES:
case LIBSPECTRUM_TAPE_BLOCK_PURE_DATA:
case LIBSPECTRUM_TAPE_BLOCK_GENERALISED_DATA:
case LIBSPECTRUM_TAPE_BLOCK_PAUSE:
case LIBSPECTRUM_TAPE_BLOCK_GROUP_START:
case LIBSPECTRUM_TAPE_BLOCK_GROUP_END:
case LIBSPECTRUM_TAPE_BLOCK_JUMP:
case LIBSPECTRUM_TAPE_BLOCK_LOOP_START:
case LIBSPECTRUM_TAPE_BLOCK_LOOP_END:
case LIBSPECTRUM_TAPE_BLOCK_SELECT:
case LIBSPECTRUM_TAPE_BLOCK_STOP48:
case LIBSPECTRUM_TAPE_BLOCK_COMMENT:
case LIBSPECTRUM_TAPE_BLOCK_MESSAGE:
case LIBSPECTRUM_TAPE_BLOCK_ARCHIVE_INFO:
case LIBSPECTRUM_TAPE_BLOCK_HARDWARE:
case LIBSPECTRUM_TAPE_BLOCK_CUSTOM:
case LIBSPECTRUM_TAPE_BLOCK_CONCAT:
break;
default:
libspectrum_print_error(
LIBSPECTRUM_ERROR_LOGIC,
"find_sample_rate: unknown block type 0x%02x",
libspectrum_tape_block_type( block )
);
}
}
return sample_rate;
}
static libspectrum_error
csw_write_body( libspectrum_buffer *buffer, libspectrum_tape *tape,
libspectrum_dword sample_rate,
size_t* body_uncompressed_length )
{
libspectrum_error error;
int flags = 0;
libspectrum_dword pulse_tstates = 0;
libspectrum_dword balance_tstates = 0;
long scale = 3500000/sample_rate;
libspectrum_tape_block_state it;
if( libspectrum_tape_block_internal_init( &it, tape ) ) {
while( !(flags & LIBSPECTRUM_TAPE_FLAGS_STOP) ) {
libspectrum_dword pulse_length = 0;
/* Use internal version of this that doesn't bugger up the
external tape status */
error = libspectrum_tape_get_next_edge_internal( &pulse_tstates, &flags,
tape, &it );
if( error != LIBSPECTRUM_ERROR_NONE ) return error;
balance_tstates += pulse_tstates;
if( flags & LIBSPECTRUM_TAPE_FLAGS_NO_EDGE ) continue;
/* next RLE value is: balance_tstates / scale; */
pulse_length = balance_tstates / scale;
balance_tstates = balance_tstates % scale;
if( pulse_length ) {
if( pulse_length <= 0xff ) {
libspectrum_buffer_write_byte( buffer, pulse_length );
} else {
libspectrum_buffer_write_byte( buffer, 0 );
libspectrum_buffer_write_dword( buffer, pulse_length );
}
}
}
}
/* Write the length in */
*body_uncompressed_length = libspectrum_buffer_get_data_size( buffer );
/* compression type */
#ifdef HAVE_ZLIB_H
if( *body_uncompressed_length ) {
libspectrum_byte *compressed_data = NULL;
size_t compressed_length;
error = libspectrum_zlib_compress( libspectrum_buffer_get_data( buffer ),
libspectrum_buffer_get_data_size( buffer ),
&compressed_data, &compressed_length );
if( error ) return error;
libspectrum_buffer_clear( buffer );
libspectrum_buffer_write( buffer, compressed_data, compressed_length );
libspectrum_free( compressed_data );
}
#endif
return LIBSPECTRUM_ERROR_NONE;
}
libspectrum_error
libspectrum_csw_write( libspectrum_buffer *new_buffer, libspectrum_tape *tape )
{
libspectrum_error error = LIBSPECTRUM_ERROR_NONE;
libspectrum_dword sample_rate;
size_t i;
libspectrum_buffer* body_buffer = libspectrum_buffer_alloc();
size_t body_uncompressed_length = 0;
sample_rate = find_sample_rate( tape );
error =
csw_write_body( body_buffer, tape, sample_rate, &body_uncompressed_length );
if( error != LIBSPECTRUM_ERROR_NONE ) goto exit;
/* First, write the .csw signature and the rest of the header */
libspectrum_buffer_write( new_buffer, csw_signature, strlen( csw_signature ) );
libspectrum_buffer_write_byte( new_buffer, 2 ); /* Major version number */
libspectrum_buffer_write_byte( new_buffer, 0 ); /* Minor version number */
/* sample rate */
libspectrum_buffer_write_dword( new_buffer, sample_rate );
/* Store where the total number of pulses (after decompression) will
be written, and skip over those bytes */
libspectrum_buffer_write_dword( new_buffer, body_uncompressed_length );
/* compression type */
#ifdef HAVE_ZLIB_H
libspectrum_buffer_write_byte( new_buffer, 2 ); /* Z-RLE */
#else
libspectrum_buffer_write_byte( new_buffer, 1 ); /* RLE */
#endif
/* flags */
libspectrum_buffer_write_byte( new_buffer, 0 ); /* No flags */
/* header extension length in bytes */
libspectrum_buffer_write_byte( new_buffer, 0 ); /* No header extension */
/* encoding application description */
/* No creator for now */
for( i = 0; i < 16; i++ ) {
libspectrum_buffer_write_byte( new_buffer, 0 );
}
/* header extension data is zero so on to the data */
libspectrum_buffer_write_buffer( new_buffer, body_buffer );
exit:
libspectrum_buffer_free( body_buffer );
return error;
}
|