1 module main;
2 
3 import std.stdio;
4 import std.file;
5 
6 import audioformats;
7 import dplug.core;
8 import core.stdc.stdlib;
9 
10 void main(string[] args)
11 {
12     if (args.length != 3)
13         throw new Exception("usage: transcode input.{mp3|wav|flac|ogg|opus|mod|xm} output.wav");
14 
15     string inputPath = args[1];
16     string outputPath = args[2];
17 
18     try
19     {
20 
21         AudioStream input, output;
22 
23         input.openFromFile(args[1]);
24         float sampleRate = input.getSamplerate();
25         int channels = input.getNumChannels();
26         long lengthFrames = input.getLengthInFrames();
27 
28         writefln("Opening %s:", inputPath);
29         writefln("  * format     = %s", convertAudioFileFormatToString(input.getFormat()) );
30         writefln("  * samplerate = %s Hz", sampleRate);
31         writefln("  * channels   = %s", channels);
32         if (lengthFrames == audiostreamUnknownLength)
33         {
34             writefln("  * length     = unknown");
35         }
36         else
37         {
38             double seconds = lengthFrames / cast(double) sampleRate;
39             writefln("  * length     = %.3g seconds (%s samples)", seconds, lengthFrames);
40         }
41 
42         float[] buf = new float[1024 * channels];
43         output.openToFile(outputPath, AudioFileFormat.wav, sampleRate, channels);
44 
45         // Chunked encode/decode
46         int totalFrames = 0;
47         int framesRead;
48         do
49         {
50             framesRead = input.readSamplesFloat(buf);
51             output.writeSamplesFloat(buf[0..framesRead*channels]);
52             totalFrames += framesRead;
53         } while(framesRead > 0);
54 
55         output.destroy();
56         
57         writefln("=> %s frames decoded and encoded to %s", totalFrames, outputPath);
58     }
59     catch(Exception e)
60     {
61         writeln(e.msg);
62         destroyFree(e);
63     }
64 }