コード例 #1
0
        public override void Open(AudioWriter audio)
        {
            //Download FFMPEG if we haven't
            DownloadFFMPEG();

            //Obtain our next path
            filename = GetNextTempOutputFile();

            //Get the format
            string format;

            switch (audio.Format)
            {
            case WavSampleFormat.PCM8: format = "u8"; break;

            case WavSampleFormat.PCM16: format = "s16le"; break;

            case WavSampleFormat.Float32: format = "f32le"; break;

            default: throw new Exception("Unknown format!");
            }

            //Start FFMPEG
            ffmpeg = Process.Start(new ProcessStartInfo
            {
                FileName              = "rp_lib_ffmpeg.exe",
                Arguments             = $"-f {format} -ar {audio.SampleRate} -ac {audio.channels} -i pipe: -f mp3 " + filename,
                RedirectStandardInput = true,
                UseShellExecute       = false,
                CreateNoWindow        = true,
                WindowStyle           = ProcessWindowStyle.Hidden
            });
        }
コード例 #2
0
        public IPlayAudio Play(string source)
        {
            AudioWriter audio = new AudioWriter(this, source);

            _says.Add(audio);
            return(audio);
        }
コード例 #3
0
        static void ReadWriteAudio(string input, string output)
        {
            var audio = new AudioReader(input);

            audio.LoadMetadataAsync().Wait();
            audio.Load();

            using (var writer = new AudioWriter(output, audio.Metadata.Channels, audio.Metadata.SampleRate))
            {
                writer.OpenWrite(true);

                var frame = new AudioFrame(1);
                while (true)
                {
                    // read next sample
                    var f = audio.NextFrame(frame);
                    if (f == null)
                    {
                        break;
                    }

                    writer.WriteFrame(frame);
                }
            }
        }
コード例 #4
0
        private bool CreateAudioWriter()
        {
            string audio_path = GetWavAudioName();

            mAudioWriter = new AudioWriter(audio_path);
            return(true);
        }
コード例 #5
0
    //Evaluates both the average and collectino of samples
    public void Evaluate(float avg, Queue <float> sampleList)
    {
        if (_sentences.Count > 0)
        {
            _sentences.Last.Value.Samples = sampleList;
        }

        //If volume is below silence threshold
        if (avg < _VOLUME_SILENCE)
        {
            _silenceTimer += Time.deltaTime;

            //If silence time has reached minimum threshold
            if (_silenceTimer > minSilenceTime)
            {
                _breakFlag     = true;
                _sentenceStart = false;

                if (_sentences.Count > 0)
                {
                    _sentences.Last.Value.EndSentence(_silenceTimer);

                    if (writeToWAV)
                    {
                        _writtenSentences++;
                        string s = AudioWriter.WriteToWAV(filename + _writtenSentences + ".wav", _sampleRate, _sentences.Last.Value.Samples);
                        Debug.Log("Sentence successfully written to " + s);
                        Debug.Log("# of samples = " + _sentences.Last.Value.Samples.Count);
                    }
                }
            }

            //If silent for too long
            if (_silenceTimer > maxSilenceTime)
            {
                _pauseFlag  = true;
                prompt.text = triggeredText;
            }
            else
            {
                prompt.text = untriggeredText;
            }
        }
        //If sound is detected
        else
        {
            _silenceTimer = 0;
            _breakFlag    = _pauseFlag = false;

            //If sentence hasn't been started
            if (!_sentenceStart)
            {
                _sentences.AddLast(new Sentence(_silenceTimer));
                _sentenceStart = true;
            }

            _sentences.Last.Value.StreamChart = avg;
        }
    }
コード例 #6
0
        public override void Open(AudioWriter audio)
        {
            //Set
            this.audio = audio;
            tempPaths  = new List <string>();

            //Open first file
            InternalOpenNewActiveFile();
        }
コード例 #7
0
 private bool DestroyAudioWriter()
 {
     if (mAudioWriter != null)
     {
         mAudioWriter.Dispose();
         mAudioWriter = null;
     }
     return(true);
 }
コード例 #8
0
 public void Start()
 {
     if (Writer == null)
     {
         Writer = GetComponent <AudioWriter>();
     }
     Writer.OnLevelChange += _OnLevelChange;
     Writer.OnFreqChange  += _OnFreqChange;
 }
コード例 #9
0
ファイル: ConversionsBase.cs プロジェクト: pclancy/yeti
 protected static void WriteToStream(AudioWriter writer, Stream inputStream, byte[] buffer)
 {
     int read;
     while ((read = inputStream.Read(buffer, 0, buffer.Length)) > 0)
     {
         writer.Write(buffer, 0, read);
         writer.Flush();
     }
 }
