Ejemplo n.º 1
0
        /// <summary>
        /// New binary wave.
        /// </summary>
        /// <param name="s">The stream.</param>
        public BinaryWave(b_stm s)
        {
            //Set data.
            ByteOrder  = Syroot.BinaryData.ByteOrder.LittleEndian;
            Major      = 0;
            Minor      = 1;
            SampleRate = s.info.streamSoundInfo.sampleRate;
            NumSamples = s.info.streamSoundInfo.sampleCount;
            switch (s.info.streamSoundInfo.encoding)
            {
            case 0:
                s.data.dspAdpcm = EncoderFactory.Pcm16ToDspAdpcmSTM(EncoderFactory.SignedPcm8ToPcm16(s.data.pcm8), s);
                break;

            case 1:
                s.data.dspAdpcm = EncoderFactory.Pcm16ToDspAdpcmSTM(s.data.pcm16, s);
                break;
            }
            DspAdpcmInfo = new DspAdpcmInfo[s.info.channels.Count];
            for (int i = 0; i < DspAdpcmInfo.Length; i++)
            {
                DspAdpcmInfo[i] = s.info.channels[i].dspAdpcmInfo;
            }
            Loops           = s.info.streamSoundInfo.isLoop;
            LoopStartSample = s.info.streamSoundInfo.loopStart;
            LoopEndSample   = s.info.streamSoundInfo.sampleCount;
            Data            = s.data;

            //Do channel pans.
            ChannelPans = new ChannelPan[s.info.channels.Count];
            for (int i = 0; i < s.info.channels.Count; i++)
            {
                if (i == s.info.channels.Count - 1)
                {
                    ChannelPans[i] = ChannelPan.Middle;
                }
                else if (i % 2 == 0)
                {
                    ChannelPans[i]     = ChannelPan.Left;
                    ChannelPans[i + 1] = ChannelPan.Right;
                    i++;
                }
            }
            if (s.info.tracks != null)
            {
                foreach (var t in s.info.tracks)
                {
                    if (t.globalChannelIndexTable.count > 0)
                    {
                        if (t.globalChannelIndexTable.count > 1)
                        {
                            ChannelPans[t.globalChannelIndexTable.entries[0]] = ChannelPan.Left;
                            ChannelPans[t.globalChannelIndexTable.entries[1]] = ChannelPan.Right;
                        }
                        else
                        {
                            ChannelPans[t.globalChannelIndexTable.entries[0]] = ChannelPan.Middle;
                        }
                    }
                }
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Create a standard b_stm.
        /// </summary>
        /// <param name="sampleRate">The sample rate.</param>
        /// <param name="numSamples">Number of samples.</param>
        /// <param name="samples">Pcm8[][] or Pcm16[][] audio samples.</param>
        /// <param name="encoding">If samples is Pcm8[][] always 0. Must be 1 or 2 for if samples is Pcm16[][].</param>
        /// <param name="version">The version of the file.</param>
        /// <returns></returns>
        public static b_stm CreateStream(UInt32 sampleRate, UInt32 numSamples, object samples, byte encoding, byte vMajor, byte vMinor, byte vRevision)
        {
            b_stm s = new b_stm();

            s.fileHeader = new FileHeader("FSTM", ByteOrder.BigEndian, vMajor, vMinor, vRevision, 0, new List <SizedReference>());

            s.info = new b_stm.InfoBlock();
            s.info.streamSoundInfo = new b_stm.StreamSoundInfo();
            s.info.tracks          = new List <b_stm.TrackInfo>();
            s.info.channels        = new List <b_stm.ChannelInfo>();

            //Stream info.
            s.info.streamSoundInfo             = new b_stm.StreamSoundInfo();
            s.info.streamSoundInfo.encoding    = encoding;
            s.info.streamSoundInfo.sampleCount = numSamples;
            s.info.streamSoundInfo.sampleRate  = sampleRate;

            //Channels.
            switch (encoding)
            {
            case EncodingTypes.PCM8:
                s.data = new SoundNStreamDataBlock(EncoderFactory.Pcm8ToSignedPcm8(samples as byte[][]), encoding);
                for (int i = 0; i < (samples as byte[][]).Length; i++)
                {
                    s.info.channels.Add(new b_stm.ChannelInfo());
                }
                break;

            case EncodingTypes.PCM16:
                s.data = new SoundNStreamDataBlock(samples, encoding);
                for (int i = 0; i < (samples as Int16[][]).Length; i++)
                {
                    s.info.channels.Add(new b_stm.ChannelInfo());
                }
                break;

            case EncodingTypes.DSP_ADPCM:
                s.data = new SoundNStreamDataBlock(EncoderFactory.Pcm16ToDspAdpcmSTM(samples as Int16[][], s), encoding);
                s.Update(ByteOrder.BigEndian);

                //Get DSP-ADPCM info.
                DspAdpcmInfo[] context = new DspAdpcmInfo[s.data.dspAdpcm.Length];
                int            cCount  = 0;
                foreach (var channel in s.info.channels)
                {
                    context[cCount] = channel.dspAdpcmInfo;
                    cCount++;
                }

                //Create SEEK block.
                s.seek = new SoundNStreamSeekBlock(s.data.dspAdpcm, s.info.streamSoundInfo.sampleCount, context);

                break;
            }

            //Tracks.
            for (int i = 0; i < s.info.channels.Count; i += 2)
            {
                s.info.tracks.Add(new b_stm.TrackInfo()
                {
                    volume = 0x64, pan = 0x40, span = 0x0, surroundMode = 0, globalChannelIndexTable = new Table <byte>()
                    {
                        entries = new List <byte>()
                        {
                            (byte)i
                        }
                    }
                });
                if (i + 1 != s.info.channels.Count)
                {
                    s.info.tracks[s.info.tracks.Count - 1].globalChannelIndexTable.entries.Add((byte)(i + 1));
                }
            }

            s.Update(ByteOrder.BigEndian);

            return(s);
        }