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