コード例 #10
0
        void DownloadCompletedEvent(object sender, DownloadDataCompletedEventArgs e)
        {
            var pathtoFile = currentDir + "/Audio." + ((Uri)e.UserState).Segments.Last().Split('.')[1];
            var audioData  = new AudioData(e.Result, new Uri(pathtoFile));

            AudioWriter.ByteArrayToFile(audioData.uri.AbsolutePath, audioData.Data);


            OnDownloadAndWriteCompleted(audioData);
        }
コード例 #11
0
ファイル: ConversionsBase.cs プロジェクト: 4dvn/yeti
        protected static void WriteToStream(AudioWriter writer, Stream inputStream, byte[] buffer)
        {
            int read;

            while ((read = inputStream.Read(buffer, 0, buffer.Length)) > 0)
            {
                writer.Write(buffer, 0, read);
                writer.Flush();
            }
        }
コード例 #12
0
        private string SaveAudioFromBufferToFile()
        {
            try
            {
                string fileName = GetWavAudioChildName((mListOfflineAudioFile.Count + 1).ToString());
                Queue <WaveInEventArgs> remainRawData = new Queue <WaveInEventArgs>();

                double      savedAudioLengthInArrayLength = 0;
                double      saveAudioLengthInSecond       = 0;
                int         remainAudioLength             = 0;
                AudioWriter wavWriter = new AudioWriter(fileName);
                lock (balanceLock)
                {
                    while (saveAudioLengthInSecond < Int16.Parse(mRecordingTime) * 60 && mRawAudioData.Count > 0)
                    {
                        WaveInEventArgs rawData = mRawAudioData.Dequeue();
                        if (saveAudioLengthInSecond >= (Int16.Parse(mRecordingTime) * 60 - 3))
                        {
                            remainRawData.Enqueue(rawData);
                            remainAudioLength += rawData.BytesRecorded;
                        }

                        wavWriter.WriteStream(rawData.Buffer, rawData.BytesRecorded);

                        savedAudioLengthInArrayLength += rawData.BytesRecorded;
                        saveAudioLengthInSecond        = savedAudioLengthInArrayLength / (16000 * 2);
                    }

                    while (mRawAudioData.Count > 0)
                    {
                        remainRawData.Enqueue(mRawAudioData.Dequeue());
                    }
                    mRawAudioData   = remainRawData;
                    mRawAudioLength = remainAudioLength;
                }
                wavWriter.Dispose();
                string mp3FileName = GetMp3AudioChildPath((mListOfflineAudioFile.Count + 1).ToString());
                wavWriter.WavToMP3(fileName, mp3FileName);
                //Remove wavfile
                File.Delete(fileName);
                return(mp3FileName);
            }
            catch
            {
                return(null);
            }
        }
コード例 #13
0
        public async Task ConversionStreamTest()
        {
            var path  = Res.GetPath(Res.Audio_Mp3);
            var opath = "out-test-v-2.aac";

            try
            {
                using var reader = new AudioReader(path);
                await reader.LoadMetadataAsync();

                var encoder = new AACEncoder
                {
                    Format = "flv"
                };

                using (var filestream = File.Create(opath))
                {
                    using (var writer = new AudioWriter(filestream,
                                                        reader.Metadata.Channels,
                                                        reader.Metadata.SampleRate, 16,
                                                        encoder.Create()))
                    {
                        writer.OpenWrite();

                        reader.Load();

                        await reader.CopyToAsync(writer);
                    }
                }

                using var audio = new AudioReader(opath);
                await audio.LoadMetadataAsync();

                Assert.True(audio.Metadata.Format.FormatName == "flv");
                Assert.True(audio.Metadata.Channels == 2);
                Assert.True(audio.Metadata.Streams.Length == 1);
                Assert.True(Math.Abs(audio.Metadata.Duration - 1.515102) < 0.2);
            }
            finally
            {
                if (File.Exists(opath))
                {
                    File.Delete(opath);
                }
            }
        }
コード例 #14
0
        public async Task ConversionTest()
        {
            var path  = Res.GetPath(Res.Audio_Ogg);
            var opath = "out-test-2.mp3";

            try
            {
                using var reader = new AudioReader(path);
                await reader.LoadMetadataAsync();

                using (var writer = new AudioWriter(opath,
                                                    reader.Metadata.Channels,
                                                    reader.Metadata.SampleRate, 16,
                                                    new MP3Encoder().Create()))
                {
                    writer.OpenWrite();

                    reader.Load();

                    await reader.CopyToAsync(writer);
                }

                using var audio = new AudioReader(opath);
                await audio.LoadMetadataAsync();

                Assert.True(audio.Metadata.Format.FormatName == "mp3");
                Assert.True(audio.Metadata.Channels == 2);
                Assert.True(audio.Metadata.Streams.Length == 1);
                Assert.True(Math.Abs(audio.Metadata.Duration - 1.515102) < 0.2);
            }
            finally
            {
                if (File.Exists(opath))
                {
                    File.Delete(opath);
                }
            }
        }
