kp-computercraft-scripts/pcmToLua.d

38 lines
869 B
D
Raw Normal View History

2023-01-01 22:32:25 +00:00
#!/usr/bin/env rdmd
/**
2023-01-02 20:01:38 +00:00
* Converts a signed 8-bit PCM audio file into a Lua table list. Run this from
* the command-line via `./pcmToLua my-pcm-file.pcm`.
2023-01-01 22:32:25 +00:00
*/
module pcm_to_lua;
import std.stdio;
import std.file;
2023-01-02 20:52:21 +00:00
import std.array;
2023-01-01 22:32:25 +00:00
2023-01-02 20:01:38 +00:00
const frameSize = 128 * 1024;
2023-01-01 22:32:25 +00:00
int main(string[] args) {
if (args.length < 2) {
stderr.writeln("Missing required file.");
return 1;
}
string audioFilename = args[1];
if (!exists(audioFilename)) {
stderr.writefln!"File %s doesn't exist."(audioFilename);
return 1;
}
byte[] contents = cast(byte[]) std.file.read(audioFilename);
2023-01-02 20:01:38 +00:00
ulong sampleIndex = 0;
2023-01-02 20:52:21 +00:00
auto app = appender!string;
2023-01-01 22:32:25 +00:00
stdout.writeln("local audio = {");
foreach (byte sample; contents) {
stdout.writefln!" %d,"(sample);
2023-01-02 20:01:38 +00:00
sampleIndex++;
2023-01-01 22:32:25 +00:00
}
stdout.writeln("}");
return 0;
}