Last change
on this file since 1144 was 1131, checked in by geyser, 5 years ago |
Yay, another SNDD fix that no one cares about: PC demo SNDDs! (short .dat headers, but the .raw data is MS ADPCM; use -nodemo to treat as Mac/IMA4 anyway).
|
File size:
2.5 KB
|
Rev | Line | |
---|
[1114] | 1 | using System;
|
---|
| 2 | using System.Collections.Generic;
|
---|
| 3 | using System.Text;
|
---|
| 4 |
|
---|
| 5 | namespace Oni.Sound
|
---|
| 6 | {
|
---|
| 7 | internal class SoundData
|
---|
| 8 | {
|
---|
[1130] | 9 | public bool IsIMA4;
|
---|
[1114] | 10 | public int SampleRate;
|
---|
| 11 | public int ChannelCount;
|
---|
| 12 | public byte[] Data;
|
---|
| 13 |
|
---|
[1131] | 14 | public static SoundData Read(InstanceDescriptor sndd, bool do_pc_demo_test)
|
---|
[1114] | 15 | {
|
---|
| 16 | if (sndd.Template.Tag != TemplateTag.SNDD)
|
---|
| 17 | throw new ArgumentException("descriptor");
|
---|
| 18 |
|
---|
| 19 | var sound = new SoundData();
|
---|
| 20 |
|
---|
| 21 | int dataSize;
|
---|
| 22 | int dataOffset;
|
---|
| 23 |
|
---|
| 24 | using (var reader = sndd.OpenRead())
|
---|
| 25 | {
|
---|
| 26 | if (sndd.IsMacFile)
|
---|
| 27 | {
|
---|
| 28 | sound.ChannelCount = (reader.ReadInt32() >> 1) + 1;
|
---|
| 29 | sound.SampleRate = 22050;
|
---|
| 30 | reader.Skip(4);
|
---|
[1130] | 31 | sound.IsIMA4 = true;
|
---|
[1114] | 32 | }
|
---|
| 33 | else
|
---|
| 34 | {
|
---|
[1130] | 35 | reader.Skip(6); // ADPCM format identifiers (change to support PCM?)
|
---|
[1114] | 36 | sound.ChannelCount = reader.ReadInt16();
|
---|
| 37 | sound.SampleRate = reader.ReadInt32();
|
---|
| 38 | reader.Skip(44);
|
---|
[1130] | 39 | sound.IsIMA4 = false;
|
---|
[1114] | 40 | }
|
---|
| 41 |
|
---|
| 42 | dataSize = reader.ReadInt32();
|
---|
| 43 | dataOffset = reader.ReadInt32();
|
---|
| 44 | }
|
---|
| 45 |
|
---|
| 46 | using (var rawReader = sndd.GetRawReader(dataOffset))
|
---|
| 47 | sound.Data = rawReader.ReadBytes(dataSize);
|
---|
| 48 |
|
---|
[1131] | 49 | if (sound.IsIMA4 && do_pc_demo_test) // check if the raw data actually looks like IMA4
|
---|
| 50 | {
|
---|
| 51 | int nIMABlocks = sound.Data.Length / 34;
|
---|
| 52 | int remainder = sound.Data.Length - nIMABlocks * 34;
|
---|
| 53 | if (remainder == 0 && (nIMABlocks % sound.ChannelCount) == 0)
|
---|
| 54 | {
|
---|
| 55 | bool stepIndexAbove88 = false;
|
---|
| 56 | for (int ii = 0; ii < nIMABlocks; ii++)
|
---|
| 57 | {
|
---|
| 58 | Byte cc = sound.Data[ii * 34 + 1];
|
---|
| 59 | if ((cc & 0x7F) > 88)
|
---|
| 60 | stepIndexAbove88 = true;
|
---|
| 61 | }
|
---|
| 62 | if (stepIndexAbove88)
|
---|
| 63 | sound.IsIMA4 = false;
|
---|
| 64 | }
|
---|
| 65 | else
|
---|
| 66 | sound.IsIMA4 = false;
|
---|
| 67 | if (!(sound.IsIMA4))
|
---|
| 68 | Console.WriteLine("PC-demo MS ADPCM detected; use -nodemo flag to treat as IMA4.");
|
---|
| 69 | }
|
---|
| 70 |
|
---|
[1114] | 71 | return sound;
|
---|
| 72 | }
|
---|
| 73 | }
|
---|
| 74 | }
|
---|
Note:
See
TracBrowser
for help on using the repository browser.