-
Notifications
You must be signed in to change notification settings - Fork 3
/
Track.cs
252 lines (227 loc) · 6.53 KB
/
Track.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
using System;
using System.IO;
using BinChunker;
class Track
{
public int TrackNumber;
public bool Audio;
public string Modes;
public TrackExtension FileExtension;
public int BlockStart;
public int BlockSize;
public long StartSector;
public long StopSector;
public long Stop;
public static bool SwapAudioByteOrder= false;
public static bool WavFormat = false;
public static bool TruncatePsx = false;
public enum TrackExtension
{
ISO, CDR, WAV, UGH }
public Track( string trackNumber, string mode, string time )
{
TrackNumber = int.Parse(trackNumber);
SetMode( mode );
StartSector = Track.ToFrames( time );
Console.WriteLine("Track {0:00}: {1} {2}", TrackNumber, mode.PadLeft(12), time );
}
/// <summary>
/// Property StartPosition (long)
/// </summary>
public long StartPosition
{
get { return StartSector * BinChunk.SectorLength; }
}
/// <summary>
/// Parse the mode string
/// </summary>
private void SetMode( string mode )
{
Modes = mode;
Audio = false;
BlockStart = 0;
FileExtension = TrackExtension.ISO;
switch ( mode.ToUpper() )
{
case "AUDIO":
BlockSize = 2352;
Audio = true;
if (WavFormat)
FileExtension = TrackExtension.WAV;
else
FileExtension = TrackExtension.CDR;
break;
case "MODE1/2352":
BlockStart = 16;
BlockSize = 2048;
break;
case "MODE2/2336":
// WAS 2352 in V1.361B still work? What if MODE2/2336 single track bin, still 2352 sectors?
BlockStart = 16;
BlockSize = 2336;
break;
case "MODE2/2352":
if (TruncatePsx)
// PSX: truncate from 2352 to 2336 byte tracks
BlockSize = 2336;
else
{
// Normal MODE2/2352
BlockStart = 24;
BlockSize = 2048;
}
break;
default:
Console.WriteLine("(?) ");
BlockSize = 2352;
FileExtension = TrackExtension.UGH;
break;
}
}
/// <summary>
/// Write a track
/// </summary>
/// <param name="bf"></param>
/// <param name="fileName">File to write track data to.</param>
public void Write( Stream bf, string fileName)
{
Console.Write( Environment.NewLine + "{0:d2}: {1} ", TrackNumber, fileName );
try
{
bf.Seek( StartPosition, SeekOrigin.Begin );
}
catch (Exception e)
{
throw new ApplicationException( string.Format("Could not seek to track location: {0}", e.Message ) );
}
if (BinChunk.Verbose)
{
Console.WriteLine( Environment.NewLine + "mmc sectors {0}.{1} ({2})", StartSector, StopSector, StopSector - StartSector + 1);
Console.WriteLine("mmc bytes {0}.{1} ({2})", StartPosition, Stop, Stop - StartPosition + 1);
Console.WriteLine("sector data at {0}, {1} bytes per sector", BlockStart, BlockSize);
Console.WriteLine("real data {0} bytes" + Environment.NewLine, (StopSector - StartSector + 1) * BlockSize);
}
Console.Write(" ");
try
{
using (Stream stream = File.OpenWrite( fileName ) )
{
if (Audio && WavFormat)
{
byte[] header = MakeWavHeader( RealLength );
stream.Write( header, 0, header.Length );
}
long sz = StartPosition;
long sector = StartSector;
long realsz = 0;
byte[] buf = new byte[ BinChunk.SectorLength ];
while ((sector <= StopSector) && (bf.Read(buf, 0, BinChunk.SectorLength) > 0))
{
if ( Audio && SwapAudioByteOrder )
DoByteSwap( buf );
stream.Write( buf, BlockStart, BlockSize);
sz += BinChunk.SectorLength;
realsz += BlockSize;
if (((sz / BinChunk.SectorLength) % 500) == 0)
PrintProgress( realsz, RealLength );
sector++;
}
}
}
catch (Exception e)
{
Console.WriteLine( Environment.NewLine );
throw new ApplicationException( string.Format(" Could not write to track file {0}: {1}", fileName, e.Message ) );
}
PrintProgress( RealLength, RealLength);
}
void PrintProgress( long realsz, long RealLength )
{
float fl = (float)realsz / (float)RealLength;
string msg = string.Format( "{0:d4}/{1:d4} MB [{2}] {3:d3}%",
realsz/1024/1024, RealLength/1024/1024, GetProgressBar(fl, 29), (int)(fl * 100));
msg = msg.PadLeft( 2*msg.Length, '\b' );
Console.Write( msg );
}
/// <summary>
/// return a progress bar
/// </summary>
/// <param name="value">Progress (0.0 - 1.0)</param>
/// <param name="position">Width of the progress bar (number of characters)</param>
/// <returns>String containing multiple '*' showing progress.</returns>
string GetProgressBar( float position, int length )
{
int n = (int)(length * position);
return "".PadLeft( n, '*').PadRight( length );
}
byte[] MakeWavHeader( long length )
{
const int WAV_RIFF_HLEN = 12;
const int WAV_FORMAT_HLEN = 24;
const int WAV_DATA_HLEN = 8;
const int WAV_HEADER_LEN = WAV_RIFF_HLEN + WAV_FORMAT_HLEN + WAV_DATA_HLEN;
MemoryStream memoryStream = new MemoryStream( WAV_HEADER_LEN );
using( BinaryWriter writer = new BinaryWriter( memoryStream ) )
{
// RIFF header
writer.Write( "RIFF".ToCharArray() );
uint dwordValue = (uint) length + WAV_DATA_HLEN + WAV_FORMAT_HLEN + 4;
writer.Write(dwordValue); // length of file, starting from WAVE
writer.Write( "WAVE".ToCharArray() );
// FORMAT header
writer.Write("fmt ".ToCharArray() );
dwordValue = 0x10; // length of FORMAT header
writer.Write(dwordValue);
ushort wordValue = 0x01; // constant
writer.Write(wordValue);
wordValue = 0x02; // channels
writer.Write(wordValue);
dwordValue = 44100; // sample rate
writer.Write(dwordValue);
dwordValue = 44100 * 4; // bytes per second
writer.Write(dwordValue);
wordValue = 4; // bytes per sample
writer.Write(wordValue);
wordValue = 2*8; // bits per channel
writer.Write(wordValue);
// DATA header
writer.Write( "data".ToCharArray() );
dwordValue = (uint) length;
writer.Write(dwordValue);
}
return memoryStream.ToArray();
}
void DoByteSwap( byte[] buf )
{
// swap low and high bytes
int p = BlockStart;
int ep = BlockSize;
while (p < ep)
{
byte c = buf[p];
buf[p] = buf[p + 1];
buf[p + 1] = c;
p += 2;
}
}
/// <summary>
/// Length in bytes of track.
/// </summary>
public long RealLength
{
get { return (StopSector - StartSector + 1) * BlockSize; }
}
/// <summary>
/// Convert a mins:secs:frames format to plain frames
/// </summary>
/// <param name="s">text containing mins:secs:frames</param>
/// <returns>Frame number</returns>
static long ToFrames( string time )
{
string[] segs = time.Split(':');
int mins = int.Parse(segs[0]);
int secs = int.Parse(segs[1]);
int frames = int.Parse(segs[2]);
return (mins * 60 + secs) * 75 + frames;
}
}