-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathProgram.cs
63 lines (56 loc) · 1.77 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
using System;
using System.IO;
using System.Collections.Generic;
using System.Text;
namespace Chipifier
{
public partial class Program
{
public Program()
{
}
[STAThread]
static void Main(string[] args)
{
new Program().RunImportJob(args);
}
void RunImportJob(IEnumerable<string> files)
{
foreach(var f in files)
{
RiffMaster rm = new RiffMaster();
rm.LoadFile(f);
var dataChunk = rm.riff.GetSubchunk("data", null) as RiffMaster.RiffSubchunk;
var fmt = rm.riff.GetSubchunk("fmt ", null) as RiffMaster.RiffSubchunk_fmt;
BinaryReader br = new BinaryReader(new MemoryStream(dataChunk.data));
int nSamples = dataChunk.data.Length/fmt.blockAlign;
int nChunks = nSamples / 32;
if (fmt.channels == 2) throw new InvalidOperationException("expected mono file");
string textoutfile = f + ".txt";
StreamWriter sw = new StreamWriter(textoutfile);
br = new BinaryReader(new MemoryStream(dataChunk.data));
var ms = new MemoryStream();
for (int j = 0; j < nChunks; j++)
{
string outfile = f + "." + j + ".dmw";
FileStream fs = new FileStream(outfile, FileMode.Create, FileAccess.Write, FileShare.None);
fs.WriteByte(0x20); fs.WriteByte(0x00); fs.WriteByte(0x00); fs.WriteByte(0x00);
fs.WriteByte(0x1F);
for (int i = 0; i < 32; i++)
{
short ssample = br.ReadInt16();
int sample = ssample + 16;
if(sample>31) sample=31; //clamp in case we overdrived or whatever
if (sample < 0) throw new InvalidOperationException("oops minus 0");
fs.WriteByte((byte)sample);
sw.Write("{0}{1}", (byte)sample,i==31?"":" ");
}
sw.WriteLine();
fs.Close();
}
sw.Close();
rm.WriteFile(f);
}
}
}
}