1 /*
2 
3 Boost Software License - Version 1.0 - August 17th, 2003
4 
5 Permission is hereby granted, free of charge, to any person or organization
6 obtaining a copy of the software and accompanying documentation covered by
7 this license (the "Software") to use, reproduce, display, distribute,
8 execute, and transmit the Software, and to prepare derivative works of the
9 Software, and to permit third-parties to whom the Software is furnished to
10 do so, all subject to the following:
11 
12 The copyright notices in the Software and this entire statement, including
13 the above license grant, this restriction and the following disclaimer,
14 must be included in all copies of the Software, in whole or in part, and
15 all derivative works of the Software, unless such copies or derivative
16 works are solely in the form of machine-executable object code generated by
17 a source language processor.
18 
19 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
22 SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
23 FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
24 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25 DEALINGS IN THE SOFTWARE.
26 
27 */
28 module derelict.vorbis.file;
29 
30 private {
31     import core.stdc.config;
32     import std.stdio;
33     import derelict.util.loader;
34     import derelict.util.system;
35     import derelict.ogg.ogg;
36     import derelict.vorbis.codec;
37 
38     static if(Derelict_OS_Windows)
39         enum libNames = "vorbisfile.dll, libvorbisfile-3.dll, libvorbisfile.dll";
40     else static if(Derelict_OS_Mac)
41         enum libNames = "libvorbisfile.dylib, libvorbisfile.0.dylib";
42     else static if(Derelict_OS_Posix)
43         enum libNames = "libvorbisfile.so, libvorbisfile.so.3, libvorbisfile.so.3.1.0";
44     else
45         static assert(0, "Need to implement libvorbisfile libnames for this operating system.");
46 }
47 
48 struct ov_callbacks {
49     extern(C) nothrow {
50         size_t function(void*, size_t, size_t, void*) read_func;
51         int function(void*, ogg_int64_t, int) seek_func;
52         int function(void*) close_func;
53         c_long function(void*) tell_func;
54     }
55 }
56 
57 enum {
58     NOTOPEN =0,
59     PARTOPEN =1,
60     OPENED =2,
61     STREAMSET =3,
62     INITSET =4,
63 }
64 
65 struct OggVorbis_File {
66     void* datasource;
67     int seekable;
68     ogg_int64_t offset;
69     ogg_int64_t end;
70     ogg_sync_state oy;
71     int links;
72     ogg_int64_t* offsets;
73     ogg_int64_t* dataoffsets;
74     c_long* serialnos;
75     ogg_int64_t* pcmlengths;
76     vorbis_info* vi;
77     vorbis_comment* vc;
78     ogg_int64_t pcm_offset;
79     int ready_state;
80     c_long current_serialno;
81     int current_link;
82     double bittrack;
83     double samptrack;
84     ogg_stream_state os;
85     vorbis_dsp_state vd;
86     vorbis_block vb;
87     ov_callbacks callbacks;
88 }
89 
90 extern(C) @nogc nothrow {
91     alias da_ov_clear = int function(OggVorbis_File*);
92     alias da_ov_fopen = int function(const(char)*, OggVorbis_File*);
93     alias da_ov_open_callbacks = int function(void* datasource, OggVorbis_File*, const(char)*, c_long, ov_callbacks);
94     alias da_ov_test_callbacks = int function(void*, OggVorbis_File*, const(char)*, c_long, ov_callbacks);
95     alias da_ov_test_open = int function(OggVorbis_File*);
96     alias da_ov_bitrate = c_long function(OggVorbis_File*, int);
97     alias da_ov_bitrate_instant = c_long function(OggVorbis_File*);
98     alias da_ov_streams = c_long function(OggVorbis_File*);
99     alias da_ov_seekable = c_long function(OggVorbis_File*);
100     alias da_ov_serialnumber = c_long function(OggVorbis_File*, int);
101     alias da_ov_raw_total = ogg_int64_t function(OggVorbis_File*, int);
102     alias da_ov_pcm_total = ogg_int64_t function(OggVorbis_File*, int);
103     alias da_ov_time_total = double function(OggVorbis_File*, int);
104     alias da_ov_raw_seek = int function(OggVorbis_File*, ogg_int64_t);
105     alias da_ov_pcm_seek = int function(OggVorbis_File*, ogg_int64_t);
106     alias da_ov_pcm_seek_page = int function(OggVorbis_File*, ogg_int64_t);
107     alias da_ov_time_seek = int function(OggVorbis_File*, double);
108     alias da_ov_time_seek_page = int function(OggVorbis_File*, double);
109     alias da_ov_raw_seek_lap = int function(OggVorbis_File*, ogg_int64_t);
110     alias da_ov_pcm_seek_lap = int function(OggVorbis_File*, ogg_int64_t);
111     alias da_ov_pcm_seek_page_lap = int function(OggVorbis_File*, ogg_int64_t);
112     alias da_ov_time_seek_lap = int function(OggVorbis_File*, double);
113     alias da_ov_time_seek_page_lap = int function(OggVorbis_File*, double);
114     alias da_ov_raw_tell = ogg_int64_t function(OggVorbis_File*);
115     alias da_ov_pcm_tell = ogg_int64_t function(OggVorbis_File*);
116     alias da_ov_time_tell = double function(OggVorbis_File*);
117     alias da_ov_info = vorbis_info* function(OggVorbis_File*, int);
118     alias da_ov_comment = vorbis_comment* function(OggVorbis_File*, int);
119     alias da_ov_read_float = c_long function(OggVorbis_File*, float***, int, int*);
120     alias da_ov_read = c_long function(OggVorbis_File*, byte*, int, int, int, int, int*);
121     alias da_ov_crosslap = int function(OggVorbis_File*, OggVorbis_File*);
122     alias da_ov_halfrate = int function(OggVorbis_File*, int);
123     alias da_ov_halfrate_p = int function(OggVorbis_File*);
124 }
125 
126 private extern (C) nothrow {
127     size_t Derelict_VorbisRead(void* ptr, size_t byteSize, size_t sizeToRead, void* datasource) {
128         return fread(ptr, byteSize, sizeToRead, cast(FILE*)datasource);
129     }
130 
131     int Derelict_VorbisSeek(void* datasource, ogg_int64_t offset, int whence) {
132         return fseek(cast(FILE*)datasource, cast(int)offset, whence);
133     }
134 
135     int Derelict_VorbisClose(void* datasource) {
136         return fclose(cast(FILE*)datasource);
137     }
138 
139     c_long Derelict_VorbisTell(void* datasource) {
140         return cast(c_long)ftell(cast(FILE*)datasource);
141     }
142 }
143 
144 __gshared {
145     da_ov_clear ov_clear;
146     da_ov_fopen ov_fopen;
147     da_ov_open_callbacks ov_open_callbacks;
148     da_ov_test_callbacks ov_test_callbacks;
149     da_ov_test_open ov_test_open;
150     da_ov_bitrate ov_bitrate;
151     da_ov_bitrate_instant ov_bitrate_instant;
152     da_ov_streams ov_streams;
153     da_ov_seekable ov_seekable;
154     da_ov_serialnumber ov_serialnumber;
155     da_ov_raw_total ov_raw_total;
156     da_ov_pcm_total ov_pcm_total;
157     da_ov_time_total ov_time_total;
158     da_ov_raw_seek ov_raw_seek;
159     da_ov_pcm_seek ov_pcm_seek;
160     da_ov_pcm_seek_page ov_pcm_seek_page;
161     da_ov_time_seek ov_time_seek;
162     da_ov_time_seek_page ov_time_seek_page;
163     da_ov_raw_seek_lap ov_raw_seek_lap;
164     da_ov_pcm_seek_lap ov_pcm_seek_lap;
165     da_ov_pcm_seek_page_lap ov_pcm_seek_page_lap;
166     da_ov_time_seek_lap ov_time_seek_lap;
167     da_ov_time_seek_page_lap ov_time_seek_page_lap;
168     da_ov_raw_tell ov_raw_tell;
169     da_ov_pcm_tell ov_pcm_tell;
170     da_ov_time_tell ov_time_tell;
171     da_ov_info ov_info;
172     da_ov_comment ov_comment;
173     da_ov_read_float ov_read_float;
174     da_ov_read ov_read;
175     da_ov_crosslap ov_crosslap;
176     da_ov_halfrate ov_halfrate;
177     da_ov_halfrate_p ov_halfrate_p;
178 }
179 
180 class DerelictVorbisFileLoader : SharedLibLoader {
181     public this() {
182         super(libNames);
183     }
184 
185     protected override void loadSymbols() {
186         bindFunc(cast(void**)&ov_clear, "ov_clear");
187         bindFunc(cast(void**)&ov_fopen, "ov_fopen");
188         bindFunc(cast(void**)&ov_open_callbacks, "ov_open_callbacks");
189         bindFunc(cast(void**)&ov_test_callbacks, "ov_test_callbacks");
190         bindFunc(cast(void**)&ov_test_open, "ov_test_open");
191         bindFunc(cast(void**)&ov_bitrate, "ov_bitrate");
192         bindFunc(cast(void**)&ov_bitrate_instant, "ov_bitrate_instant");
193         bindFunc(cast(void**)&ov_streams, "ov_streams");
194         bindFunc(cast(void**)&ov_seekable, "ov_seekable");
195         bindFunc(cast(void**)&ov_serialnumber, "ov_serialnumber");
196         bindFunc(cast(void**)&ov_raw_total, "ov_raw_total");
197         bindFunc(cast(void**)&ov_pcm_total, "ov_pcm_total");
198         bindFunc(cast(void**)&ov_time_total, "ov_time_total");
199         bindFunc(cast(void**)&ov_raw_seek, "ov_raw_seek");
200         bindFunc(cast(void**)&ov_pcm_seek, "ov_pcm_seek");
201         bindFunc(cast(void**)&ov_pcm_seek_page, "ov_pcm_seek_page");
202         bindFunc(cast(void**)&ov_time_seek, "ov_time_seek");
203         bindFunc(cast(void**)&ov_time_seek_page, "ov_time_seek_page");
204         bindFunc(cast(void**)&ov_raw_seek_lap, "ov_raw_seek_lap");
205         bindFunc(cast(void**)&ov_pcm_seek_lap, "ov_pcm_seek_lap");
206         bindFunc(cast(void**)&ov_pcm_seek_page_lap, "ov_pcm_seek_page_lap");
207         bindFunc(cast(void**)&ov_time_seek_lap, "ov_time_seek_lap");
208         bindFunc(cast(void**)&ov_time_seek_page_lap, "ov_time_seek_page_lap");
209         bindFunc(cast(void**)&ov_raw_tell, "ov_raw_tell");
210         bindFunc(cast(void**)&ov_pcm_tell, "ov_pcm_tell");
211         bindFunc(cast(void**)&ov_time_tell, "ov_time_tell");
212         bindFunc(cast(void**)&ov_info, "ov_info");
213         bindFunc(cast(void**)&ov_comment, "ov_comment");
214         bindFunc(cast(void**)&ov_read_float, "ov_read_float");
215         bindFunc(cast(void**)&ov_read, "ov_read");
216         bindFunc(cast(void**)&ov_crosslap, "ov_crosslap");
217         bindFunc(cast(void**)&ov_halfrate, "ov_halfrate");
218         bindFunc(cast(void**)&ov_halfrate_p, "ov_halfrate_p");
219     }
220 }
221 
222 __gshared DerelictVorbisFileLoader DerelictVorbisFile;
223 
224 shared static this() {
225     DerelictVorbisFile = new DerelictVorbisFileLoader();
226 }