public void sendAudio() { discord._client.SetGame(currentSongText); Console.WriteLine("Setting game to " + currentSongText); var channelCount = discord._client.GetService<AudioService>().Config.Channels; // Get the number of AudioChannels our AudioService has been configured to use. var OutFormat = new WaveFormat(48000, 16, channelCount); // Create a new Output Format, using the spec that Discord will accept, and with the number of channels that our client supports. using (var MP3Reader = new Mp3FileReader(musicToPlay)) // Create a new Disposable MP3FileReader, to read audio from the filePath parameter using (var resampler = new MediaFoundationResampler(MP3Reader, OutFormat)) // Create a Disposable Resampler, which will convert the read MP3 data to PCM, using our Output Format { resampler.ResamplerQuality = 60; // Set the quality of the resampler to 60, the highest quality int blockSize = OutFormat.AverageBytesPerSecond/50; // Establish the size of our AudioBuffer byte[] buffer = new byte[blockSize]; int byteCount; while ((byteCount = resampler.Read(buffer, 0, blockSize)) > 0) // Read audio into our buffer, and keep a loop open while data is present { if (byteCount < blockSize) { // Incomplete Frame for (int i = byteCount; i < blockSize; i++) buffer[i] = 0; } buffer = adjustVolume(buffer, volume); try { discord._vClient.Send(buffer, 0, blockSize); // Send the buffer to Discord } catch (Exception ex1) { } } Console.WriteLine("Song done"); // discord._client.SetGame(""); discord._client.SetGame(new Game()); if (!playingFolders && !nextSongText.Equals("null") && !nextSongText.Equals(currentSongText)) { Console.WriteLine("meow"); currentSongText = nextSongText; nextSongText = "null"; //update(); sendAudio(); } else if (playingFolders) { if (folderFiles.Count > 0) { Random rdm = new Random(); int choice = rdm.Next(0, folderFiles.Count); musicToPlay = folderFiles.ElementAt(choice); currentSongText = System.IO.Path.GetFileName(musicToPlay); nextSongText = "null"; //update(); folderFiles.RemoveAt(choice); sendAudio(); } else { Console.WriteLine("Queue complete"); } } } }
public static void SendAudio(string filePath) { var channelCount = bot.GetService<AudioService>().Config.Channels; // Get the number of AudioChannels our AudioService has been configured to use. var OutFormat = new WaveFormat(48000, 16, channelCount); // Create a new Output Format, using the spec that Discord will accept, and with the number of channels that our client supports. using (var MP3Reader = new Mp3FileReader(filePath)) // Create a new Disposable MP3FileReader, to read audio from the filePath parameter using (var resampler = new MediaFoundationResampler(MP3Reader, OutFormat)) // Create a Disposable Resampler, which will convert the read MP3 data to PCM, using our Output Format { resampler.ResamplerQuality = 60; // Set the quality of the resampler to 60, the highest quality int blockSize = OutFormat.AverageBytesPerSecond / 50; // Establish the size of our AudioBuffer byte[] buffer = new byte[blockSize]; int byteCount; while ((byteCount = resampler.Read(buffer, 0, blockSize)) > 0) // Read audio into our buffer, and keep a loop open while data is present { if (byteCount < blockSize) { // Incomplete Frame for (int i = byteCount; i < blockSize; i++) buffer[i] = 0; } _vClient.Send(buffer, 0, blockSize); // Send the buffer to Discord } } _vClient.Disconnect(); }
private static void VoiceStuffs(DiscordVoiceClient vc, string file) { try { int ms = 60; int channels = 1; int sampleRate = 48000; int blockSize = 48 * 2 * channels * ms; //sample rate * 2 * channels * milliseconds byte[] buffer = new byte[blockSize]; var outFormat = new WaveFormat(sampleRate, 16, channels); vc.SetSpeaking(true); using (var mp3Reader = new MediaFoundationReader(file)) { using (var resampler = new MediaFoundationResampler(mp3Reader, outFormat) { ResamplerQuality = 60 }) { //resampler.ResamplerQuality = 60; int byteCount; while ((byteCount = resampler.Read(buffer, 0, blockSize)) > 0) { if (vc.Connected) { vc.SendVoice(buffer); } else break; } Console.ForegroundColor = ConsoleColor.Yellow; Console.WriteLine("Voice finished enqueuing"); Console.ForegroundColor = ConsoleColor.White; } } } catch (Exception ex) { owner.SendMessage("Exception during voice: `" + ex.Message + "`\n\n```" + ex.StackTrace + "\n```"); } }
private void TestRheaStream() { DiscordVoiceClient vc = client.GetVoiceClient(); try { int ms = voiceMsBuffer; int channels = 1; int sampleRate = 48000; int blockSize = 48 * 2 * channels * ms; //sample rate * 2 * channels * milliseconds byte[] buffer = new byte[blockSize]; var outFormat = new WaveFormat(sampleRate, 16, channels); vc.SetSpeaking(true); using (var mp3Reader = new MediaFoundationReader("http://radiosidewinder.out.airtime.pro:8000/radiosidewinder_a")) { using (var resampler = new MediaFoundationResampler(mp3Reader, outFormat) { ResamplerQuality = 60 }) { int byteCount; while ((byteCount = resampler.Read(buffer, 0, blockSize)) > 0) { if (vc.Connected) { vc.SendVoice(buffer); } else break; } Console.ForegroundColor = ConsoleColor.Yellow; Console.WriteLine("Voice finished enqueuing"); Console.ForegroundColor = ConsoleColor.White; resampler.Dispose(); mp3Reader.Close(); } } } catch(Exception ex) { owner.SendMessage("Exception during voice: `" + ex.Message + "`\n\n```" + ex.StackTrace + "\n```"); } }
/// <summary> /// Converts the audio using the specified wave format. /// </summary> /// <param name="waveFormat">The WaveFormat to use for the conversion.</param> void ConvertWav(WaveFormat waveFormat) { reader.Position = 0; #if WINDOWS //var mediaTypes = MediaFoundationEncoder.GetOutputMediaTypes(NAudio.MediaFoundation.AudioSubtypes.MFAudioFormat_PCM); using (var resampler = new MediaFoundationResampler(reader, waveFormat)) { using (var outStream = new MemoryStream()) { // Since we cannot determine ahead of time the number of bytes to be // read, read four seconds worth at a time. byte[] bytes = new byte[reader.WaveFormat.AverageBytesPerSecond * 4]; while (true) { int bytesRead = resampler.Read(bytes, 0, bytes.Length); if (bytesRead == 0) break; outStream.Write(bytes, 0, bytesRead); } data = new List<byte>(outStream.ToArray()); format = new AudioFormat(waveFormat); } } #else throw new NotImplementedException(); #endif }
public void ProcessPP(ppParser pp) { for (int i = 0; i < pp.Subfiles.Count; i++) { IWriteFile iw = pp.Subfiles[i]; if (!iw.Name.EndsWith(".wav")) continue; Stream str = Tools.GetReadStream(iw); if (!str.CanSeek || str.Length == 0) { str.Close(); continue; } using (str) using (WaveFileReader wv = new WaveFileReader(str)) { if (wv.WaveFormat.Channels > 1 || wv.WaveFormat.SampleRate > SampleRate) // || wv.WaveFormat.Encoding != WaveFormatEncoding.Adpcm { WaveFormat f = new WaveFormat(SampleRate, 16, 1); //new AdpcmWaveFormat(wv.WaveFormat.SampleRate, 1); using (MediaFoundationResampler resampledAudio = new MediaFoundationResampler(wv, f)) { resampledAudio.ResamplerQuality = 60; MemoryStream o = new MemoryStream(); using (WaveFileWriter wr = new WaveFileWriter(o, f)) { int count = 0; byte[] buffer = new byte[2048]; while ((count = resampledAudio.Read(buffer, 0, 2048)) > 0) { wr.Write(buffer, 0, count); } wr.Flush(); pp.Subfiles[i] = new MemSubfile(ToByteArray(o), iw.Name); } } } } if (ProgressUpdated != null) ProgressUpdated((int)Math.Floor((double)(100 * i) / pp.Subfiles.Count)); } }
private void SendVoice(string file, DiscordClient client, YouTubeVideo video) { DiscordVoiceClient vc = client.GetVoiceClient(); try { int ms = vc.VoiceConfig.FrameLengthMs; int channels = vc.VoiceConfig.Channels; int sampleRate = 48000; int blockSize = 48 * 2 * channels * ms; //sample rate * 2 * channels * milliseconds byte[] buffer = new byte[blockSize]; var outFormat = new WaveFormat(sampleRate, 16, channels); vc.SetSpeaking(true); if(video.AudioFormat == AudioFormat.Mp3) { using (var mp3Reader = new Mp3FileReader(video.Stream())) { using (var resampler = new MediaFoundationResampler(mp3Reader, outFormat) { ResamplerQuality = 60 }) { //resampler.ResamplerQuality = 60; int byteCount; while ((byteCount = resampler.Read(buffer, 0, blockSize)) > 0) { if (vc.Connected) { vc.SendVoice(buffer); } else break; } Console.ForegroundColor = ConsoleColor.Yellow; Console.WriteLine("Voice finished enqueuing"); Console.ForegroundColor = ConsoleColor.White; resampler.Dispose(); mp3Reader.Close(); } } } else if(video.AudioFormat == AudioFormat.Vorbis) { using (var vorbis = new NAudio.Vorbis.VorbisWaveReader(video.Stream())) { using (var resampler = new MediaFoundationResampler(vorbis, outFormat) { ResamplerQuality = 60 }) { int byteCount; while ((byteCount = resampler.Read(buffer, 0, blockSize)) > 0) { if (vc.Connected) { vc.SendVoice(buffer); } else break; } Console.ForegroundColor = ConsoleColor.Yellow; Console.WriteLine("Voice finished enqueuing"); Console.ForegroundColor = ConsoleColor.White; resampler.Dispose(); vorbis.Close(); } } } } catch (Exception ex) { try { MainEntry.owner.SendMessage("Exception during voice: `" + ex.Message + "`\n\n```" + ex.StackTrace + "\n```"); } catch { } } }