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