egra: agg will respect clip rect now
[iv.d.git] / dopus / orig / optest_00.d
blob53ee1172df3676b689eabd25cfd1957c72b43ee1
1 import iv.libopus;
2 import zogg;
4 import iv.alsa;
5 import iv.cmdcon;
6 import iv.vfs;
9 //version = noplay;
11 //enum FileName = "/tmp/03/linda_karandashi_i_spichki.opus";
12 //enum FileName = "/tmp/03/melodie_128.opus";
13 enum FileName = "z00.raw";
16 __gshared snd_pcm_t* pcm;
19 enum Rate = 48000;
20 enum Chans = 2;
23 void alsaOpen () {
24 int err;
25 if ((err = snd_pcm_open(&pcm, "plug:default", SND_PCM_STREAM_PLAYBACK, 0)) < 0) {
26 import core.stdc.stdio : printf;
27 import core.stdc.stdlib : exit, EXIT_FAILURE;
28 printf("Playback open error: %s\n", snd_strerror(err));
29 exit(EXIT_FAILURE);
32 if ((err = snd_pcm_set_params(pcm, SND_PCM_FORMAT_S16_LE, SND_PCM_ACCESS_RW_INTERLEAVED, Chans, Rate, 1, 500000)) < 0) {
33 import core.stdc.stdio : printf;
34 import core.stdc.stdlib : exit, EXIT_FAILURE;
35 printf("Playback open error: %s\n", snd_strerror(err));
36 exit(EXIT_FAILURE);
41 void alsaClose () {
42 if (pcm !is null) {
43 snd_pcm_close(pcm);
44 pcm = null;
49 void alsaWriteX (short* sptr, uint frms) {
50 while (frms > 0) {
51 snd_pcm_sframes_t frames = snd_pcm_writei(pcm, sptr, frms);
52 if (frames < 0) {
53 frames = snd_pcm_recover(pcm, cast(int)frames, 0);
54 if (frames < 0) {
55 import core.stdc.stdio : printf;
56 import core.stdc.stdlib : exit, EXIT_FAILURE;
57 printf("snd_pcm_writei failed: %s\n", snd_strerror(cast(int)frames));
58 exit(EXIT_FAILURE);
60 } else {
61 frms -= frames;
62 sptr += frames*Chans;
68 // ////////////////////////////////////////////////////////////////////////// //
69 void main (string[] args) {
70 if (args.length == 1) args ~= FileName;
72 auto fl = VFile(args[1]);
74 int err;
75 OpusDecoder* dc = opus_decoder_create(Rate, Chans, &err);
76 if (err != OPUS_OK) assert(0, opus_strerr(err).idup);
77 scope(exit) opus_decoder_destroy(dc);
79 short[] sbuf;
80 sbuf.length = Rate*4;
82 ulong packets;
84 version(noplay) {} else alsaOpen();
85 version(noplay) {} else scope(exit) alsaClose();
87 ubyte[] data;
88 for (;;) {
89 uint pklen;
90 try { pklen = fl.readNum!(uint, "BE"); } catch (Exception) break;
91 fl.readNum!uint; // final range state
92 //conwriteln("packet length: ", pklen);
93 if (data.length < pklen) data.length = pklen;
94 fl.rawReadExact(data[0..pklen]);
96 err = opus_decode(dc, data.ptr, pklen, sbuf.ptr, sbuf.length/2, 0);
97 if (err < 0) assert(0, opus_strerr(err).idup);
99 conwrite("\rpacket length: ", pklen, "; samples: ", opus_decoder_get_nb_samples(dc, data.ptr, pklen), " (", err, "); chans=", opus_packet_get_nb_channels(data.ptr), "\e[K");
101 //conwriteln(" ", c.streams.out_size);
102 //conwriteln("samples=", err);
103 version(noplay) {} else alsaWriteX(sbuf.ptr, err);
105 ++packets;
107 conwriteln("\n", packets, " opus packets found");