View Single Post
Neobeo

JCF Member

Joined: Sep 2002

Posts: 409

Neobeo is an asset to this forumNeobeo is an asset to this forum

Dec 5, 2011, 10:12 PM
Neobeo is offline
Reply With Quote
Well, while we're submitting code I thought I should submit a full sound-playing C# code that doesn't use any libraries outside the .NET framework. This plays the input sample number as per the Ambient Sound sequence. For TSF, sound #306 is called sLORISOUNDS_TOUCH. Too bad ambient sound only goes up to 255 though...

Code:
using System;
using System.IO;
using System.IO.Compression;

namespace jj2sound
{
    class Program
    {
        static readonly byte[] magic = { 82, 73, 70, 70, 87, 65, 86, 69, 102, 109, 116, 32,
                                                16, 0, 0, 0, 1, 0, 1, 0, 100, 97, 116, 97 };
        static void Main()
        {
            using (var br = new BinaryReader(File.OpenRead(@"D:\Games\Jazz2TSF\Anims.j2a")))
            {
                br.ReadBytes(24);
                int i, s, sets = br.ReadInt32();
                while (true)
                {
                    Console.Write("Play sample #");
                    if (!int.TryParse(Console.ReadLine(), out s) || s < 0) break;

                    for (i = 0; i < sets; i++)
                    {
                        br.BaseStream.Position = 28 + i * 4;
                        br.BaseStream.Position = br.ReadInt32() + 5;
                        int samples = br.ReadByte();
                        if (s < samples) break;
                        else s -= samples;
                    }
                    if (i == sets) break;

                    br.ReadBytes(6);
                    int[] size = new int[8]; for (i = 0; i < 8; i++) size[i] = br.ReadInt32();
                    br.ReadBytes(size[0] + size[2] + size[4] + 2);

                    using (var bz = new BinaryReader(new DeflateStream(br.BaseStream, 0, true)))
                    {
                        for (i = 0; i < s; i++) bz.ReadBytes(bz.ReadInt32() - 4);
                        bz.ReadBytes(64);
                        int mul = bz.ReadInt16() / 4 + 1; bz.ReadInt16();
                        int length = bz.ReadInt32(); bz.ReadInt64();
                        int rate = bz.ReadInt32(); bz.ReadInt64();
                        Console.WriteLine("Length: {0:0.000}s", (double)length / rate);
                        length *= mul;

                        using (var bw = new BinaryWriter(new MemoryStream()))
                        {
                            bw.Write(magic, 0, 4);
                            bw.Write(length + 36);
                            bw.Write(magic, 4, 16);
                            bw.Write(rate);
                            bw.Write(rate * mul);
                            bw.Write(mul * 0x80001);
                            bw.Write(magic, 20, 4);
                            bw.Write(length);
                            for (i = 0; i < length; i++) bw.Write((byte)(bz.ReadByte() ^ (mul << 7)));
                            bw.Seek(0, 0);
                            new System.Media.SoundPlayer(bw.BaseStream).PlaySync();
                        }
                    }
                }
            }
            Console.WriteLine("Sample not found. Press any key to continue.");
            Console.ReadKey();
        }
    }
}
Also, I'm pretty sure I uploaded a bunch of WAVs back in the day, but that must have been at least a few years ago.
__________________
<TABLE border=1><TR><TD>Facts:
Jazz Sprite Dynamite (JSD)
Tileset Extractor
Neobeo's Firetruck

</TD><TD>Myths:
Jazz Creation Station Plus (JCS+) - 10%
Coming soon - a dedicated server! - 25%
Jazz Sprite Dynamite v2 (JSDv2) - 2%
Another generic single-player level - 0%
</TD></TR></TABLE>