コード例 #1
0
        public void TrimWavFile(WaveFileReader reader, VoiceAudioStream writer, long startPos, long endPos)
        {
            reader.Position = startPos;
            var buffer = new byte[1024];

            while (reader.Position < endPos)
            {
                var bytesRequired = (int)(endPos - reader.Position);
                if (bytesRequired <= 0)
                {
                    continue;
                }

                var bytesToRead = Math.Min(bytesRequired, buffer.Length);
                var bytesRead   = reader.Read(buffer, 0, bytesToRead);
                if (bytesRead > 0)
                {
                    writer.Write(buffer, 0, bytesRead);
                }
            }
        }
コード例 #2
0
        /// <summary>
        /// Processes this instance.
        /// </summary>
        private async Task _process()
        {
            _currentAudioProcessor = new AudioProcessor(_settings);

            if (_settings.CaptureEvents && !_isDraining && _capture == null)
            {
                _capture = new CaptureEvents(Path.Combine(Path.GetTempPath(), BotConstants.DefaultOutputFolder, _settings.EventsFolder, _mediaId, "media"));
            }

            try
            {
                while (await _buffer.OutputAvailableAsync(_tokenSource.Token).ConfigureAwait(false))
                {
                    SerializableAudioMediaBuffer data = await _buffer.ReceiveAsync(_tokenSource.Token).ConfigureAwait(false);

                    if (!data.IsSilence)
                    {
                        if (data.SerializableUnmixedAudioBuffers != null)
                        {
                            foreach (var s in data.SerializableUnmixedAudioBuffers)
                            {
                                if (string.IsNullOrWhiteSpace(s.AdId) || string.IsNullOrWhiteSpace(s.DisplayName))
                                {
                                    continue;
                                }
                                _audioStream.Write(s.Buffer, 0, s.Buffer.Length);
                            }
                        }
                    }

                    if (_settings.CaptureEvents)
                    {
                        await _capture?.Append(data);
                    }

                    await _currentAudioProcessor.Append(data);

                    _tokenSource.Token.ThrowIfCancellationRequested();
                }
            }
            catch (TaskCanceledException ex)
            {
                _logger.Error(ex, "The queue processing task has been cancelled.");
            }
            catch (ObjectDisposedException ex)
            {
                _logger.Error(ex, "The queue processing task object has been disposed.");
            }
            catch (Exception ex)
            {
                // Catch all other exceptions and log
                _logger.Error(ex, "Caught Exception");

                // Continue processing elements in the queue
                await _process().ConfigureAwait(false);
            }

            //send final segment as a last precation in case the loop did not process it
            if (_currentAudioProcessor != null)
            {
                await _chunkProcess();
            }

            if (_settings.CaptureEvents)
            {
                await _capture?.Finalise();
            }

            _isDraining = false;
        }