예제 #1
0
        public void StartRecording()
        {
            if (IsRecording)
            {
                return;
            }

            try {
                log.Debug("Starting recording...");

                log.Debug($"  checking if save path {SettingsFile.Instance.SavePath} exists...");
                if (!Directory.Exists(SettingsFile.Instance.SavePath))
                {
                    Directory.CreateDirectory(SettingsFile.Instance.SavePath);
                }

                var speakerId = SettingsFile.Instance.DefaultSpeakers; //TODO: check skype profile and find matching
                if (String.IsNullOrEmpty(speakerId))
                {
                    speakerId = DeviceHelper.GetDefaultSpeaker();
                }

                var microphoneId = SettingsFile.Instance.DefaultMicrophone;
                if (String.IsNullOrEmpty(microphoneId))
                {
                    microphoneId = DeviceHelper.GetDefaultMicrophone();
                }

                log.Debug($"Using devices: mic = {microphoneId}, spkr = {speakerId}");

                if (!String.IsNullOrEmpty(speakerId))
                {
                    spkSource = new WasapiLoopbackCapture(DeviceHelper.GetDeviceById(speakerId));
                }
                else
                {
                    spkSource = new WasapiLoopbackCapture();
                }

                if (!String.IsNullOrEmpty(microphoneId))
                {
                    micSource = new WasapiCapture(DeviceHelper.GetDeviceById(microphoneId));
                }
                else
                {
                    micSource = new WasapiCapture();
                }

                //spkSource.WaveFormat = new WaveFormat(44100, 1);
                spkSource.DataAvailable    += spkSource_DataAvailable;
                spkSource.RecordingStopped += spkSource_RecordingStopped;
                //micSource.WaveFormat = new WaveFormat(44100, 1);
                micSource.DataAvailable    += micSource_DataAvailable;
                micSource.RecordingStopped += micSource_RecordingStopped;

                var guid = Guid.NewGuid();
                _micSourceFile = Path.Combine(Path.GetTempPath(), $"{guid}_in.wav");
                micWaveFile    = new WaveFileWriter(_micSourceFile, micSource.WaveFormat);
                _spkSourceFile = Path.Combine(Path.GetTempPath(), $"{guid}_out.wav");
                spkWaveFile    = new WaveFileWriter(_spkSourceFile, micSource.WaveFormat);

                log.Debug($"Using temp files: {_micSourceFile}, {_spkSourceFile}");

                micSource.StartRecording();
                spkSource.StartRecording();

                log.Debug($"Started recording...");

                IsRecording = true;
            }
            catch (Exception ex) {
                log.Error("Error starting capture: " + ex.Message, ex);
            }
        }