Пример #1
0
        private void btnWav_Click(object sender, EventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;
            string sound = SaveFile();

            SaveFileDialog o = new SaveFileDialog();

            o.FileName = SearchFile().name;
            o.Filter   = "WAVE (*.wav)|*.wav";
            if (o.ShowDialog() == DialogResult.OK)
            {
                string wavSaved = o.FileName;

                switch (SearchFile().type)
                {
                case FormatSound.SWAV:
                    WAV.Write(SWAV.ConvertToWAV(SWAV.Read(sound), false), wavSaved);
                    break;

                case FormatSound.STRM:
                    WAV.Write(STRM.ConvertToWAV(STRM.Read(sound), false), wavSaved);
                    break;
                }
            }

            File.Delete(sound);
            this.Cursor = Cursors.Default;
        }
Пример #2
0
        public static sWAV ConvertToWAV(sSWAV swav, bool loop)
        {
            sWAV wav = new sWAV();

            if (swav.data.info.nWaveType == 0) // 8 Bits per sample, PCM-8
            {
                swav.data.data = PCM.PCM8SignedToPCM16(swav.data.data);
                if (loop)
                {
                    Byte[] data = new Byte[(int)swav.data.info.nNonLoopLen];
                    Array.Copy(swav.data.data, swav.data.info.nLoopOffset, data, 0, data.Length);
                    swav.data.data = data;
                }

                wav = WAV.Create_WAV(1, swav.data.info.nSampleRate, 16, swav.data.data);
            }
            else if (swav.data.info.nWaveType == 1) // 16 Bits per sample, PCM-16
            {
                if (loop)                           // NO TESTED
                {
                    Byte[] data = new Byte[(int)swav.data.info.nNonLoopLen];
                    Array.Copy(swav.data.data, swav.data.info.nLoopOffset, data, 0, data.Length);
                    swav.data.data = data;
                }

                wav = WAV.Create_WAV(1, swav.data.info.nSampleRate, 16, swav.data.data);
            }
            else if (swav.data.info.nWaveType >= 2) // 4 Bits per sample, IMA-ADPCM
            {
                swav.data.data = Compression_ADPCM.Decompress(
                    swav.data.data,
                    BitConverter.ToUInt16(swav.data.data, 0),
                    BitConverter.ToUInt16(swav.data.data, 2));

                if (loop)
                {
                    Byte[] data = new Byte[swav.data.data.Length - ((int)swav.data.info.nLoopOffset * 2)];
                    Array.Copy(swav.data.data, swav.data.info.nLoopOffset * 2, data, 0, data.Length);
                    swav.data.data = data;
                }

                wav = WAV.Create_WAV(1, swav.data.info.nSampleRate, 16, swav.data.data);
            }
            return(wav);
        }
Пример #3
0
        public static sSWAV ConvertToSWAV(sWAV wav, int waveType, int volume = 150)
        {
            sSWAV swav = new sSWAV();

            swav.header.type   = "SWAV".ToCharArray();
            swav.header.magic  = 0x0100FEFF;
            swav.header.nSize  = 0x10;
            swav.header.nBlock = 0x01;

            swav.data.type             = "DATA".ToCharArray();
            swav.data.info.nWaveType   = (byte)waveType;
            swav.data.info.bLoop       = 1;
            swav.data.info.nSampleRate = (ushort)wav.wave.fmt.sampleRate;
            swav.data.info.nTime       = (ushort)(1.6756991e+7 / wav.wave.fmt.sampleRate);
            swav.data.info.nLoopOffset = 0x01;

            if (wav.wave.fmt.numChannels > 1)
            {
                wav.wave.data.data = WAV.ConvertToMono(wav.wave.data.data, wav.wave.fmt.numChannels, wav.wave.fmt.bitsPerSample);
            }

            //wav.wave.data.data = ChangeVolume(wav.wave.data.data, volume, wav.wave.fmt.bitsPerSample);
            if (waveType == 0)
            {
                swav.data.data = PCM.PCM16ToPCM8(wav.wave.data.data);
            }
            else if (waveType == 2)
            {
                List <byte> temp = new List <byte>();
                temp.AddRange(new Byte[] { 0x00, 0x00, 0x00, 0x00 });
                temp.AddRange(Compression_ADPCM.Compress(wav.wave.data.data));
                swav.data.data = temp.ToArray();
            }
            else
            {
                swav.data.data = wav.wave.data.data;
            }

            swav.data.nSize            = (uint)swav.data.data.Length + 0x0A;
            swav.data.info.nNonLoopLen = (uint)swav.data.data.Length;
            swav.header.nFileSize      = swav.data.nSize + swav.header.nSize;

            return(swav);
        }
