egra: agg will respect clip rect now
[iv.d.git] / dopus / orig / optest.d
blob6e00f18871e7b96ca95335fc1100a31cb8fb9c41
1 import avmem;
2 import avfft;
3 import opus;
4 import opus_celt;
5 import opus_silk;
6 import opusdec;
7 import xalsa;
8 import zogg;
10 import iv.alsa;
11 import iv.cmdcon;
12 import iv.vfs;
15 //version = noplay;
17 enum FileName = "/tmp/03/linda_karandashi_i_spichki.opus";
18 //enum FileName = "/tmp/03/melodie_128.opus";
21 // ////////////////////////////////////////////////////////////////////////// //
22 void main (string[] args) {
23 if (args.length == 1) args ~= FileName;
25 AVCtx ctx;
26 OpusContext c;
28 OggStream ogg;
29 ogg.setup(VFile(args[1]));
31 ogg.PageInfo lastpage;
32 if (!ogg.findLastPage(lastpage)) assert(0, "can't find last ogg page");
33 lastpage.granule -= ctx.preskip;
34 conwriteln("last page seqnum: ", lastpage.seqnum);
35 conwriteln("last page granule: ", lastpage.granule);
36 conwriteln("last page filepos: ", lastpage.pgfpos);
38 for (;;) {
39 auto r = opus_header(&ctx, ogg);
40 if (r < 0) assert(0);
41 if (!ogg.loadPacket()) assert(0);
42 if (r == 1) break;
45 if (opus_decode_init(&ctx, &c) < 0) assert(0, "fuuuuu");
46 scope(exit) opus_decode_close(&c);
47 assert(c.nb_streams == 1);
48 assert(c.streams[0].output_channels >= 1 && c.streams[0].output_channels <= 2);
49 //assert(c.streams[0].output_channels == 2);
52 foreach (immutable chn; 0..c.streams[0].output_channels) {
53 conwriteln("output buf for chan #", chn);
54 //c.streams.out_[chn] = av_mallocz!float(Rate);
58 float[][2] obuf;
59 foreach (ref obb; obuf[]) obb.length = Rate;
60 ubyte*[2] eptr;
61 ulong packets, frames;
63 version(noplay) {} else alsaOpen(c.streams[0].output_channels);
64 version(noplay) {} else scope(exit) alsaClose();
66 //ogg.seekPCM(lastpage.granule/4+ctx.preskip);
67 //ogg.seekPCM(lastpage.granule/6+ctx.preskip);
69 ulong lastgran = 0;
70 for (;;) {
71 auto r = opus_packet(&ctx, ogg);
72 if (r < 0) break;
74 //conwriteln("packet #", packets, "; frame #", frames);
75 AVFrame frame;
76 AVPacket pkt;
77 frame.linesize[0] = obuf[0].length*obuf[0].sizeof;
78 pkt.data = ogg.packetData.ptr;
79 pkt.size = cast(uint)ogg.packetLength;
80 foreach (immutable idx, ref obb; obuf[]) eptr[idx] = cast(ubyte*)(obb.ptr);
81 frame.extended_data = eptr.ptr;
82 //c.streams.out_size = outsize;
83 int gotfrptr = 0;
84 r = opus_decode_packet(/*&ctx,*/ &c, &frame, &gotfrptr, &pkt);
85 if (r < 0) {
86 conwriteln("can't process packet #", packets);
87 assert(0);
89 if (gotfrptr) ++frames;
90 //conwriteln(" ", c.streams.out_size);
91 //conwriteln("dc=", r);
92 if (ogg.packetGranule && ogg.packetGranule != -1) lastgran = ogg.packetGranule-ctx.preskip;
93 conwritef!"\r%s:%02s / %s:%02s"((lastgran/48000)/60, (lastgran/48000)%60, (lastpage.granule/48000)/60, (lastpage.granule/48000)%60);
94 version(noplay) {} else alsaWrite2B(eptr.ptr, r);
96 ++packets;
97 if (!ogg.loadPacket()) break;
99 conwriteln(packets, " opus packets, ", frames, " frames found");