private void MainForm_Load(object sender, EventArgs e) { Fpcalc.Path = @"D:\Projects\AcoustId\extern\fpcalc.exe"; decoder = new NAudioDecoder(); //decoder = new BassDecoder(); }
private void btnOpen_Click(object sender, EventArgs e) { OpenFileDialog dlg = new OpenFileDialog(); dlg.Filter = "MP3 files (*.mp3)|*.mp3|WAV files (*.wav)|*.wav"; if (dlg.ShowDialog() == DialogResult.OK) { if (decoder != null) { decoder.Dispose(); } try { decoder = new NAudioDecoder(dlg.FileName); //decoder = new BassDecoder(); lbFile.Text = dlg.FileName; ResetAll(); int bits = decoder.Format.BitDepth; int channels = decoder.Format.Channels; lbAudio.Text = String.Format("{0}Hz, {1}bit{2}, {3}", decoder.Format.SampleRate, bits, bits != 16 ? " (not supported)" : "", channels == 2 ? "stereo" : (channels == 1 ? "mono" : "multi-channel")); lbDuration.Text = decoder.Format.Duration.ToString(); btnFingerPrint.Enabled = true; } catch { lbAudio.Text = "Failed to load audio"; lbDuration.Text = String.Empty; } } }
public int Open(string FileName) { if (_FileOpened) return -1; if (!System.IO.File.Exists(FileName)) return -1; if (_FileOpened) return -1; _Decoder = new CAudioDecoderFFmpeg(); _Decoder.Init(); try { _source = AL.GenSource(); _buffers = AL.GenBuffers(buffer_count); _state = 0; //AL.SourceQueueBuffers(_source, _buffers.Length, _buffers); } catch (Exception) { _Initialized = false; CLog.LogError("Error Init OpenAL Playback"); return -1; } _FileName = FileName; _Decoder.Open(FileName); _Duration = _Decoder.GetLength(); _format = _Decoder.GetFormatInfo(); _ByteCount = 2 * _format.ChannelCount; _BytesPerSecond = _format.SamplesPerSecond * _ByteCount; _CurrentTime = 0f; _Timer.Reset(); AudioStreams stream = new AudioStreams(0); stream.handle = _buffers[0]; if (stream.handle != 0) { _FileOpened = true; _data = new RingBuffer(BUFSIZE); _NoMoreData = false; _DecoderThread.Priority = ThreadPriority.Normal; _DecoderThread.Name = Path.GetFileName(FileName); _DecoderThread.Start(); return stream.handle; } return -1; }
private void Process(IAudioDecoder decoder) { int counter = 0; AudioRecord item; while ((item = GetItemFromQueue()) != null) { try { counter++; //decode audio source to samples and mp3 tags extracting AudioInfo info = null; using (var stream = item.GetSourceStream()) info = decoder.Decode(stream, targetBitRate, item.GetSourceExtension()); //normalize volume level info.Samples.Normalize(); //launch sample processors foreach (var processor in factory.CreateSampleProcessors()) try { processor.Process(item, info); } catch (Exception E) { } //OnProgress(new ProgressChangedEventArgs(100 * (itemsCount - recordsQueue.Count) / itemsCount, null)); item.State = RecordState.Processed; } catch (Exception E) { item.State = RecordState.Bad; } OnProcessingProgress(new ProgressChangedEventArgs(100 * (itemsCount - recordsQueue.Count) / itemsCount, null)); } }
private void ProcessMultiThreading(IAudioDecoder decoder) { var threads = new List<Thread>(); var threadCount = 1; for (int i = 0; i < threadCount; i++) { var t = new Thread(() => Process(decoder)) { IsBackground = true }; t.Start(); threads.Add(t); } while (recordsQueue.Count > 0) { Thread.Sleep(100); } foreach (var t in threads) { t.Join(); } }
/// <summary> /// Gets audio from queue and process it /// </summary> protected virtual void AudioAnalys(IAudioDecoder decoder) { int counter = 0; Audio item; while ((item = GetItemFromQueue()) != null) try { counter++; //decode audio source to samples and mp3 tags extracting AudioInfo info = null; using (var stream = item.GetSourceStream()) { info = decoder.Decode(stream, TargetBitrate, item.GetSourceExtension()); } //normalize volume level info.Samples.Normalize(); //launch sample processors foreach (var processor in factory.CreateSampleProcessors()) { try { processor.Process(item, info); } catch (Exception E) { Logger.WarnException("Audio processor exception.", E); } } OnProgress(new ProgressChangedEventArgs(100 * (itemsCount - audioSourceQueue.Count) / itemsCount, null)); item.State = AudioState.Processed; } catch (Exception E) { Logger.ErrorException("Audio processing failed.", E); item.State = AudioState.Bad; } }
protected virtual void ProcessMultithreading(IAudioDecoder decoder) { var threads = new List<Thread>(); //create threads var threadCount = Environment.ProcessorCount; for (int i = 0; i < threadCount; i++) { var t = new Thread(() => AudioAnalys(decoder)) { IsBackground = true }; t.Start(); threads.Add(t); } while (audioSourceQueue.Count > 0) { Thread.Sleep(100); } foreach (var t in threads) { t.Join(); } }
public TagDialog() { InitializeComponent(); decoder = new NAudioDecoder(); }
public int Open(string FileName) { if (_FileOpened) { return(-1); } if (!System.IO.File.Exists(FileName)) { return(-1); } if (_FileOpened) { return(-1); } _Decoder = new CAudioDecoderFFmpeg(); _Decoder.Init(); try { _source = AL.GenSource(); _buffers = AL.GenBuffers(buffer_count); _state = 0; //AL.SourceQueueBuffers(_source, _buffers.Length, _buffers); } catch (Exception) { _Initialized = false; CLog.LogError("Error Init OpenAL Playback"); return(-1); } _FileName = FileName; _Decoder.Open(FileName); _Duration = _Decoder.GetLength(); _format = _Decoder.GetFormatInfo(); _ByteCount = 2 * _format.ChannelCount; _BytesPerSecond = _format.SamplesPerSecond * _ByteCount; _CurrentTime = 0f; _Timer.Reset(); AudioStreams stream = new AudioStreams(0); stream.handle = _buffers[0]; if (stream.handle != 0) { _FileOpened = true; _data = new RingBuffer(BUFSIZE); _NoMoreData = false; _DecoderThread.Priority = ThreadPriority.Normal; _DecoderThread.Name = Path.GetFileName(FileName); _DecoderThread.Start(); return(stream.handle); } return(-1); }
private void initOpusDecoder() { audioDecoder = new OpusDecoder(sampleRate, channels, this); audioDecoder.start(); }
private void SuspendImmediate() { StopImmediate(); // Set to null to prevent disposing the decoder on next Play(). decoder = null; }
public override bool Open(bool prescan) { Debug.Assert(!_FileOpened); if (_FileOpened) { return(false); } if (!File.Exists(_Medium)) { Dispose(); return(false); } try { _PaHandle = new CPortAudioHandle(); int hostApi = _PaHandle.GetHostApi(); _ApiInfo = PortAudioSharp.PortAudio.Pa_GetHostApiInfo(hostApi); _OutputDeviceInfo = PortAudioSharp.PortAudio.Pa_GetDeviceInfo(_ApiInfo.defaultOutputDevice); if (_OutputDeviceInfo.defaultLowOutputLatency < 0.1) { _OutputDeviceInfo.defaultLowOutputLatency = 0.1; } _PaStreamCallback = _ProcessNewData; } catch (Exception) { Dispose(); CLog.LogError("Error Init PortAudio Playback"); return(false); } _Decoder = new CAudioDecoderFFmpeg(); if (!_Decoder.Open(_Medium)) { Dispose(); CLog.LogError("Error opening audio file: " + _Medium); return(false); } SFormatInfo format = _Decoder.GetFormatInfo(); if (format.SamplesPerSecond == 0) { Dispose(); CLog.LogError("Error Init PortAudio Playback (samples=0)"); return(false); } Length = _Decoder.GetLength(); _ByteCount = 2 * format.ChannelCount; _BytesPerSecond = format.SamplesPerSecond * _ByteCount; _SyncTimer.Pause(); _SyncTimer.Time = 0f; PortAudioSharp.PortAudio.PaStreamParameters?outputParams = new PortAudioSharp.PortAudio.PaStreamParameters { channelCount = format.ChannelCount, device = _ApiInfo.defaultOutputDevice, sampleFormat = PortAudioSharp.PortAudio.PaSampleFormat.paInt16, suggestedLatency = _OutputDeviceInfo.defaultLowOutputLatency, hostApiSpecificStreamInfo = IntPtr.Zero }; if (!_PaHandle.OpenOutputStream( out _Stream, ref outputParams, format.SamplesPerSecond, (uint)CConfig.Config.Sound.AudioBufferSize / 2, PortAudioSharp.PortAudio.PaStreamFlags.paNoFlag, _PaStreamCallback, IntPtr.Zero) || _Stream == IntPtr.Zero) { Dispose(); return(false); } _Latency = CConfig.Config.Sound.AudioLatency / 1000f + (float)PortAudioSharp.PortAudio.Pa_GetStreamInfo(_Stream).outputLatency; //From now on closing the driver and the decoder is handled by the thread ONLY! _Paused = true; _FileOpened = true; _Data = new CRingBuffer(_Bufsize); _NoMoreData = false; _DecoderThread = new Thread(_Execute) { Priority = ThreadPriority.Normal, Name = Path.GetFileName(_Medium) }; _DecoderThread.Start(); return(true); }
public int Open(string FileName) { if (_FileOpened) return -1; if (!System.IO.File.Exists(FileName)) return -1; if (_FileOpened) return -1; _Decoder = new CAudioDecoderFFmpeg(); _Decoder.Init(); try { if (errorCheck("Initialize", PortAudio.Pa_Initialize())) return -1; _Initialized = true; int hostApi = apiSelect(); _apiInfo = PortAudio.Pa_GetHostApiInfo(hostApi); _outputDeviceInfo = PortAudio.Pa_GetDeviceInfo(_apiInfo.defaultOutputDevice); _paStreamCallback = new PortAudio.PaStreamCallbackDelegate(_PaStreamCallback); if (_outputDeviceInfo.defaultLowOutputLatency < 0.1) _outputDeviceInfo.defaultLowOutputLatency = 0.1; } catch (Exception) { _Initialized = false; CLog.LogError("Error Init PortAudio Playback"); return -1; } _FileName = FileName; _Decoder.Open(FileName); _Duration = _Decoder.GetLength(); FormatInfo format = _Decoder.GetFormatInfo(); _ByteCount = 2 * format.ChannelCount; _BytesPerSecond = format.SamplesPerSecond * _ByteCount; _CurrentTime = 0f; _SyncTimer.Time = _CurrentTime; AudioStreams stream = new AudioStreams(0); IntPtr data = new IntPtr(0); PortAudio.PaStreamParameters outputParams = new PortAudio.PaStreamParameters(); outputParams.channelCount = format.ChannelCount; outputParams.device = _apiInfo.defaultOutputDevice; outputParams.sampleFormat = PortAudio.PaSampleFormat.paInt16; outputParams.suggestedLatency = _outputDeviceInfo.defaultLowOutputLatency; uint bufsize = (uint)CConfig.AudioBufferSize; errorCheck("OpenDefaultStream", PortAudio.Pa_OpenStream( out _Ptr, IntPtr.Zero, ref outputParams, format.SamplesPerSecond, bufsize, PortAudio.PaStreamFlags.paNoFlag, _paStreamCallback, data)); stream.handle = _Ptr.ToInt32(); if (stream.handle != 0) { _Paused = true; _waiting = true; _FileOpened = true; _data = new RingBuffer(BUFSIZE); _NoMoreData = false; _DecoderThread.Priority = ThreadPriority.Normal; _DecoderThread.Name = Path.GetFileName(FileName); _DecoderThread.Start(); return stream.handle; } return -1; }