public void Connect() { if (_ezb.EZBType != EZB.EZ_B_Type_Enum.ezb4) { return; } Disconnect(); if (!_ezb.IsConnected) { return; } if (_bw == null) { _bw = new EZBGWorker("MIP Controller"); _bw.DoWork += _bw_DoWork; } Init(UART_PORT); if (_bw.IsBusy) { _bw.RunWorkerAsync(); } }
/// <summary> /// Stream raw audio data to the EZ-B v4's speakers. /// 0 is silent, 100 is normal, 200 is 2x gain, 300 is 3x gain, etc. /// The audio must be RAW 8 Bit at 18 KHZ Sample Rate /// </summary> public async Task PlayData(Stream ms, decimal volume, int[] progressPositions, int playFromSample) { await Stop(); if (!_ezb.IsConnected) { return; } if (_ezb.EZBType != EZB.EZ_B_Type_Enum.ezb4) { _ezb.Log(false, "This feature is only available for EZ-B v4"); return; } if (_ms != null) { _ms.Dispose(); } _ms = new MemoryStream(); ms.CopyTo(_ms); byte[] bTmp = new byte[PREBUFFER_SIZE]; _ms.Write(bTmp, 0, bTmp.Length); _playFromSample = playFromSample; _ms.Position = playFromSample; _progressPositions = progressPositions; Volume = volume; _threadAudio.RunWorkerAsync(); }
private void startWorker(Classes.HT16K33AnimatorAction action) { _tf.RunWorkerAsync(action); }
private void startWorker() { _worker.RunWorkerAsync(); }
private async Task _threadAudio_DoWork(Object sender, DoWorkEventArgs e) { try { if (OnStartPlaying != null) { OnStartPlaying(); } await _ezb.sendCommand(EZB.CommandEnum.CmdSoundStreamCmd, (byte)EZB.CmdSoundv4Enum.CmdSoundInitStop); bool playing = false; int position = 0; _sw.Restart(); _threadProgress.RunWorkerAsync(); do { byte[] bTmp = new byte[PACKET_SIZE]; int bytesRead = _ms.Read(bTmp, 0, PACKET_SIZE); position += bytesRead; byte[] bArray = new byte[bytesRead]; bool isClipping = false; int highest = 0; int lowest = 255; int average = 0; long total = 0; decimal volumeMultiplier = Volume / 100m; for (int x = 0; x < bytesRead; x++) { decimal newVal = (decimal)bTmp[x]; if (newVal > 128) { newVal = Math.Max(128, 128 + ((newVal - 128) * volumeMultiplier)); } else if (newVal < 128) { newVal = Math.Min(128, 128 - ((128 - newVal) * volumeMultiplier)); } if (newVal > 255) { newVal = 255; isClipping = true; } else if (newVal < 0) { newVal = 0; isClipping = true; } highest = Math.Max(highest, (int)newVal); lowest = Math.Min(lowest, (int)newVal); total += (int)newVal; bArray[x] = (byte)newVal; } average = (int)(total / bytesRead); List <byte> dataTmp = new List <byte>(); dataTmp.Add((byte)EZB.CmdSoundv4Enum.CmdSoundLoad); dataTmp.AddRange(BitConverter.GetBytes((UInt16)bArray.Length)); dataTmp.AddRange(bArray); await _ezb.sendCommand(EZB.CommandEnum.CmdSoundStreamCmd, dataTmp.ToArray()); if (!playing && position > PREBUFFER_SIZE) { await _ezb.sendCommand(EZB.CommandEnum.CmdSoundStreamCmd, (byte)EZB.CmdSoundv4Enum.CmdSoundPlay); playing = true; } if (OnAudioDataChanged != null) { OnAudioDataChanged(dataTmp.ToArray(), lowest, highest, average); } if (OnClippingStatus != null) { OnClippingStatus(isClipping); } float runtime = (float)_sw.ElapsedMilliseconds; float shouldSend = ((runtime * AUDIO_SAMPLE_BITRATE) / 1000) + PREBUFFER_SIZE; float difference = position - shouldSend; if (difference > 0) { float delay = (difference / AUDIO_SAMPLE_BITRATE) * 1000; await Task.Delay((int)delay); } } while (position < _ms.Length && _ezb.IsConnected && !_threadAudio.CancellationPending); } catch (Exception ex) { _ezb.Log(false, "Error Streaming Audio: {0}", ex); } }