public void StopRecord() { RecordData recordData = StopRec(); bData = new byte[recordData.len]; Debug.WriteLine(recordData.len); Marshal.Copy(recordData.ip, bData, 0, (int)recordData.len); WaveHeader header = (WaveHeader)Marshal.PtrToStructure(GetWaveform(), typeof(WaveHeader)); wav = new Wav("RIFF", recordData.len + 36, "WAVE", "fmt", 16, 1, header.nChannels, header.nSamplesPerSec, header.nAvgBytesPerSec, header.nBlockAlign, header.wBitsPerSample, "data", recordData.len); short[] temp = new short[recordData.len / (int)wav.blockAlign]; for (int i = 0; i < temp.Length - 1; i++) { temp[i] = BitConverter.ToInt16(bData, i * (int)wav.blockAlign); } samples = temp.Select(x => (double)(x)).ToArray(); plotSample(samples.Length); }
public void readWaveFile() { OpenFileDialog fileDialog = new OpenFileDialog(); if (fileDialog.ShowDialog() == DialogResult.OK) { byte[] wavData = File.ReadAllBytes(fileDialog.FileName); byte[] wavHeader = new List <byte>(wavData).GetRange(0, 44).ToArray(); wav = new WaveFile.Wav(wavHeader); wavData = new List <byte>(wavData).GetRange(44, wavData.Length - 44).ToArray(); Debug.WriteLine("\nOpening File:" + "\nChunk ID: " + wav.RIFFChunkID + "\nRIFF Chuck Size: " + wav.RIFFChuckSize + "\nFormat: " + wav.format + "\nfmt Chuck ID: " + wav.fmtChuckID + "\nfmt Chunk Size: " + wav.fmtChunkSize + "\naudio format: " + wav.audioFormat + "\nNum Channels: " + wav.numChannels + "\nSample Rate: " + wav.sampleRate + "\nByte Rate: " + wav.byteRate + "\nBlock Align: " + wav.blockAlign + "\nBits Per Sample: " + wav.bitsPerSample + "\nData Chunk ID: " + wav.dataChunkID + "\nData Chunk Size: " + wav.dataChunkSize); signalData = new byte[wavData.Length]; signalData = wavData; generateWavSample(wavData); } }
public void TestLoadFile() { var exampleDir = Directory.GetParent(Directory.GetCurrentDirectory()) .Parent.Parent.Parent.FullName + "/examples/"; string wavFilePath = exampleDir + "8k16bitpcm.wav"; Wav wav = new Wav(File.ReadAllBytes(wavFilePath).ToList()); }
void play() { int ct = mDataPtr; int _base = 0; if (mDataFull) { ct = mDataCount; _base = mDataPtr; } if (ct * mSampleStep < .05) { MessageBox.Show("Audio data is not ready yet.\r\nIncrease simulation speed to make data ready sooner."); return; } /* rescale data to maximize */ double max = -1e8; double min = 1e8; for (int i = 0; i != ct; i++) { if (mData[i] > max) { max = mData[i]; } if (mData[i] < min) { min = mData[i]; } } double adj = -(max + min) / 2; double mult = (.25 * 32766) / (max + adj); /* fade in over 1/20 sec */ int fadeLen = mSamplingRate / 20; int fadeOut = ct - fadeLen; double fadeMult = mult / fadeLen; var samples = new short[ct]; for (int i = 0; i != ct; i++) { double fade = (i < fadeLen) ? i * fadeMult : (i > fadeOut) ? (ct - i) * fadeMult : mult; samples[i] = (short)((mData[(i + _base) % mDataCount] + adj) * fade); } var wav = new Wav(mSamplingRate); wav.setBuffer(samples); var srclist = new List <short>(); while (!wav.eof()) { srclist.AddRange(wav.getBuffer(1000)); } }
static bool Noise( string cliScript, Func <bool> IsTerminated) { if (cliScript.StartsWith("--noise")) { cliScript = cliScript.Remove(0, "--noise".Length).Trim(); } else if (cliScript.StartsWith("noise")) { cliScript = cliScript.Remove(0, "noise".Length).Trim(); } else { throw new ArgumentException(); } string wavFile = "noise.wav"; Set filter = new Set( "C3" ); var Model = System.Ai.Mel.Noise(); SaveMidi( Model.GetBuffer(), "MIDI", Path.ChangeExtension(wavFile, ".md")); // Model = LoadFromFile(Path.ChangeExtension(wavFile, ".md"), // out string fmt, out CBOW.DIMS); Console.Write($"\r\nSynthesizing: {Path.ChangeExtension(wavFile, ".g.wav")}...\r\n"); Wav.Write( Path.ChangeExtension(wavFile, ".g.wav"), Wav.Synthesize(Model.GetBuffer()) ); StartWinUI( () => { return(Model); }, Path.ChangeExtension(wavFile, ".g.wav") ); WinMM.PlaySound(Path.ChangeExtension(wavFile, ".g.wav"), IntPtr.Zero, WinMM.PLAYSOUNDFLAGS.SND_ASYNC | WinMM.PLAYSOUNDFLAGS.SND_FILENAME | WinMM.PLAYSOUNDFLAGS.SND_NODEFAULT | WinMM.PLAYSOUNDFLAGS.SND_NOWAIT | WinMM.PLAYSOUNDFLAGS.SND_PURGE); return(false); }
public void ReadTest() { Song song = Wav.Read(@"Files/1.wav"); Assert.AreEqual(1, song.Data.NumChannels); Assert.AreEqual(100, song.Data.SampleRate); Assert.AreEqual(16, song.Data.BitsPerSample); Assert.AreEqual(100, song.Data.NumSamples); }
public static void TrueLoadSound(string key) { if (!SoundList.ContainsKey(key) && LoadSoundList.ContainsKey(key)) { Wav wav = new Wav(); wav.load(LoadPath + LoadSoundList[key]); SoundList.Add(key, wav); } }
public void PlayAudio() { WavBytes = System.Convert.FromBase64String(Vocabdata.AudioString[j]); Wav wav = new Wav(WavBytes); AudioClip audioClip = AudioClip.Create("testSound", wav.SampleCount, 1, wav.Frequency, false, false); audioClip.SetData(wav.LeftChannel, 0); audio.clip = audioClip; audio.Play(); }
public void Send(TransportConnection connection, int resource, ref Wav wav) { using var writer = new DataBufferWriter(0); writer.WriteInt((int)EAudioSendType.SendReplyResourceData); writer.WriteInt(resource); writer.WriteValue((double)wav.getLength()); foreach (var(_, feature) in Features) { feature.Driver.Send(default, connection, writer.Span);
static bool PlayFrequency(App app, string cliScript, Func <bool> IsTerminated) { string title = cliScript; if (string.IsNullOrWhiteSpace(cliScript)) { return(false); } var inWavFile = Path.ChangeExtension( Path.Combine(app.CurrentDirectory, cliScript), ".md"); var inWav = File.Exists(inWavFile) ? Wav.Parse(inWavFile) : Wav.Parse(cliScript, "MIDI"); var wavOutFile = File.Exists(inWavFile) ? Path.GetFullPath(Path.ChangeExtension(inWavFile, ".g.wav")) : Path.GetFullPath(Path.ChangeExtension("cli.wav", ".g.wav")); Console.Write($"\r\nSynthesizing...\r\n\r\n"); var dataOut = Wav.Synthesize(inWav); Wav.Write(wavOutFile, dataOut); var dataIn = Wav.Read(wavOutFile) .Select(s => s.Left).ToArray(); using (TextWriter writer = new StreamWriter(Path.ChangeExtension(inWavFile, ".g.md"))) { writer.WriteLine("MIDI"); foreach (var fft in Complex.ShortTimeFourierTransform( dataIn, 1024 * 4, Shapes.Hann)) { var span = System.Audio.Process.Translate( fft, Stereo.Hz); Print.Dump(writer, span, 1024 * 4, Stereo.Hz); } } Console.Write($"\r\nReady!\r\n\r\n"); Microsoft.Win32.WinMM.PlaySound(wavOutFile, IntPtr.Zero, Microsoft.Win32.WinMM.PLAYSOUNDFLAGS.SND_ASYNC | Microsoft.Win32.WinMM.PLAYSOUNDFLAGS.SND_FILENAME | Microsoft.Win32.WinMM.PLAYSOUNDFLAGS.SND_NODEFAULT | Microsoft.Win32.WinMM.PLAYSOUNDFLAGS.SND_NOWAIT | // Microsoft.Win32.WinMM.PLAYSOUNDFLAGS.SND_LOOP | Microsoft.Win32.WinMM.PLAYSOUNDFLAGS.SND_PURGE); return(false); }
static void Main(string[] args) { string[] filePaths = Directory.GetFiles("media"); ArrayList PIList = new ArrayList(); foreach (string path in filePaths) { string type = path.Substring(path.Length - 4, 4); string name = path.Substring(6, path.Length - 10); PIList.Add(new PlayerItem(path, name, type)); } for (int i = 0; i < PIList.Count; i++) { Console.WriteLine($"{i + 1} - {((PlayerItem)PIList[i]).Name}{((PlayerItem)PIList[i]).Type}"); } Console.WriteLine(); Console.WriteLine("Выберите музыку по индексу"); byte index = Convert.ToByte(Console.ReadLine()); PlayerItem chosen = ((PlayerItem)PIList[index - 1]); Mp3 mp3 = new Mp3(chosen); Wav wav = new Wav(chosen); Mkv mkv = new Mkv(chosen); switch (chosen.Type) { case ".mkv": mkv.Play(); mkv.Pause(); mkv.Stop(); break; case ".mp3": mp3.Play(); mp3.Pause(); mp3.Stop(); break; case ".wav": wav.Play(); wav.Pause(); wav.Stop(); wav.Record(); break; default: Console.WriteLine("расширение не поддерживается"); break; } Console.ReadKey(); }
public override T Load <T>(string space, string filename, LangFlag?flag = null) { LoadBase(Path.Combine("res", "audio", space, filename), flag ?? Rosetta.Locale); ExtensionName = "wav"; var wav = new Wav(LoadFile <byte[]>()); var clip = AudioClip.Create($"{filename}_{(flag ?? Rosetta.Locale).ToString().ToLower()}", wav.SampleCount, 1, wav.Frequency, false); clip.SetData(wav.LeftChannel, 0); return((T)(object)clip); }
public static void LoadSound(string key, string path, double defaultVolume = 1) { #if DEBUG GhostLoadSound(key, path, defaultVolume); #else Wav wav = new Wav(); wav.load(LoadPath + path); SoundList.Add(key, wav); DefaultSoundVolume.Add(key, defaultVolume); #endif }
public static AudioClip GetAudio(string path) { // Load MP3 data and convert to WAV var mp3Stream = new MemoryStream(File.ReadAllBytes(path)); var mp3Audio = new Mp3FileReader(mp3Stream); var wav = new Wav(AudioMemStream(mp3Audio).ToArray()); var audioClip = AudioClip.Create(Path.GetFileName(path), wav.SampleCount, 1, wav.Frequency, false); audioClip.SetData(wav.LeftChannel, 0); return(audioClip); }
public void SaveIntegrationTest(string filePath) { Song origSong = Wav.Read(filePath); Wav.Save("temp.wav", origSong); Song newSong = Wav.Read("temp.wav"); Assert.IsTrue(origSong.Equals(newSong)); File.Delete("temp.wav"); }
public void WritesFile() { var tones = new List <Tone> { new Tone { Frequency = 220.0 } }; Wav.Create(tones, FileName); Assert.That(File.Exists(FileName), Is.True); }
static bool PlayFrequency(App app, string cliScript, Func <bool> IsTerminated) { string title = cliScript; if (string.IsNullOrWhiteSpace(cliScript)) { return(false); } // app.StartWin32Window<Mic32>( // onDrawMicrophoneFastFourierTransform, () => app.Mic, "Fast Fourier Transform (Mic)", // Color.Black, // app.onKeyDown); var inFile = @"D:\data\play.md"; Chord[] inWav = Wav.Parse(inFile); var wavOutFile = Path.GetFullPath(Path.ChangeExtension(inFile, ".g.wav")); Console.Write($"\r\nSynthesizing...\r\n\r\n"); var data = Wav.Synthesize(inWav); foreach (var it in data) { var fft = Complex.ShortTimeFourierTransform( it, 1024, Shapes.Square); Print.Dump(fft, Wav._hz); // Print.Dump(fft); } Wav.Write(wavOutFile, data); Console.Write($"\r\nReady!\r\n\r\n"); Microsoft.Win32.WinMM.PlaySound(wavOutFile, IntPtr.Zero, Microsoft.Win32.WinMM.PLAYSOUNDFLAGS.SND_ASYNC | Microsoft.Win32.WinMM.PLAYSOUNDFLAGS.SND_FILENAME | Microsoft.Win32.WinMM.PLAYSOUNDFLAGS.SND_NODEFAULT | Microsoft.Win32.WinMM.PLAYSOUNDFLAGS.SND_NOWAIT | Microsoft.Win32.WinMM.PLAYSOUNDFLAGS.SND_LOOP | Microsoft.Win32.WinMM.PLAYSOUNDFLAGS.SND_PURGE); return(false); }
void PlayAudio(DetectIntentResponse response) { Debug.Log("Playing the audio"); byte[] bytes = response.OutputAudio.ToByteArray(); var wav = new Wav(bytes); var audioClip = AudioClip.Create("Sound", wav.SampleCount, 1, wav.Frequency, false); audioClip.SetData(wav.LeftChannel, 0); audioOut.clip = audioClip; audioOut.Play(); }
static bool Play( string cliScript, Func <bool> IsTerminated) { if (cliScript.StartsWith("--play")) { cliScript = cliScript.Remove(0, "--play".Length).Trim(); } else if (cliScript.StartsWith("play")) { cliScript = cliScript.Remove(0, "play".Length).Trim(); } else { throw new ArgumentException(); } string md = cliScript; if (!File.Exists(md) && string.IsNullOrWhiteSpace(Path.GetExtension(md))) { if (File.Exists(Path.ChangeExtension(md, ".md"))) { md = Path.ChangeExtension(md, ".md"); } } var Model = File.Exists(md) ? System.Ai.Model.LoadFromFile(md, System.Ai.Cli.SIZE, out string fmt, out int dims) : BuildFromFragment(md); Console.Write($"Synthesizing: {Path.ChangeExtension(md, ".g.wav")}...\r\n"); Wav.Write(Path.ChangeExtension(md, ".g.wav"), Wav.Synthesize(Model.GetBuffer())); StartWinUI(() => { return(Model); }, Path.ChangeExtension(md, ".g.wav")); WinMM.PlaySound(Path.ChangeExtension(md, ".g.wav"), IntPtr.Zero, WinMM.PLAYSOUNDFLAGS.SND_ASYNC | WinMM.PLAYSOUNDFLAGS.SND_FILENAME | WinMM.PLAYSOUNDFLAGS.SND_NODEFAULT | WinMM.PLAYSOUNDFLAGS.SND_NOWAIT | WinMM.PLAYSOUNDFLAGS.SND_PURGE); return(false); }
public override SoundInput TryOpen(IBinaryStream file) { var header = file.ReadHeader(0x12); if (!header.AsciiEqual("\0\0\0\0")) { return(null); } int riff_length = header.ToInt32(4); if (file.Length != riff_length + 8) { return(null); } if (!header.AsciiEqual(8, "\0\0\0\0\0\0\0\0")) { return(null); } int header_length = header.ToUInt16(0x10); if (header_length < 0x10 || header_length > riff_length) { return(null); } header = file.ReadHeader(0x18 + header_length); if (!header.AsciiEqual(0x14 + header_length, "data")) { return(null); } var header_bytes = new byte[0x10] { (byte)'R', (byte)'I', (byte)'F', (byte)'F', header[4], header[5], header[6], header[7], (byte)'W', (byte)'A', (byte)'V', (byte)'E', (byte)'f', (byte)'m', (byte)'t', (byte)' ' }; Stream riff = new StreamRegion(file.AsStream, 0x10); riff = new PrefixStream(header_bytes, riff); var wav = new BinaryStream(riff, file.Name); try { return(Wav.TryOpen(wav)); } catch { wav.Dispose(); throw; } }
public void WritesFileWithDifferentFrequencies() { var tones = new List <Tone> { new Tone { Frequency = 680.0 } }; Wav.Create(tones, FileName); var stream = File.Open(FileName, FileMode.Open, FileAccess.Read, FileShare.Delete); var player = new SoundPlayer(stream); Assert.DoesNotThrow(() => player.Play()); }
void PlayMusic() { try { musicStream = Mpq.fs.OpenFile(@"data\global\music\introedit.wav"); AudioClip clip = Wav.Load("intro music", true, musicStream); var musicObject = new GameObject(); var audioSource = musicObject.AddComponent <AudioSource>(); audioSource.clip = clip; audioSource.loop = true; audioSource.Play(); } catch (FileNotFoundException) { } }
void PlayMusic() { MpqFile file = Mpq.fs.FindFile(@"data\global\music\introedit.wav"); if (file == null) { return; } musicStream = file.Open(); AudioClip clip = Wav.Load("intro music", true, musicStream); var musicObject = new GameObject(); var audioSource = musicObject.AddComponent <AudioSource>(); audioSource.clip = clip; audioSource.loop = true; audioSource.Play(); }
public void DFTTest(string path, double[] output) { Song song = Wav.Read(path); DecomposedSongSegment dss = song.DFT(); foreach (var(amplitude, index) in dss.Audio.Select((x, i) => (x, i))) { if (index > output.Length - 1) { Assert.AreEqual(0, amplitude, 5e-4); } else { Assert.AreEqual(output[index], amplitude, 5e-4); } } }
private void PlayMusic() { try { _musicStream = Mpq.fs.OpenFile(@"data\global\music\introedit.wav"); var clip = Wav.Load("intro music", true, _musicStream); var musicObject = new GameObject("intro music"); musicObject.transform.parent = logoPlaceholder; var audioSource = musicObject.AddComponent <AudioSource>(); audioSource.clip = clip; audioSource.loop = true; audioSource.Play(); } catch (FileNotFoundException) { } }
/// <summary> /// 将mp3格式的字节数组转换为audioclip /// </summary> /// <param name="data"></param> /// <returns></returns> public static AudioClip FromMp3Data(byte[] data) { // Load the data into a stream MemoryStream mp3stream = new MemoryStream(data); // Convert the data in the stream to WAV format Mp3FileReader mp3audio = new Mp3FileReader(mp3stream); WaveStream waveStream = WaveFormatConversionStream.CreatePcmStream(mp3audio); // Convert to WAV data Wav wav = new Wav(AudioMemStream(waveStream).ToArray()); AudioClip audioClip = AudioClip.Create("testSound", wav.SampleCount, 1, wav.Frequency, false); audioClip.SetData(wav.LeftChannel, 0); // Return the clip return(audioClip); }
public unsafe Effect LoadEffect(SfxData sfx_data) { var wav = new Wav(); fixed(byte *p = sfx_data.Data) { var ptr = (IntPtr)p; wav.loadMem(ptr, (uint)sfx_data.Data.Length, aCopy: true); } var effect = new Effect(wav) { Id = sfx_data.Id }; return(effect); }
public void WritesFileWithVaryingvolumes() { var tones = new List <Tone> { new Tone { Frequency = 680.0, Duration = 1, Volume = 0.5 }, new Tone { Frequency = 680, Duration = 1, Volume = 1 } }; Wav.Create(tones, FileName); var stream = File.Open(FileName, FileMode.Open, FileAccess.Read, FileShare.Delete); var player = new SoundPlayer(stream); Assert.DoesNotThrow(() => player.Play()); }
public void WithoutOctaves() { var tones = new List <Tone> { new Tone { Frequency = 680.0, Duration = 1, Volume = 0.5 }, new Tone { Frequency = 230, Duration = 1, Volume = 1 }, new Tone { Frequency = 250, Duration = 0.1, Volume = 1 }, new Tone { Frequency = 290, Duration = 0.1, Volume = 1 }, new Tone { Frequency = 400, Duration = 0.1, Volume = 1 }, new Tone { Frequency = 500, Duration = 0.1, Volume = 1 }, new Tone { Frequency = 600, Duration = 0.1, Volume = 1 }, new Tone { Frequency = 700, Duration = 0.1, Volume = 1 }, new Tone { Frequency = 900, Duration = 0.25, Volume = 1 }, new Tone { Frequency = 1200, Duration = 0.25, Volume = 1 }, }; Wav.Create(tones, FileName, synth: false); var stream = File.Open(FileName, FileMode.Open, FileAccess.Read, FileShare.Delete); var player = new SoundPlayer(stream); Assert.DoesNotThrow(() => player.Play()); }
private async void OnWave(object sender, RoutedEventArgs e) { var helpher = new AudioHelper(); var wave = await helpher.GetWaveStorageFile(sender, e); Sequensor.Controller.SetAudioTrack(wave); Wav wavFile = new Wav(wave); var imgFile = new PlottingGraphImg(wavFile, 400, 100); var image = await imgFile.GetGraphicFile(); var sequence = Sequensor.Controller.Sequences[0]; var item = new Sequence.SequenceBaseObject(); item.Template.Content = image; sequence.Add(item); }