コード例 #15
0
 private string SaveAllAudioFromBufferFile()
 {
     lock (balanceLock)
     {
         if (mRawAudioData.Count < 20)
         {
             mRawAudioData.Clear();
             mRawAudioLength = 0;
             return(null);
         }
     }
     try
     {
         string      fileName  = GetWavAudioChildName((mListOfflineAudioFile.Count + 1).ToString());
         AudioWriter wavWriter = new AudioWriter(fileName);
         lock (balanceLock)
         {
             while (mRawAudioData.Count > 0)
             {
                 WaveInEventArgs rawData = mRawAudioData.Dequeue();
                 wavWriter.WriteStream(rawData.Buffer, rawData.BytesRecorded);
             }
             mRawAudioLength = 0;
         }
         wavWriter.Dispose();
         string mp3FileName = GetMp3AudioChildPath((mListOfflineAudioFile.Count + 1).ToString());
         wavWriter.WavToMP3(fileName, mp3FileName);
         //Remove wavfile
         File.Delete(fileName);
         return(mp3FileName);
     }
     catch
     {
         return(null);
     }
 }
コード例 #16
0
 public override void Open(AudioWriter audio)
 {
     path = GetNextTempOutputFile();
     fs   = new FileStream(path, FileMode.Create);
 }
コード例 #17
0
 public abstract void Open(AudioWriter audio);
コード例 #18
0
 public CreateSongToAlbumCommandHandler(ViMusicDbContext context, ImageWriter imageWriter, AudioWriter audioWriter)
 {
     _context     = context;
     _imageWriter = imageWriter;
     _audioWriter = audioWriter;
 }
コード例 #19
0
        /// <summary>
        /// Renders a listener to a file, and returns some measurements of the render.
        /// </summary>
        public static RenderStats WriteRender(Listener listener, Track target, AudioWriter writer,
                                              TaskEngine taskEngine, bool dynamicOnly, bool heightOnly)
        {
            RenderStats stats          = new(listener);
            const long  updateInterval = 50000;
            long        rendered       = 0,
                        untilUpdate    = updateInterval;
            double samplesToProgress   = 1.0 / target.Length,
                   samplesToSeconds    = 1.0 / listener.SampleRate;
            bool     customMuting      = dynamicOnly || heightOnly;
            DateTime start             = DateTime.Now;

            while (rendered < target.Length)
            {
                float[] result = listener.Render();

#if DEBUG
                if (rendered > 2500000 && WaveformUtils.GetPeakSigned(result) > .5f)
                {
                    ; // TODO: debug, Amaze will follow with a heavy gain frame and then a normal frame after this detection
                }
#endif
                if (target.Length - rendered < listener.UpdateRate)
                {
                    Array.Resize(ref result, (int)(target.Length - rendered));
                }
                if (writer != null)
                {
                    writer.WriteBlock(result, 0, result.LongLength);
                }
                if (rendered > firstFrame)
                {
                    stats.Update();
                }

                if (customMuting)
                {
                    IReadOnlyList <Source> objects = target.Renderer.Objects;
                    for (int i = 0, c = objects.Count; i < c; ++i)
                    {
                        Vector3 rawPos = objects[i].Position / Listener.EnvironmentSize;
                        objects[i].Mute =
                            (dynamicOnly && MathF.Abs(rawPos.X) % 1 < .01f &&
                             MathF.Abs(rawPos.Y) % 1 < .01f && MathF.Abs(rawPos.Z % 1) < .01f) ||
                            (heightOnly && rawPos.Y == 0);
                    }
                }

                rendered += listener.UpdateRate;
                if ((untilUpdate -= listener.UpdateRate) <= 0)
                {
                    double progress = rendered * samplesToProgress;
                    double speed    = rendered * samplesToSeconds / (DateTime.Now - start).TotalSeconds;
                    taskEngine.UpdateStatusLazy($"Rendering... ({progress:0.00%}, speed: {speed:0.00}x)");
                    taskEngine.UpdateProgressBar(progress);
                    untilUpdate = updateInterval;
                }
            }

            if (writer != null)
            {
                writer.Dispose();
            }
            return(stats);
        }