1 /*
2  * Copyright 2008-2013 Various Authors
3  * Copyright 2004 Timo Hirvonen
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License as
7  * published by the Free Software Foundation; either version 2 of the
8  * License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 #ifndef CMUS_NOMAD_H
20 #define CMUS_NOMAD_H
21 
22 #include <sys/types.h>
23 
24 #ifdef WIN32
25 #include <stddef.h>
26 typedef ptrdiff_t ssize_t;
27 #else
28 #ifndef __GNUC__
29 #include <unistd.h>
30 #endif
31 #endif
32 
33 /* default callbacks use read, lseek, close */
34 struct nomad_callbacks {
35 	ssize_t (*read)(void *datasource, void *buffer, size_t count);
36 	off_t (*lseek)(void *datasource, off_t offset, int whence);
37 	int (*close)(void *datasource);
38 };
39 
40 enum {
41 	XING_FRAMES = 0x00000001L,
42 	XING_BYTES  = 0x00000002L,
43 	XING_TOC    = 0x00000004L,
44 	XING_SCALE  = 0x00000008L
45 };
46 
47 typedef enum _nomad_sample_format {
48     SAMPLE_FORMAT_16_BIT_PCM = 0,
49     SAMPLE_FORMAT_32_BIT_FLOAT = 1
50 } nomad_sample_format;
51 
52 struct nomad_xing {
53 	unsigned int is_info : 1;
54 	unsigned int flags;
55 	unsigned int nr_frames;
56 	unsigned int bytes;
57 	unsigned int scale;
58 	unsigned char toc[100];
59 };
60 
61 struct nomad_lame {
62 	char encoder[10];   /* 9 byte encoder name/version ("LAME3.97b") */
63 	int vbr_method;     /* VBR method */
64 	float peak;         /* replaygain peak */
65 	float trackGain;    /* replaygain track gain */
66 	float albumGain;    /* replaygain album gain */
67 	int encoderDelay;   /* # of added samples at start of mp3 */
68 	int encoderPadding; /* # of added samples at end of mp3 */
69 };
70 
71 /* always 16-bit signed little-endian */
72 struct nomad_info {
73 	double duration;
74 	int sample_rate;
75 	int channels;
76 	int nr_frames;
77 	int layer;
78 	/* guessed */
79 	int vbr;
80 	/* guessed */
81 	int avg_bitrate;
82 	/* -1 if file not seekable */
83 	off_t filesize;
84 	unsigned int joint_stereo : 1;
85 	unsigned int dual_channel : 1;
86 };
87 
88 enum {
89 	NOMAD_ERROR_SUCCESS,
90 	NOMAD_ERROR_ERRNO,
91 	NOMAD_ERROR_FILE_FORMAT
92 };
93 
94 struct nomad;
95 
96 /* -NOMAD_ERROR_ERRNO -NOMAD_ERROR_FILE_FORMAT */
97 int nomad_open_callbacks(struct nomad **nomadp, void *datasource,
98 		struct nomad_callbacks *cbs);
99 
100 void nomad_close(struct nomad *nomad);
101 
102 /* -NOMAD_ERROR_ERRNO */
103 int nomad_read(struct nomad *nomad, char *buffer, int count, nomad_sample_format format);
104 
105 /* -NOMAD_ERROR_ERRNO */
106 int nomad_time_seek(struct nomad *nomad, double pos);
107 
108 const struct nomad_xing *nomad_xing(struct nomad *nomad);
109 const struct nomad_lame *nomad_lame(struct nomad *nomad);
110 const struct nomad_info *nomad_info(struct nomad *nomad);
111 long nomad_current_bitrate(struct nomad *nomad);
112 
113 #endif
114