|
| 1 | +#include <stdlib.h> |
| 2 | +#include <string.h> |
| 3 | + |
| 4 | +#include "../usf/usf.h" |
| 5 | + |
| 6 | +#include <psflib.h> |
| 7 | + |
| 8 | +unsigned char * state = 0; |
| 9 | + |
| 10 | +unsigned int enable_compare = 0; |
| 11 | +unsigned int enable_fifo_full = 0; |
| 12 | + |
| 13 | +unsigned long length_ms = 0; |
| 14 | + |
| 15 | +static void * stdio_fopen( const char * path ) |
| 16 | +{ |
| 17 | + return fopen( path, "rb" ); |
| 18 | +} |
| 19 | + |
| 20 | +static size_t stdio_fread( void *p, size_t size, size_t count, void *f ) |
| 21 | +{ |
| 22 | + return fread( p, size, count, (FILE*) f ); |
| 23 | +} |
| 24 | + |
| 25 | +static int stdio_fseek( void * f, int64_t offset, int whence ) |
| 26 | +{ |
| 27 | + return fseek( (FILE*) f, offset, whence ); |
| 28 | +} |
| 29 | + |
| 30 | +static int stdio_fclose( void * f ) |
| 31 | +{ |
| 32 | + return fclose( (FILE*) f ); |
| 33 | +} |
| 34 | + |
| 35 | +static long stdio_ftell( void * f ) |
| 36 | +{ |
| 37 | + return ftell( (FILE*) f ); |
| 38 | +} |
| 39 | + |
| 40 | +static psf_file_callbacks stdio_callbacks = |
| 41 | +{ |
| 42 | + "\\/:", |
| 43 | + stdio_fopen, |
| 44 | + stdio_fread, |
| 45 | + stdio_fseek, |
| 46 | + stdio_fclose, |
| 47 | + stdio_ftell |
| 48 | +}; |
| 49 | + |
| 50 | +static int usf_loader(void * context, const uint8_t * exe, size_t exe_size, |
| 51 | + const uint8_t * reserved, size_t reserved_size) |
| 52 | +{ |
| 53 | + if ( exe && exe_size > 0 ) return -1; |
| 54 | + |
| 55 | + return usf_upload_section( state, reserved, reserved_size ); |
| 56 | +} |
| 57 | + |
| 58 | +#define BORK_TIME 0xC0CAC01A |
| 59 | + |
| 60 | +static unsigned long parse_time_crap(const char *input) |
| 61 | +{ |
| 62 | + unsigned long value = 0; |
| 63 | + unsigned long multiplier = 1000; |
| 64 | + const char * ptr = input; |
| 65 | + unsigned long colon_count = 0; |
| 66 | + |
| 67 | + while (*ptr && ((*ptr >= '0' && *ptr <= '9') || *ptr == ':')) |
| 68 | + { |
| 69 | + colon_count += *ptr == ':'; |
| 70 | + ++ptr; |
| 71 | + } |
| 72 | + if (colon_count > 2) return BORK_TIME; |
| 73 | + if (*ptr && *ptr != '.' && *ptr != ',') return BORK_TIME; |
| 74 | + if (*ptr) ++ptr; |
| 75 | + while (*ptr && *ptr >= '0' && *ptr <= '9') ++ptr; |
| 76 | + if (*ptr) return BORK_TIME; |
| 77 | + |
| 78 | + ptr = strrchr(input, ':'); |
| 79 | + if (!ptr) |
| 80 | + ptr = input; |
| 81 | + for (;;) |
| 82 | + { |
| 83 | + char * end; |
| 84 | + if (ptr != input) ++ptr; |
| 85 | + if (multiplier == 1000) |
| 86 | + { |
| 87 | + double temp = strtod(ptr, &end); |
| 88 | + if (temp >= 60.0) return BORK_TIME; |
| 89 | + value = (long)(temp * 1000.0f); |
| 90 | + } |
| 91 | + else |
| 92 | + { |
| 93 | + unsigned long temp = strtoul(ptr, &end, 10); |
| 94 | + if (temp >= 60 && multiplier < 3600000) return BORK_TIME; |
| 95 | + value += temp * multiplier; |
| 96 | + } |
| 97 | + if (ptr == input) break; |
| 98 | + ptr -= 2; |
| 99 | + while (ptr > input && *ptr != ':') --ptr; |
| 100 | + multiplier *= 60; |
| 101 | + } |
| 102 | + |
| 103 | + return value; |
| 104 | +} |
| 105 | + |
| 106 | +static int usf_info(void * context, const char * name, const char * value) |
| 107 | +{ |
| 108 | + if (!strcasecmp(name, "length") || !strcasecmp(name, "fade")) |
| 109 | + length_ms += parse_time_crap(value); |
| 110 | + else if (!strcasecmp(name, "_enablecompare") && *value) |
| 111 | + enable_compare = 1; |
| 112 | + else if (!strcasecmp(name, "_enablefifofull") && *value) |
| 113 | + enable_fifo_full = 1; |
| 114 | + |
| 115 | + return 0; |
| 116 | +} |
| 117 | + |
| 118 | +int main(int argc, char ** argv) |
| 119 | +{ |
| 120 | + if ( argc == 2 || argc == 3 ) |
| 121 | + { |
| 122 | + int32_t sample_rate; |
| 123 | + |
| 124 | + state = (unsigned char *) malloc(usf_get_state_size()); |
| 125 | + |
| 126 | + usf_clear(state); |
| 127 | + |
| 128 | + if ( psf_load( argv[1], &stdio_callbacks, 0x21, usf_loader, 0, usf_info, 0, 1 ) <= 0 ) |
| 129 | + return 1; |
| 130 | + |
| 131 | + usf_set_compare(state, enable_compare); |
| 132 | + usf_set_fifo_full(state, enable_fifo_full); |
| 133 | + |
| 134 | + if (argc == 3) |
| 135 | + usf_set_hle_audio(state, 1); |
| 136 | + |
| 137 | + usf_render(state, 0, 0, &sample_rate); |
| 138 | + |
| 139 | + usf_render(state, 0, length_ms * sample_rate / 1000, &sample_rate); |
| 140 | + |
| 141 | + usf_shutdown(state); |
| 142 | + |
| 143 | + free(state); |
| 144 | + } |
| 145 | + |
| 146 | + return 0; |
| 147 | +} |
0 commit comments