1/*
2** Copyright (C) 2008-2014 Erik de Castro Lopo <erikd@mega-nerd.com>
3** Copyright (C) 2008-2010 George Blood Audio
4**
5** All rights reserved.
6**
7** Redistribution and use in source and binary forms, with or without
8** modification, are permitted provided that the following conditions are
9** met:
10**
11**     * Redistributions of source code must retain the above copyright
12**       notice, this list of conditions and the following disclaimer.
13**     * Redistributions in binary form must reproduce the above copyright
14**       notice, this list of conditions and the following disclaimer in
15**       the documentation and/or other materials provided with the
16**       distribution.
17**     * Neither the author nor the names of any contributors may be used
18**       to endorse or promote products derived from this software without
19**       specific prior written permission.
20**
21** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
23** TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24** PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
25** CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
26** EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
27** PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
28** OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29** WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
30** OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
31** ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32*/
33
34#include <config.h>
35
36#include <stdio.h>
37#include <stdlib.h>
38#include <string.h>
39#include <ctype.h>
40#include <time.h>
41
42#include <sndfile.h>
43
44#include "common.h"
45
46#define	BUFFER_LEN		(1 << 16)
47
48static void usage_exit (const char *progname, int exit_code) ;
49static void process_args (SNDFILE * file, const SF_BROADCAST_INFO_2K * binfo, int argc, char * argv []) ;
50
51int
52main (int argc, char *argv [])
53{	SNDFILE *file ;
54	SF_INFO sfinfo ;
55	SF_BROADCAST_INFO_2K binfo ;
56	const char *progname ;
57	const char * filename = NULL ;
58	int	start ;
59
60	/* Store the program name. */
61	progname = program_name (argv [0]) ;
62
63	/* Check if we've been asked for help. */
64	if (argc < 2 || strcmp (argv [1], "--help") == 0 || strcmp (argv [1], "-h") == 0)
65		usage_exit (progname, 0) ;
66
67	if (argv [argc - 1][0] != '-')
68	{	filename = argv [argc - 1] ;
69		start = 1 ;
70		}
71	else if (argv [1][0] != '-')
72	{	filename = argv [1] ;
73		start = 2 ;
74		}
75	else
76	{	printf ("Error : Either the first or the last command line parameter should be a filename.\n\n") ;
77		exit (1) ;
78		} ;
79
80	memset (&sfinfo, 0, sizeof (sfinfo)) ;
81	if ((file = sf_open (filename, SFM_READ, &sfinfo)) == NULL)
82	{	printf ("Error : Open of file '%s' failed : %s\n\n", filename, sf_strerror (file)) ;
83		exit (1) ;
84		} ;
85
86	memset (&binfo, 0, sizeof (binfo)) ;
87	if (sf_command (file, SFC_GET_BROADCAST_INFO, &binfo, sizeof (binfo)) == 0)
88		memset (&binfo, 0, sizeof (binfo)) ;
89
90	process_args (file, &binfo, argc - 2, argv + start) ;
91
92	sf_close (file) ;
93	return 0 ;
94} /* main */
95
96/*==============================================================================
97**	Print version and usage.
98*/
99
100static void
101usage_exit (const char *progname, int exit_code)
102{	printf ("\nUsage :\n  %s [options] <file>\n\nOptions:\n", progname) ;
103
104	puts (
105		"    --bext-description     Print the 'bext' description.\n"
106		"    --bext-originator      Print the 'bext' originator info.\n"
107		"    --bext-orig-ref        Print the 'bext' origination reference.\n"
108		"    --bext-umid            Print the 'bext' UMID.\n"
109		"    --bext-orig-date       Print the 'bext' origination date.\n"
110		"    --bext-orig-time       Print the 'bext' origination time.\n"
111		"    --bext-loudness-value  Print the 'bext' loudness value.\n"
112		"    --bext-loudness-range  Print the 'bext' loudness range.\n"
113		"    --bext-max-truepeak    Print the 'bext' max. true peak level\n"
114		"    --bext-max-momentary   Print the 'bext' max. momentary loudness\n"
115		"    --bext-max-shortterm   Print the 'bext' max. short term loudness\n"
116		"    --bext-coding-hist     Print the 'bext' coding history.\n"
117		) ;
118
119	puts (
120		"    --str-title            Print the title metadata.\n"
121		"    --str-copyright        Print the copyright metadata.\n"
122		"    --str-artist           Print the artist metadata.\n"
123		"    --str-comment          Print the comment metadata.\n"
124		"    --str-date             Print the creation date metadata.\n"
125		"    --str-album            Print the album metadata.\n"
126		"    --str-license          Print the license metadata.\n"
127		) ;
128
129	printf ("Using %s.\n\n", sf_version_string ()) ;
130	exit (exit_code) ;
131} /* usage_exit */
132
133static void
134process_args (SNDFILE * file, const SF_BROADCAST_INFO_2K * binfo, int argc, char * argv [])
135{	const char * str ;
136	int k, do_all = 0 ;
137
138#define HANDLE_BEXT_ARG(cmd, name, field) \
139		if (do_all || strcmp (argv [k], cmd) == 0) \
140		{	printf ("%-22s : %.*s\n", name, (int) sizeof (binfo->field), binfo->field) ; \
141			if (! do_all) \
142				continue ; \
143			} ;
144
145#define HANDLE_BEXT_ARG_INT(cmd, name, field) \
146		if (do_all || strcmp (argv [k], cmd) == 0) \
147		{	printf ("%-22s : %6.2f\n", name, binfo->field / 100.0) ; \
148			if (! do_all) \
149				continue ; \
150			} ;
151
152#define HANDLE_STR_ARG(cmd, name, id) \
153		if (do_all || strcmp (argv [k], cmd) == 0) \
154		{	str = sf_get_string (file, id) ; \
155			printf ("%-22s : %s\n", name, str ? str : "") ; \
156			if (! do_all) continue ; \
157			} ;
158
159	if (argc == 0)
160	{	do_all = 1 ;
161		argc = 1 ;
162		} ;
163
164	for (k = 0 ; k < argc ; k++)
165	{	if (do_all || strcmp (argv [k], "--all") == 0)
166			do_all = 1 ;
167
168		HANDLE_BEXT_ARG ("--bext-description", "Description", description) ;
169		HANDLE_BEXT_ARG ("--bext-originator", "Originator", originator) ;
170		HANDLE_BEXT_ARG ("--bext-orig-ref", "Origination ref", originator_reference) ;
171		HANDLE_BEXT_ARG ("--bext-umid", "UMID", umid) ;
172		HANDLE_BEXT_ARG ("--bext-orig-date", "Origination date", origination_date) ;
173		HANDLE_BEXT_ARG ("--bext-orig-time", "Origination time", origination_time) ;
174		HANDLE_BEXT_ARG_INT ("--bext-loudness-value", "Loudness value", loudness_value) ;
175		HANDLE_BEXT_ARG_INT ("--bext-loudness-range", "Loudness range", loudness_range) ;
176		HANDLE_BEXT_ARG_INT ("--bext-max-truepeak", "Max. true peak level", max_true_peak_level) ;
177		HANDLE_BEXT_ARG_INT ("--bext-max-momentary", "Max. momentary level", max_momentary_loudness) ;
178		HANDLE_BEXT_ARG_INT ("--bext-max-shortterm", "Max. short term level", max_shortterm_loudness) ;
179		HANDLE_BEXT_ARG ("--bext-coding-hist", "Coding history", coding_history) ;
180
181		HANDLE_STR_ARG ("--str-title", "Name", SF_STR_TITLE) ;
182		HANDLE_STR_ARG ("--str-copyright", "Copyright", SF_STR_COPYRIGHT) ;
183		HANDLE_STR_ARG ("--str-artist", "Artist", SF_STR_ARTIST) ;
184		HANDLE_STR_ARG ("--str-comment", "Comment", SF_STR_COMMENT) ;
185		HANDLE_STR_ARG ("--str-date", "Create date", SF_STR_DATE) ;
186		HANDLE_STR_ARG ("--str-album", "Album", SF_STR_ALBUM) ;
187		HANDLE_STR_ARG ("--str-license", "License", SF_STR_LICENSE) ;
188
189		if (! do_all)
190		{	printf ("Error : Don't know what to do with command line arg '%s'.\n\n", argv [k]) ;
191			exit (1) ;
192			} ;
193		break ;
194		} ;
195
196	return ;
197} /* process_args */
198