Пример #4
0
        /// <summary>
        /// Convert a STRM structure to a WAV structure
        /// </summary>
        /// <param name="strm">STRM structure to convert</param>
        /// <param name="loop">If true, the new WAV data will start in the loop offset</param>
        /// <returns>WAV structure converted</returns>
        public static sWAV ConvertToWAV(sSTRM strm, bool loop)
        {
            sWAV wav = new sWAV();

            // Get the audio data
            if (strm.head.channels == 2)
            {
                // Get both channels and convert it to PCM-16
                strm.data.leftChannel = DivideChannels(strm.data.data, strm.head.nBlocks, strm.head.blockLen,
                                                       strm.head.lastBlocklen, true, strm.head.waveType);
                strm.data.rightChannel = DivideChannels(strm.data.data, strm.head.nBlocks, strm.head.blockLen,
                                                        strm.head.lastBlocklen, false, strm.head.waveType);
                Array.Clear(strm.data.data, 0, strm.data.data.Length);

                if (loop && strm.head.waveType == 0) // 8 bits per sample
                {
                    strm.data.data = MergeChannels(strm.data.leftChannel, strm.data.rightChannel, (int)strm.head.loopOffset);
                }
                else if (loop && strm.head.waveType == 2) // 4 bits per sample
                {
                    strm.data.data = MergeChannels(strm.data.leftChannel, strm.data.rightChannel, (int)strm.head.loopOffset * 2);
                }
                else if (loop && strm.head.waveType == 1) // 16 bits per sample (NO TESTED)
                {
                    strm.data.data = MergeChannels(strm.data.leftChannel, strm.data.rightChannel, (int)strm.head.loopOffset);
                }
                else // No loop
                {
                    strm.data.data = MergeChannels(strm.data.leftChannel, strm.data.rightChannel);
                }
            }
            else if (strm.head.channels == 1)
            {
                // Get the channel and convert it to PCM-16
                strm.data.data = MonoChannel(strm.data.data, strm.head.nBlocks, strm.head.blockLen,
                                             strm.head.lastBlocklen, strm.head.waveType);

                if (strm.head.waveType == 0 && loop) // 8 bits per sample
                {
                    Byte[] data = new Byte[strm.data.data.Length - (int)strm.head.loopOffset];
                    Array.Copy(strm.data.data, strm.head.loopOffset, data, 0, data.Length);
                    strm.data.data = data;
                }
                else if (loop && strm.head.waveType == 2) // 4 bits per sample
                {
                    Byte[] data = new Byte[strm.data.data.Length - ((int)strm.head.loopOffset * 2)];
                    Array.Copy(strm.data.data, strm.head.loopOffset * 2, data, 0, data.Length);
                    strm.data.data = data;
                }
                else if (loop && strm.head.waveType == 1) // 16-bits per sample (NO TESTED)
                {
                    Byte[] data = new Byte[strm.data.data.Length - (int)strm.head.loopOffset];
                    Array.Copy(strm.data.data, strm.head.loopOffset, data, 0, data.Length);
                    strm.data.data = data;
                }
            }

            // Create the WAV structure from the STRM data
            wav = WAV.Create_WAV(strm.head.channels, strm.head.sampleRate, 16, strm.data.data);

            return(wav);
        }
