1 /* See COPYING file for copyright and license details. */
2 
3 #include <sndfile.h>
4 #include <stdlib.h>
5 #include <string.h>
6 
7 #include "ebur128.h"
8 
main(int ac,const char * av[])9 int main(int ac, const char* av[]) {
10   SF_INFO file_info;
11   SNDFILE* file;
12   sf_count_t nr_frames_read;
13   ebur128_state** sts = NULL;
14   double* buffer;
15   double loudness;
16   int i;
17 
18   if (ac < 2) {
19     fprintf(stderr, "usage: %s FILENAME...\n", av[0]);
20     exit(1);
21   }
22 
23   sts = malloc((size_t) (ac - 1) * sizeof(ebur128_state*));
24   if (!sts) {
25     fprintf(stderr, "malloc failed\n");
26     return 1;
27   }
28 
29   for (i = 0; i < ac - 1; ++i) {
30     memset(&file_info, '\0', sizeof(file_info));
31     file = sf_open(av[i + 1], SFM_READ, &file_info);
32     if (!file) {
33       fprintf(stderr, "Could not open file with sf_open!\n");
34       return 1;
35     }
36 
37     sts[i] = ebur128_init((unsigned) file_info.channels,
38                           (unsigned) file_info.samplerate, EBUR128_MODE_I);
39     if (!sts[i]) {
40       fprintf(stderr, "Could not create ebur128_state!\n");
41       return 1;
42     }
43 
44     /* example: set channel map (note: see ebur128.h for the default map) */
45     if (file_info.channels == 5) {
46       ebur128_set_channel(sts[i], 0, EBUR128_LEFT);
47       ebur128_set_channel(sts[i], 1, EBUR128_RIGHT);
48       ebur128_set_channel(sts[i], 2, EBUR128_CENTER);
49       ebur128_set_channel(sts[i], 3, EBUR128_LEFT_SURROUND);
50       ebur128_set_channel(sts[i], 4, EBUR128_RIGHT_SURROUND);
51     }
52 
53     buffer = (double*) malloc(sts[i]->samplerate * sts[i]->channels *
54                               sizeof(double));
55     if (!buffer) {
56       fprintf(stderr, "malloc failed\n");
57       return 1;
58     }
59 
60     while ((nr_frames_read = sf_readf_double(
61                 file, buffer, (sf_count_t) sts[i]->samplerate))) {
62       ebur128_add_frames_double(sts[i], buffer, (size_t) nr_frames_read);
63     }
64 
65     ebur128_loudness_global(sts[i], &loudness);
66     fprintf(stderr, "%.2f LUFS, %s\n", loudness, av[i + 1]);
67 
68     free(buffer);
69     buffer = NULL;
70 
71     if (sf_close(file)) {
72       fprintf(stderr, "Could not close input file!\n");
73     }
74   }
75 
76   ebur128_loudness_global_multiple(sts, (size_t) ac - 1, &loudness);
77   fprintf(stderr, "-----------\n%.2f LUFS\n", loudness);
78 
79   /* clean up */
80   for (i = 0; i < ac - 1; ++i) {
81     ebur128_destroy(&sts[i]);
82   }
83   free(sts);
84 
85   return 0;
86 }
87