Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Update code

  • Loading branch information...
commit 429772c1ff40b3c967ce687cfe06c21b4212e94b 1 parent d7a91d3
@mtgrosser mtgrosser authored
Showing with 188 additions and 176 deletions.
  1. +149 −155 src/bin/main.c
  2. +1 −0  src/bin/main.h
  3. +36 −20 src/bin/wave.c
  4. +2 −1  src/bin/wave.h
View
304 src/bin/main.c
@@ -34,189 +34,183 @@
char *infname, *outfname;
FILE *infile, *outfile;
int quiet = 0;
+int _verbose = 0;
-/* Routine we tell libshine-fxp to call to write out the MP3 file */
-int write_mp3(long bytes, void *buffer, void *config) {
- return fwrite(buffer, sizeof(unsigned char), bytes, outfile);
+int verbose()
+{
+ return _verbose;
}
+/* Write out the MP3 file */
+int write_mp3(long bytes, void *buffer, void *config)
+{
+ return fwrite(buffer, sizeof(unsigned char), bytes, outfile);
+}
-/*
- * error:
- * ------
- */
+/* Output error message and exit */
void error(char *s)
{
- fprintf(stderr, "[ERROR] %s\n",s);
- exit(1);
+ fprintf(stderr, "Error: %s\n", s);
+ exit(1);
}
static void print_name()
{
- printf("shineenc (Liquidsoap version)\n");
+ printf("shineenc (Liquidsoap version)\n");
}
-/*
- * print_usage:
- * ------------
- */
static void print_usage()
{
- printf("Usage: shineenc [options] <infile> <outfile>\n\n");
- printf("Options:\n");
- printf(" -h this help message\n");
- printf(" -b <bitrate> set the bitrate [32-320], default 128kbit\n");
- printf(" -c set copyright flag, default off\n");
- printf(" -q quiet mode\n");
+ printf("Usage: shineenc [options] <infile> <outfile>\n\n");
+ printf("Options:\n");
+ printf(" -h this help message\n");
+ printf(" -b <bitrate> set the bitrate [32-320], default 128kbit\n");
+ printf(" -c set copyright flag, default off\n");
+ printf(" -q quiet mode\n");
+ printf(" -v verbose mode\n");
}
-/*
- * set_defaults:
- * -------------
- */
+/* Use these default settings, can be overridden */
static void set_defaults(shine_config_t *config)
{
- L3_set_config_mpeg_defaults(&config->mpeg);
- /* Could set overrides here, if any - see Layer3.c */
+ L3_set_config_mpeg_defaults(&config->mpeg);
}
-/*
- * parse_command line arguments
- * --------------
- */
+/* Parse command line arguments */
static int parse_command(int argc, char** argv, shine_config_t *config)
{
- int i = 0;
-
- if(argc<3) return 0;
-
- while (argv[++i][0] == '-' && argv[i][1] != '\000' && argv[i][1] != ' ')
- switch (argv[i][1])
- {
- case 'b':
- config->mpeg.bitr = atoi(argv[++i]);
- break;
-
- case 'c':
- config->mpeg.copyright = 1;
- break;
-
- case 'q':
- quiet = 1;
- break;
-
- case 'h':
- default :
- return 0;
- }
-
- if (argc - i != 2) return 0;
- infname = argv[i++];
- outfname = argv[i];
- return 1;
+ int i = 0;
+
+ if(argc < 3) return 0;
+
+ while (argv[++i][0] == '-' && argv[i][1] != '\000' && argv[i][1] != ' ')
+ switch (argv[i][1]) {
+ case 'b':
+ config->mpeg.bitr = atoi(argv[++i]);
+ break;
+
+ case 'c':
+ config->mpeg.copyright = 1;
+ break;
+
+ case 'q':
+ quiet = 1;
+ _verbose = 0;
+ break;
+
+ case 'v':
+ _verbose = 1;
+ quiet = 0;
+ break;
+
+ case 'h':
+ default :
+ return 0;
+ }
+
+ if (argc - i != 2) return 0;
+ infname = argv[i++];
+ outfname = argv[i];
+ return 1;
}
-/*
- * check_config: Print some info about what we're going to encode
- * -------------
- */
+/* Print some info about what we're going to encode */
static void check_config(shine_config_t *config)
{
- static char *mode_names[4] = { "stereo", "j-stereo", "dual-ch", "mono" };
- static char *demp_names[4] = { "none", "50/15us", "", "CITT" };
-
- printf("MPEG-I layer III, %s Psychoacoustic Model: Shine\n",
- mode_names[config->mpeg.mode]);
- printf("Bitrate=%d kbps ",config->mpeg.bitr );
- printf("De-emphasis: %s %s %s\n",
- demp_names[config->mpeg.emph],
- ((config->mpeg.original)?"Original":""),
- ((config->mpeg.copyright)?"(C)":""));
-
- printf("Encoding \"%s\" to \"%s\"\n", infname, outfname);
+ static char *mode_names[4] = { "stereo", "j-stereo", "dual-ch", "mono" };
+ static char *demp_names[4] = { "none", "50/15us", "", "CITT" };
+
+ printf("MPEG-I layer III, %s Psychoacoustic Model: Shine\n",
+ mode_names[config->mpeg.mode]);
+ printf("Bitrate: %d kbps ", config->mpeg.bitr);
+ printf("De-emphasis: %s %s %s\n",
+ demp_names[config->mpeg.emph],
+ ((config->mpeg.original) ? "Original" : ""),
+ ((config->mpeg.copyright) ? "(C)" : ""));
+ printf("Encoding \"%s\" to \"%s\"\n", infname, outfname);
}
-/*
- * main:
- * -----
- */
int main(int argc, char **argv)
{
- time_t start_time, end_time;
- int16_t buffer[2][samp_per_frame];
- shine_config_t config;
- shine_t s;
- long written;
- unsigned char *data;
-
- time(&start_time);
-
- /* Set the default MPEG encoding paramters - basically init the struct */
- set_defaults(&config);
-
- if (!parse_command(argc, argv, &config))
- {
- print_usage();
- exit(1);
- }
- quiet = quiet || !strcmp(outfname, "-");
-
- if (!quiet) print_name();
-
- /* Open the input file and fill the config shine_wave_t header */
- infile = wave_open(infname, &config, quiet);
-
- /* See if samplerate is valid */
- if (L3_find_samplerate_index(config.wave.samplerate) < 0) error("invalid samplerate");
-
- /* See if bitrate is valid */
- if (L3_find_bitrate_index(config.mpeg.bitr) < 0) error("invalid bitrate");
-
- /* open the output file */
- if (!strcmp(outfname, "-"))
- outfile = stdout;
- else
- outfile = fopen(outfname, "wb");
- if (!outfile)
- {
- fprintf(stderr, "Could not create \"%s\".\n", outfname);
- exit(1);
- }
-
- /* Set to stereo mode if wave data is stereo, mono otherwise. */
- if (config.wave.channels > 1)
- config.mpeg.mode = STEREO;
- else
- config.mpeg.mode = MONO;
-
- /* Print some info about the file about to be created (optional) */
- if (!quiet) check_config(&config);
-
- /* Initiate encoder */
- s = L3_initialise(&config);
-
- /* All the magic happens here */
- while (wave_get(buffer, infile, &config)) {
- data = L3_encode_frame(s,buffer,&written);
- write_mp3(written, data, &config);
- }
-
- /* Flush and write remaining data. */
- data = L3_flush(s,&written);
- write_mp3(written, data, &config);
-
- /* Close encoder. */
- L3_close(s);
-
- /* Close the wave file (using the wav reader) */
- wave_close(infile);
-
- /* Close the MP3 file */
- fclose(outfile);
-
- time(&end_time);
- end_time -= start_time;
- if (!quiet)
- printf(" Finished in %2ld:%2ld:%2ld\n", end_time/3600, (end_time/60)%60, end_time%60);
- exit(0);
+ wave_t wave;
+ time_t start_time, end_time;
+ int16_t buffer[2][samp_per_frame];
+ shine_config_t config;
+ shine_t s;
+ long written;
+ unsigned char *data;
+
+ time(&start_time);
+
+ /* Set the default MPEG encoding paramters - basically init the struct */
+ set_defaults(&config);
+
+ if (!parse_command(argc, argv, &config)) {
+ print_usage();
+ exit(1);
+ }
+
+ quiet = quiet || !strcmp(outfname, "-");
+
+ if (!quiet) print_name();
+
+ /* Open the input file and fill the config shine_wave_t header */
+ if (!wave_open(infname, &wave, &config, quiet))
+ error("Could not open WAVE file");
+
+ infile = wave.file;
+
+ /* See if samplerate is valid */
+ if (L3_find_samplerate_index(config.wave.samplerate) < 0) error("Unsupported samplerate");
+
+ /* See if bitrate is valid */
+ if (L3_find_bitrate_index(config.mpeg.bitr) < 0) error("Unsupported bitrate");
+
+ /* open the output file */
+ if (!strcmp(outfname, "-"))
+ outfile = stdout;
+ else
+ outfile = fopen(outfname, "wb");
+ if (!outfile) {
+ fprintf(stderr, "Could not create \"%s\".\n", outfname);
+ exit(1);
+ }
+
+ /* Set to stereo mode if wave data is stereo, mono otherwise. */
+ if (config.wave.channels > 1)
+ config.mpeg.mode = STEREO;
+ else
+ config.mpeg.mode = MONO;
+
+ /* Print some info about the file about to be created (optional) */
+ if (!quiet) check_config(&config);
+
+ /* Initiate encoder */
+ s = L3_initialise(&config);
+
+ /* All the magic happens here */
+ while (wave_get(buffer, &wave, &config)) {
+ data = L3_encode_frame(s, buffer, &written);
+ write_mp3(written, data, &config);
+ }
+
+ /* Flush and write remaining data. */
+ data = L3_flush(s, &written);
+ write_mp3(written, data, &config);
+
+ /* Close encoder. */
+ L3_close(s);
+
+ /* Close the wave file (using the wav reader) */
+ wave_close(&wave);
+
+ /* Close the MP3 file */
+ fclose(outfile);
+
+ time(&end_time);
+ end_time -= start_time;
+ if (!quiet)
+ printf("Finished in %02ld:%02ld:%02ld (%01.1fx realtime)\n", end_time / 3600, (end_time / 60) % 60, end_time % 60, (float)wave.duration / (float)end_time);
+
+ return 0;
}
View
1  src/bin/main.h
@@ -2,5 +2,6 @@
#define MAIN_H
void error(char *s);
+int verbose();
#endif
View
56 src/bin/wave.c
@@ -38,32 +38,44 @@ typedef struct {
uint32_t byte_rate; /* bytes per second = samp_rate * byte_samp = 176400 */
uint16_t frame_size; /* block align (bytes per sample) = channels * bits_per_sample / 8 = 4 */
uint16_t depth; /* bits per sample = 16 for MS PCM (format specific) */
- uint16_t extra_length; /* optional extra data */
} fmt_chunk_t;
-struct data_chunk {
- riff_chunk_header_t header;
- char data[];
-}
-bool wave_get_chunk_header(FILE *file, const char* id, riff_chunk_header_t *header)
+bool wave_get_chunk_header(FILE *file, const char id[4], riff_chunk_header_t *header)
{
- bool found;
+ bool found = false;
+ uint32_t chunk_length;
+ long i;
+ char chunk_id[5] = "\0\0\0\0\0";
+
+ if (verbose())
+ fprintf(stderr, "Looking for chunk '%s'\n", id);
while(!found) {
- if (fread(header, sizeof(header), 1, file) != 1) {
+ if (fread(header, sizeof(riff_chunk_header_t), 1, file) != 1) {
if (feof(file))
return false;
else
error("Read error");
+ }
+
+ /* chunks must be word-aligned, chunk data doesn't need to */
+ chunk_length = header->length + header->length % 2;
+ if (verbose()) {
+ memcpy(chunk_id, &header->id, 4);
+ fprintf(stderr, "Found chunk '%s', length: %u\n", chunk_id, header->length);
+ }
- if (strncmp(header.id, id, 4) == 0)
+ if (strncmp(header->id, id, 4) == 0)
return true;
- for (long i = 0; i < (header.length - sizeof(header), i++)
+ /* move forward */
+ for (i = 0; i < chunk_length; i++)
getc(file);
}
+
+ return false;
}
@@ -79,10 +91,9 @@ void wave_close(wave_t *wave)
* Opens and verifies the header of the Input Wave file. The file pointer is
* left pointing to the start of the samples.
*/
-bool wave_open(const char *fname, wave_t *wave, shine_config_t *config, int quiet, long *data_length)
+bool wave_open(const char *fname, wave_t *wave, shine_config_t *config, int quiet)
{
static char *channel_mappings[] = { NULL, "mono", "stereo" };
- uint32_t duration;
wave_chunk_t wave_chunk;
fmt_chunk_t fmt_chunk;
riff_chunk_header_t data_chunk;
@@ -106,13 +117,16 @@ bool wave_open(const char *fname, wave_t *wave, shine_config_t *config, int quie
error("Not a WAVE audio file");
/* Check the fmt chunk */
- if (!wave_get_chunk_header(wave->file, 'fmt ', &fmt_chunk))
+ if (!wave_get_chunk_header(wave->file, "fmt ", (riff_chunk_header_t *)&fmt_chunk))
error("WAVE fmt chunk not found");
- if(fread(&fmt_chunk + sizeof(riff_chunk_header_t),
- sizeof(fmt_chunk) - sizeof(riff_chunk_header_t), 1, wave->file) != 1)
+ if(fread(&fmt_chunk.format,
+ sizeof(fmt_chunk_t) - sizeof(riff_chunk_header_t), 1, wave->file) != 1)
error("Read error");
+ if (verbose())
+ fprintf(stderr, "WAVE format: %u\n", fmt_chunk.format);
+
if (fmt_chunk.format != 1)
error("Unknown WAVE format");
@@ -123,17 +137,19 @@ bool wave_open(const char *fname, wave_t *wave, shine_config_t *config, int quie
error("Unsupported PCM bit depth");
/* Position the file pointer at the data chunk */
- if (!wave_get_chunk_header(wave->file, 'data', &data_chunk))
+ if (!wave_get_chunk_header(wave->file, "data", &data_chunk))
error("WAVE data chunk not found");
config->wave.channels = fmt_chunk.channels;
config->wave.samplerate = fmt_chunk.sample_rate;
- duration = data_chunk.length / fmt_chunk.byte_rate;
+
+ wave->length = data_chunk.length;
+ wave->duration = data_chunk.length / fmt_chunk.byte_rate;
if (!quiet)
- printf("%s, %s %ldHz %ldbit, Length: %2ld:%2ld:%2ld\n",
- "WAV PCM DATA", channel_mappings[fmt_chunk.channels], (long)fmt_chunk.sample_rate, (long)fmt_chunk.depth,
- (long)duration / 3600, (long)(duration / 60) % 60, (long)duration % 60);
+ printf("%s, %s %ldHz %ldbit, duration: %02ld:%02ld:%02ld\n",
+ "WAVE PCM Data", channel_mappings[fmt_chunk.channels], (long)fmt_chunk.sample_rate, (long)fmt_chunk.depth,
+ (long)wave->duration / 3600, (long)(wave->duration / 60) % 60, (long)wave->duration % 60);
return true;
}
View
3  src/bin/wave.h
@@ -7,9 +7,10 @@
typedef struct {
FILE *file;
long length;
+ long duration;
} wave_t;
-bool wave_open(const char *fname, wave_t *wave, shine_config_t *config, int quiet, long *data_length);
+bool wave_open(const char *fname, wave_t *wave, shine_config_t *config, int quiet);
int wave_get(int16_t buffer[2][samp_per_frame], wave_t *wave, void *config_in);
void wave_close(wave_t *wave);
Please sign in to comment.
Something went wrong with that request. Please try again.