Пример #5
0
        private void btnReproducir_Click(object sender, EventArgs e)
        {
            try
            {
                Cursor = Cursors.WaitCursor;
                btnStop.PerformClick();

                if (File.Exists(wavFile))
                {
                    File.Delete(wavFile);
                }
                if (File.Exists(loopFile))
                {
                    File.Delete(loopFile);
                }

                string sound = SaveFile();
                wavFile = Path.GetTempFileName();
                if (checkLoop.Checked)
                {
                    loopFile = Path.GetTempFileName();
                }

                switch (SearchFile().type)
                {
                case FormatSound.SWAV:
                    WAV.Write(SWAV.ConvertToWAV(SWAV.Read(sound), false), wavFile);
                    if (checkLoop.Checked)
                    {
                        WAV.Write(SWAV.ConvertToWAV(SWAV.Read(sound), true), loopFile);
                    }
                    break;

                case FormatSound.STRM:
                    WAV.Write(STRM.ConvertToWAV(STRM.Read(sound), false), wavFile);
                    if (checkLoop.Checked)
                    {
                        WAV.Write(STRM.ConvertToWAV(STRM.Read(sound), true), loopFile);
                    }
                    break;
                }

                File.Delete(sound);

                if (checkLoop.Checked)
                {
                    bgdWorker = new Thread(bgdWorker_DoWork);
                    bgdWorker.Start(new String[] { wavFile, loopFile });
                }
                else
                {
                    soundPlayer = new SoundPlayer(wavFile);
                    soundPlayer.Play();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                Console.WriteLine(ex.Message);
            }
            finally
            {
                Cursor = Cursors.Default;
            }
        }
Пример #6
0
        private void btnImport_Click(object sender, EventArgs e)
        {
            Sound  selectedFile = SearchFile();
            String fileout      = pluginHost.Get_TempFolder() + Path.DirectorySeparatorChar + Path.GetRandomFileName();

            OpenFileDialog o = new OpenFileDialog();

            o.CheckFileExists = true;
            o.AddExtension    = true;
            o.DefaultExt      = "wav";
            o.Filter          = "WAVE audio format (*.wav)|*.wav";
            if (o.ShowDialog() != DialogResult.OK)
            {
                return;
            }
            String filein = o.FileName;

            sWAV wav = new sWAV();

            try { wav = WAV.Read(filein); }
            catch (Exception ex) { MessageBox.Show(ex.Message); return; }

            NewAudioOptions dialog = new NewAudioOptions(pluginHost, (selectedFile.type == FormatSound.SWAV ? true : false));

            switch (selectedFile.type)
            {
            case FormatSound.STRM:
                sSTRM oldStrm = STRM.Read(SaveFile());
                dialog.Compression = oldStrm.head.waveType;
                dialog.BlockSize   = (int)oldStrm.head.blockLen;
                dialog.Loop        = (oldStrm.head.loop != 0 ? true : false);
                dialog.LoopOffset  = (int)oldStrm.head.loopOffset;
                dialog.SampleRate  = (int)wav.wave.fmt.sampleRate;
                if (dialog.ShowDialog() != DialogResult.OK)
                {
                    return;
                }

                sSTRM strm = STRM.ConvertToSTRM(wav, dialog.Compression);
                strm.head.loop       = (byte)(dialog.Loop ? 0x01 : 0x00);
                strm.head.loopOffset = (uint)dialog.LoopOffset;

                STRM.Write(strm, fileout);
                break;

            case FormatSound.SWAV:
                sSWAV oldSwav = SWAV.Read(SaveFile());
                dialog.Compression = oldSwav.data.info.nWaveType;
                dialog.Loop        = (oldSwav.data.info.bLoop != 0 ? true : false);
                dialog.LoopLength  = (int)oldSwav.data.info.nNonLoopLen;
                dialog.LoopOffset  = (int)oldSwav.data.info.nLoopOffset;
                dialog.SampleRate  = (int)wav.wave.fmt.sampleRate;
                if (dialog.ShowDialog() != DialogResult.OK)
                {
                    return;
                }

                sSWAV swav = SWAV.ConvertToSWAV(wav, dialog.Compression, dialog.Volume);

                swav.data.info.bLoop       = (byte)(dialog.Loop ? 0x01 : 0x00);
                swav.data.info.nLoopOffset = (ushort)dialog.LoopOffset;
                swav.data.info.nNonLoopLen = (uint)dialog.LoopLength;

                SWAV.Write(swav, fileout);
                break;

            case FormatSound.SSEQ:
            case FormatSound.SSAR:
            case FormatSound.SBNK:
            case FormatSound.SWAR:
            default:
                break;
            }

            if (!File.Exists(fileout))
            {
                return;
            }
            ChangeFile((int)selectedFile.id, fileout);
        }