Beispiel #1
0
        private void checkBoxSaveToWave_CheckedChanged(object sender, EventArgs e)
        {
            if (_asioIn != null)
            {
                if (_wavEncoder != null)
                {
                    // stop the encoder
                    _wavEncoder.Stop();
                    _wavEncoder = null;
                }

                if (checkBoxSaveToWave.Checked)
                {
                    // setup an encoder on the asio input channel
                    // Note: this will write a 32-bit, 48kHz, stereo Wave file
                    _wavEncoder            = new EncoderWAV(_asioIn.InputChannel);
                    _wavEncoder.InputFile  = null; // use STDIN (the above channel)
                    _wavEncoder.OutputFile = Path.Combine(Application.StartupPath, "output.wav");
                    _wavEncoder.Start(null, IntPtr.Zero, false);
                }
            }
        }
        //複数のサウンドを合体(Posにマイナスが含まれている場合バグります
        //引数:合体させるファイル, サウンドの開始位置, 音量, 再生速度, 保存先, MP3として保存するか(falseの場合は.wav形式), 元のファイルを削除するか
        public static void Sound_Combine(List <string> Files, List <double> Pos, List <double> Volume, List <double> Speed, string To_File, bool IsEncodeMP3, bool IsFromFileDelete = false)
        {
            int        mixer            = BassMix.BASS_Mixer_StreamCreate(48000, 2, BASSFlag.BASS_STREAM_DECODE);
            long       Mixer_Max_Length = 0;
            List <int> Streams          = new List <int>();
            List <int> Stream_Handles   = new List <int>();

            //ファイルをミックスする
            for (int Number = 0; Number < Files.Count; Number++)
            {
                Streams.Add(Bass.BASS_StreamCreateFile(Files[Number], 0, 0, BASSFlag.BASS_SAMPLE_FLOAT | BASSFlag.BASS_STREAM_DECODE));
                Stream_Handles.Add(BassFx.BASS_FX_TempoCreate(Streams[Streams.Count - 1], BASSFlag.BASS_STREAM_DECODE | BASSFlag.BASS_FX_FREESOURCE));
                float Freq = 48000;
                Bass.BASS_ChannelGetAttribute(Stream_Handles[Stream_Handles.Count - 1], BASSAttribute.BASS_ATTRIB_TEMPO_FREQ, ref Freq);
                Bass.BASS_ChannelSetAttribute(Stream_Handles[Stream_Handles.Count - 1], BASSAttribute.BASS_ATTRIB_TEMPO_FREQ, Freq * (float)Speed[Number]);
                Bass.BASS_ChannelSetAttribute(Stream_Handles[Stream_Handles.Count - 1], BASSAttribute.BASS_ATTRIB_VOL, (float)(Volume[Number] / 100));
                long start = Bass.BASS_ChannelSeconds2Bytes(mixer, Pos[Number]);
                BassMix.BASS_Mixer_StreamAddChannelEx(mixer, Stream_Handles[Stream_Handles.Count - 1], BASSFlag.BASS_SAMPLE_FLOAT | BASSFlag.BASS_STREAM_DECODE, start, 0);
                long Now_Stream_Length = Bass.BASS_ChannelGetLength(Streams[Streams.Count - 1], BASSMode.BASS_POS_BYTES);
                if (Mixer_Max_Length < Now_Stream_Length + start)
                {
                    Mixer_Max_Length = Now_Stream_Length + start;
                }
            }
            //.wavでエンコード(こっちの方が速い + 安定性が高い)
            EncoderWAV l = new EncoderWAV(mixer);

            l.InputFile         = null;
            l.OutputFile        = To_File + ".tmp";
            l.WAV_BitsPerSample = 24;
            l.Start(null, IntPtr.Zero, false);
            byte[] encBuffer = new byte[65536];
            while (Bass.BASS_ChannelIsActive(mixer) == BASSActive.BASS_ACTIVE_PLAYING)
            {
                int len = Bass.BASS_ChannelGetData(mixer, encBuffer, encBuffer.Length);
                if (len <= 0)
                {
                    break;
                }
                else if (Mixer_Max_Length <= Bass.BASS_ChannelGetPosition(mixer, BASSMode.BASS_POS_BYTES))
                {
                    break;
                }
            }
            l.Stop();
            //メモリ解放
            Bass.BASS_StreamFree(mixer);
            foreach (int Stream in Stream_Handles)
            {
                Bass.BASS_StreamFree(Stream);
            }
            foreach (int Stream in Streams)
            {
                Bass.BASS_StreamFree(Stream);
            }
            //MP3形式にエンコード
            if (IsEncodeMP3)
            {
                Un4seen.Bass.Misc.EncoderLAME mc = new Un4seen.Bass.Misc.EncoderLAME(0);
                mc.EncoderDirectory = Voice_Set.Special_Path + "/Encode_Mp3";
                mc.InputFile        = To_File + ".tmp";
                mc.OutputFile       = To_File;
                mc.LAME_Bitrate     = (int)Un4seen.Bass.Misc.EncoderLAME.BITRATE.kbps_144;
                mc.LAME_Mode        = Un4seen.Bass.Misc.EncoderLAME.LAMEMode.Default;
                mc.LAME_Quality     = Un4seen.Bass.Misc.EncoderLAME.LAMEQuality.Q2;
                Un4seen.Bass.Misc.BaseEncoder.EncodeFile(mc, null, true, false, true);
                mc.Dispose();
                File.Delete(To_File + ".tmp");
            }
            else
            {
                Sub_Code.File_Move(To_File + ".tmp", To_File, true);
            }
            if (File.Exists(To_File) && IsFromFileDelete)
            {
                foreach (string File_Now in Files)
                {
                    Sub_Code.File_Delete_V2(File_Now);
                }
            }
        }