diff options
-rw-r--r-- | .gitignore | 1 | ||||
-rw-r--r-- | Makefile | 7 | ||||
-rw-r--r-- | cue-bin-split.c | 124 | ||||
-rw-r--r-- | cue-bin-split.h | 34 | ||||
-rw-r--r-- | timestamp.c | 35 | ||||
-rw-r--r-- | timestamp.h | 35 |
6 files changed, 199 insertions, 37 deletions
@@ -4,3 +4,4 @@ track_* *.bin *.cue *.sh +*.raw @@ -3,14 +3,17 @@ .POSIX: include config.mk -OBJECTS = cue-bin-split.o +OBJECTS = \ + cue-bin-split.o \ + timestamp.o + CFLAGS += -Wall -Werror all: cue-bin-split cue-bin-split: $(OBJECTS) - $(CC) -o $(EXEC_NAME) $< $(LDFLAGS) + $(CC) -o $(EXEC_NAME) $^ $(LDFLAGS) %.o: %.c $(CC) -c -o $@ $< $(CFLAGS) diff --git a/cue-bin-split.c b/cue-bin-split.c index 0a1e7ab..81ed7da 100644 --- a/cue-bin-split.c +++ b/cue-bin-split.c @@ -28,48 +28,103 @@ #include <stdio.h> #include <stdlib.h> #include <assert.h> +#include <getopt.h> + +#include "cue-bin-split.h" +#include "timestamp.h" + + +/* FIXME move me */ +void die_help() +{ + fprintf(stderr, + "Options: \n" + " -r bitrate_Hz\n" + " -c channel_count\n" + " -i input_file\n" + " -s size of a single channel's sample (bytes)\n" + " -f name_format (%%d and co are replaced with track number)\n" + ); + exit(EXIT_FAILURE); +} int main(int argc, char **argv) { + /* File handles */ FILE *fin = NULL; FILE *fout = NULL; + + /* Command line options */ char *format = NULL; char *in_fname = NULL; - char out_fname[] = "track-000000000000"; /* That should do it */ - int m = 0; - int s = 0; - int frame = 0; - int index = 0; - int items = 0; int channels = 0; int rate = 0; int sample_size = 0; + + /* Misc */ + char out_fname[] = "track-000000000000"; /* That should do it */ + int index = 0; + int items = 0; int i = 0; - char buffer[10240]; - double start = 0; - double finish = 0; + char buffer[BUFFER_SIZE]; + + /* Timestamp building blocks */ + int mm = 0; + int ss = 0; + int ff = 0; + + /* Boundaries */ + double start_sec = 0; + double finish_sec = 0; unsigned long start_sample = 0; unsigned long finish_sample = 0; - /* FIXME Use getopt */ - /* FIXME Replace assertions with useful checks+messages */ - assert(argc == 6); + /* FIXME move me */ + char c; + + while ( ( c = getopt(argc, argv, "r:c:i:s:f:") ) != -1 ) + { + switch (c) + { + case 'r': + rate = atoi(optarg); + break; + + case 'c': + channels = atoi(optarg); + break; + + case 'i': + in_fname = optarg; + break; + + case 's': + sample_size = atoi(optarg); + break; + + case 'f': + format = optarg; + break; + + case '?': + default: + die_help(); + break; + } + } - in_fname = argv[1]; - channels = atoi(argv[2]); - rate = atoi(argv[3]); - sample_size = atoi(argv[4]); - format = argv[5]; - assert(channels > 0); - assert(rate > 0); - assert(sample_size > 0); - assert(in_fname != NULL); - assert(format != NULL); + if (channels <= 0 || + rate <= 0 || + sample_size <= 0) + { + fprintf(stderr, "Channel count, bitrate and sample size must all be positive\n"); + return EXIT_FAILURE; + } /* Open it up */ - if ((fin = fopen(in_fname, "r")) == NULL) + if ((fin = fopen(in_fname, "r")) == NULL) { fprintf(stderr,"Failed to open '%s': ", in_fname); perror("fopen"); @@ -79,11 +134,10 @@ int main(int argc, char **argv) index = 0; - items = fscanf(stdin, "%d:%d:%d\n", &m, &s, &frame); /* FIXME doesn't check return value */ - start = (double)m*60 + (double)s + ((double)frame)/75; + items = get_stamp(&mm, &ss, &ff); /* FIXME doesn't check return value */ + start_sec = (double)mm*60 + (double)ss + ((double)ff)/FRAMES_PER_SEC; - /* FIXME duplication of code to get mm:ss:ff */ - while ( ( items = fscanf(stdin, "%d:%d:%d\n", &m, &s, &frame) ) ) + while ( ( items = get_stamp(&mm, &ss, &ff) ) ) { index++; @@ -109,7 +163,7 @@ int main(int argc, char **argv) /* EOF means this is the last track; run it to end of input file */ if (items == EOF) { - finish = -1; + finish_sec = -1; } else { /* Following track starts at end of last */ if (items != 3) @@ -117,18 +171,18 @@ int main(int argc, char **argv) fprintf(stderr, "Timestamp #%d malformed\n", index); break; } - finish = (double)m*60 + (double)s + ((double)frame)/75; + finish_sec = (double)mm*60 + (double)ss + ((double)ff)/75; } - fprintf(stderr, "Track %d starts %f s, finishes %f s\n", index, start, finish); + fprintf(stderr, "Track %d starts %f s, finishes %f s\n", index, start_sec, finish_sec); - start_sample = start * rate * channels; - finish_sample = finish * rate * channels; + start_sample = start_sec * rate * channels; + finish_sample = finish_sec * rate * channels; /* Seek to first sample of track */ fseek(fin, (start_sample * sample_size), SEEK_SET); - if (finish >= 0) + if (finish_sec >= 0) { for (i = (int)start_sample; i < (int)finish_sample; i += items) { @@ -155,9 +209,9 @@ int main(int argc, char **argv) } fclose(fout); - start = finish; + start_sec = finish_sec; - if (finish < 0) + if (finish_sec < 0) break; } return 0; diff --git a/cue-bin-split.h b/cue-bin-split.h new file mode 100644 index 0000000..1c30ea9 --- /dev/null +++ b/cue-bin-split.h @@ -0,0 +1,34 @@ +/* + * Part of cue-bin-split - Splits raw PCM files on time boundaries + * Copyright (c) 2015 David Phillips <dbphillipsnz@gmail.com> + * All rights reserved + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#ifndef CUE_BIN_SPLIT_H +#define CUE_BIN_SPLIT_H + +#define FRAMES_PER_SEC 75 +#define BUFFER_SIZE 1024*1024 /* Meh, good enough */ + +#endif diff --git a/timestamp.c b/timestamp.c new file mode 100644 index 0000000..44f793b --- /dev/null +++ b/timestamp.c @@ -0,0 +1,35 @@ +/* + * Part of cue-bin-split - Splits raw PCM files on time boundaries + * Copyright (c) 2015 David Phillips <dbphillipsnz@gmail.com> + * All rights reserved + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include "timestamp.h" + +int get_stamp(int *m, int *s, int *f) +{ + int foo = 0; + foo = fscanf(stdin, "%d:%d:%d\n", m, s, f); + return foo; +} diff --git a/timestamp.h b/timestamp.h new file mode 100644 index 0000000..4706189 --- /dev/null +++ b/timestamp.h @@ -0,0 +1,35 @@ +/* + * Part of cue-bin-split - Splits raw PCM files on time boundaries + * Copyright (c) 2015 David Phillips <dbphillipsnz@gmail.com> + * All rights reserved + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#ifndef CUE_BIN_SPLIT_TIMESTAMP_H +#define CUE_BIN_SPLIT_TIMESTAMP_H + +#include <stdio.h> + +int get_stamp(int *m, int *s, int *f); + +#endif |