static void Main(string[] args) { Application.Init(); WaveOutEvent outputDevice = new WaveOutEvent(); string musicPath = GetFilepath(); AudioFileReader musicDataReader = new AudioFileReader(musicPath); AudioFileReader musicPlaybackReader = new AudioFileReader(musicPath); SerialConnection.Start(); Console.WriteLine("Connection open!"); outputDevice.Init(musicPlaybackReader); outputDevice.Play(); while (outputDevice.PlaybackState == PlaybackState.Playing) { if (musicDataReader.HasData(currentSentPosition)) { musicDataReader.Position = musicPlaybackReader.Position; musicDataReader.Read(amplitudeBuffer, 0, amplitudeBuffer.Length); AudioData lightCounts = AudioMethods.GetFrequencyBands(amplitudeBuffer); byte[] bytesSent = SerialConnection.SendFrequencyBandData(lightCounts); foreach (byte byteSent in bytesSent) { Console.Write(byteSent + ", "); } Console.WriteLine(""); currentSentPosition += amplitudeBuffer.Length; } } SerialConnection.SendData(new byte[48]); }
private void WriteWaveFile(SfxDefinition sfxDefinition, float[] inBuffer, byte[] outBuffer) { var offset = 0; var startOffset = 0; _writer.StartObject(ObjectType.Audio, "sfx"); foreach (var wave in sfxDefinition.Waveforms) { using (var audioFile = new AudioFileReader(wave.Value.SoundFile.FromInputFolder())) { while (audioFile.HasData(BufferSize)) { var sampleCount = audioFile.Read(inBuffer, 0, BufferSize); if (sampleCount > 0) { for (var i = 0; i < BufferSize; i++) { outBuffer[i] = (byte)(127 * inBuffer[i]); } _writer.WriteBlob(outBuffer, sampleCount); if (sampleCount % 2 != 0) // make wave ends on even bytes { sampleCount++; _writer.WriteByte(0); } offset += sampleCount; } } } _waveOffsets.Add(wave.Key, startOffset); _waveLengths.Add(wave.Key, offset - startOffset); startOffset = offset; } _writer.EndObject(); }