/// <summary> /// Resamples data from multiple channels to single one. /// </summary> /// <param name="audio">audio with</param> public static short[] ConvertToMono(IAudioFormat audio) { if (audio == null) { throw new ArgumentNullException("Argument 'audio' is null."); } if (audio.Data == null) { throw new ArgumentNullException("Argument 'audio.Data' is null"); } switch (audio.Channels) { case 1: return(audio.Data); case 2: short[] mono = new short[audio.NumOfDataSamples / 2]; for (int i = 0; i < audio.NumOfDataSamples; i += 2) //4 bytes per loop are processed (2 left + 2 right samples) { mono[i / 2] = Arithmetics.Average(audio.Data[i], audio.Data[i + 1]); } return(mono); default: throw new NotImplementedException($"Convert from {audio.Channels} channels to mono is not supported."); } }
public static void BuildParseCompareAudio(IAudioFormat audio, IAudioWriter writer, IAudioReader reader) { byte[] builtFile = writer.GetFile(audio); IAudioFormat parsedAudio = reader.ReadFormat(builtFile); Assert.Equal(audio, parsedAudio, new AudioFormatComparer()); }
public void CreatingAudioDataWithInterface() { IAudioFormat adpcm = GenerateAudio.GenerateAdpcmSineWave(200, 2, 48000); var audio = new AudioData(adpcm); Assert.Same(adpcm, audio.GetFormat <GcAdpcmFormat>()); }
public void AdpcmSampleCountIsCorrectAfterLooping(int sampleCount, bool looping, int loopStart, int loopEnd) { IAudioFormat adpcm = GenerateAudio.GenerateAdpcmEmpty(sampleCount, 1, 48000) .WithLoop(looping, loopStart, loopEnd); Assert.Equal(sampleCount, adpcm.SampleCount); }
private void SetTranscodeParameters() { IVideoFormat videoFormat = Preview.VideoFormat; IAudioFormat audioFormat = Preview.AudioFormat; mediaComposer.addSourceFile(new File(Preview.File)); IMediaFile mediaFile = mediaComposer.getSourceFiles()[0]; var sourceFiles = mediaComposer.getSourceFiles(); if (sourceFiles.Count != 0) { double mul = Preview.Duration / 100; long trimStartTime = (long)(Preview.SegmentStart * mul); long trimStopTime = (long)(Preview.SegmentEnd * mul); var sourceFile = sourceFiles[0]; sourceFile.addSegment(Segment.fromMicroSeconds(trimStartTime, trimStopTime)); } ComboBoxItem framerateItem = (ComboBoxItem)FramerateSelect.SelectedItem; String videoProfile = ((ComboBoxItem)ProfileSelect.SelectedItem).Content.ToString(); var match = Regex.Match(videoProfile, @"\(([0-9]+)x([0-9]+)\)"); uint width = uint.Parse(match.Groups[1].Value); uint height = uint.Parse(match.Groups[2].Value); uint framerate = uint.Parse(framerateItem.Content.ToString()); uint bitrate = (uint)(BitrateSelect.Value * 1000); if (videoFormat != null) { var vFormat = new WinRtVideoFormat(videoMimeType, width, height) { bitrate = bitrate }; vFormat.frameRate.Numerator = framerate; vFormat.frameRate.Denominator = 1; mediaComposer.setTargetVideoFormat(vFormat); } if (audioFormat != null) { var aFormat = new WinRtAudioFormat(audioMimeType) { bitrate = audioFormat.bitrate, bitsPerSample = audioFormat.bitsPerSample, channelCount = audioFormat.channelCount, sampleRate = audioFormat.sampleRate }; mediaComposer.setTargetAudioFormat(aFormat); } }
public void AdpcmLoopIsCorrectAfterUnalignment(int sampleCount, bool looping, int loopStart, int loopEnd, int alignment) { IAudioFormat adpcm = GenerateAudio.GenerateAdpcmEmpty(sampleCount, 1, 48000) .WithAlignment(alignment) .WithLoop(looping, loopStart, loopEnd) .WithAlignment(0); Assert.Equal(loopStart, adpcm.LoopStart); Assert.Equal(loopEnd, adpcm.LoopEnd); }
public void LoopsProperlyAfterDecoding(int sampleCount, bool looping, int loopStart, int loopEnd) { GcAdpcmFormat adpcm = GenerateAudio.GenerateAdpcmEmpty(sampleCount, 1, 48000) .WithLoop(looping, loopStart, loopEnd); IAudioFormat pcm = adpcm.ToPcm16(); Assert.Equal(looping, pcm.Looping); Assert.Equal(loopStart, pcm.LoopStart); Assert.Equal(loopEnd, pcm.LoopEnd); }
public void AdpcmLoopIsCorrectAfterAlignment(int sampleCount, bool looping, int loopStart, int loopEnd, int alignment) { IAudioFormat adpcm = GenerateAudio.GenerateAdpcmEmpty(sampleCount, 1, 48000) .WithAlignment(alignment) .WithLoop(looping, loopStart, loopEnd); int extraSamples = Helpers.GetNextMultiple(loopStart, alignment) - loopStart; Assert.Equal(loopStart + extraSamples, adpcm.LoopStart); Assert.Equal(loopEnd + extraSamples, adpcm.LoopEnd); }
private void SetTranscodeParameters() { IVideoFormat videoFormat = Preview.VideoFormat; IAudioFormat audioFormat = Preview.AudioFormat; mediaComposer.addSourceFile(new File(Preview.File)); ComboBoxItem framerateItem = (ComboBoxItem)FramerateSelect.SelectedItem; String videoProfile = ((ComboBoxItem)ProfileSelect.SelectedItem).Content.ToString(); var match = Regex.Match(videoProfile, @"\(([0-9]+)x([0-9]+)\)"); uint width = uint.Parse(match.Groups[1].Value); uint height = uint.Parse(match.Groups[2].Value); uint framerate = uint.Parse(framerateItem.Content.ToString()); uint bitrate = (uint)(BitrateSelect.Value * 1000); if (videoFormat != null) { var vFormat = new WinRtVideoFormat(videoMimeType, width, height) { bitrate = bitrate }; vFormat.frameRate.Numerator = framerate; vFormat.frameRate.Denominator = 1; mediaComposer.setTargetVideoFormat(vFormat); } if (audioFormat != null) { var aFormat = new WinRtAudioFormat(audioMimeType) { bitrate = audioFormat.bitrate, bitsPerSample = audioFormat.bitsPerSample, channelCount = audioFormat.channelCount, sampleRate = audioFormat.sampleRate }; mediaComposer.setTargetAudioFormat(aFormat); } for (int i = 0; i < EffectsMenu.Items.Count; ++i) { ToggleMenuFlyoutItem item = (ToggleMenuFlyoutItem)EffectsMenu.Items[i]; if (item.IsChecked) { mediaComposer.addVideoEffect(SamplesUtils.GetVideoEffectByName(item.Text)); } } }
public void Play([CanBeNull] string fileName, [NotNull] IAudioFormat format) { if (fileName == null) { return; } fileName = Path.GetFullPath(fileName); PreloadSfx(fileName, format); var key = Environment.OSVersion.Platform == PlatformID.Win32NT ? fileName.ToLowerInvariant() : fileName; var currentTime = _audioManager.MixerTime; var free = GetFreeStream(key); if (free.OffsetStream != null) { free.OffsetStream.StartTime = currentTime; free.OffsetStream.CurrentTime = currentTime; _playingStates[free.Index] = true; _audioManager.AddInputStream(free.OffsetStream, Volume); return; } var(data, waveFormat) = _preloaded[key]; var source = new RawSourceWaveStream(data, 0, data.Length, waveFormat); // Offset requires 16-bit integer input. WaveStream toOffset; if (AudioHelper.NeedsFormatConversionFrom(waveFormat, RequiredFormat)) { toOffset = new ResamplerDmoStream(source, RequiredFormat); } else { toOffset = source; } var offset = new WaveOffsetStream(toOffset, currentTime, TimeSpan.Zero, toOffset.TotalTime); _audioManager.AddInputStream(offset, Volume); lock (_queueLock) { _playingWaveStreams.Add((key, offset, toOffset, source)); } _playingStates.Add(true); }
public void PlayLooped([CanBeNull] string fileName, [NotNull] IAudioFormat format, [NotNull] object state) { if (fileName == null) { return; } if (state == null) { throw new ArgumentNullException(nameof(state)); } if (_loopedStreams.ContainsKey(state)) { return; } fileName = Path.GetFullPath(fileName); PreloadSfx(fileName, format); var key = Environment.OSVersion.Platform == PlatformID.Win32NT ? fileName.ToLowerInvariant() : fileName; var currentTime = _audioManager.MixerTime; var(data, waveFormat) = _preloaded[key]; var source = new RawSourceWaveStream(data, 0, data.Length, waveFormat); var looped = new LoopedWaveStream(source, LoopedWaveStream.DefaultMaxLoops); // Offset requires 16-bit integer input. WaveStream toOffset; if (AudioHelper.NeedsFormatConversionFrom(waveFormat, RequiredFormat)) { toOffset = new ResamplerDmoStream(looped, RequiredFormat); } else { toOffset = looped; } var offset = new WaveOffsetStream(toOffset, currentTime, TimeSpan.Zero, toOffset.TotalTime); _audioManager.AddInputStream(offset, Volume); lock (_queueLock) { _loopedStreams[state] = (offset, toOffset, looped, source); } }
protected override void SetupWriter(AudioData audio) { var parameters = new GcAdpcmParameters { Progress = Configuration.Progress }; if (Codec == NwCodec.GcAdpcm) { Adpcm = audio.GetFormat <GcAdpcmFormat>(parameters); AudioFormat = Adpcm; } else if (Codec == NwCodec.Pcm16Bit) { Pcm16 = audio.GetFormat <Pcm16Format>(parameters); AudioFormat = Pcm16; } }
public void PreloadSfx([CanBeNull] string fileName, [NotNull] IAudioFormat format) { if (fileName == null) { return; } fileName = Path.GetFullPath(fileName); if (!File.Exists(fileName)) { throw new FileNotFoundException("Audio file is not found.", fileName); } var key = Environment.OSVersion.Platform == PlatformID.Win32NT ? fileName.ToLowerInvariant() : fileName; if (_preloaded.ContainsKey(key)) { return; } using (var waveStream = format.Read(fileName)) { byte[] data; var buf = new byte[1024]; using (var memoryStream = new MemoryStream()) { var read = waveStream.Read(buf, 0, buf.Length); while (read > 0) { memoryStream.Write(buf, 0, read); if (read < buf.Length) { break; } read = waveStream.Read(buf, 0, buf.Length); } data = memoryStream.ToArray(); } var waveFormat = waveStream.WaveFormat; _preloaded.Add(key, (data, waveFormat)); } }
protected void SetTranscodeParameters() { IVideoFormat videoFormat = Preview.VideoFormat; IAudioFormat audioFormat = Preview.AudioFormat; mediaComposer.addSourceFile(new File(Preview.File)); mediaComposer.addSourceFile(new File(Preview2.File)); ComboBoxItem framerateItem = (ComboBoxItem)FramerateSelect.SelectedItem; String videoProfile = ((ComboBoxItem)ProfileSelect.SelectedItem).Content.ToString(); var match = Regex.Match(videoProfile, @"\(([0-9]+)x([0-9]+)\)"); uint width = uint.Parse(match.Groups[1].Value); uint height = uint.Parse(match.Groups[2].Value); uint framerate = uint.Parse(framerateItem.Content.ToString()); uint bitrate = (uint)(BitrateSelect.Value * 1000); if (videoFormat != null) { var vFormat = new WinRtVideoFormat(videoMimeType, width, height) { bitrate = bitrate }; vFormat.frameRate.Numerator = framerate; vFormat.frameRate.Denominator = 1; mediaComposer.setTargetVideoFormat(vFormat); } if (audioFormat != null) { var aFormat = new WinRtAudioFormat(audioMimeType) { bitrate = audioFormat.bitrate, bitsPerSample = audioFormat.bitsPerSample, channelCount = audioFormat.channelCount, sampleRate = audioFormat.sampleRate }; mediaComposer.setTargetAudioFormat(aFormat); } }
public bool TryAdd(IAudioFormat format, out IAudioFormat result) { result = null; TFormat castFormat = format as TFormat; if (castFormat == null) { return(false); } try { result = Add(castFormat); } catch { return(false); } return(true); }
/// <summary> /// Creates fingerprint of given audio and returns it together with number of valid notes. (needed to compute recognition accuracy). <br></br> /// </summary> /// <param name="audio">Audio whos fingerprint we want.</param> /// <returns>Tuple where Item1 is fingerprint, Item2 is total number of notes. <br></br>fingerprint: [hash; (absolute anchor times)]<br></br>Note: Address is the hash.</returns> public Tuple <Dictionary <uint, List <uint> >, int> GetAudioFingerprint(IAudioFormat audio) { short[] monoData = AudioProcessor.ConvertToMono(audio); double[] data = Array.ConvertAll(monoData, item => (double)item); //compute downsample coeficient for resampling to Parameters.TargetSamplingRate int downsampleCoef = (int)audio.SampleRate / Parameters.TargetSamplingRate; double[] downsampledData = AudioProcessor.DownSample(data, downsampleCoef, audio.SampleRate); int bufferSize = Parameters.WindowSize / downsampleCoef; //default: 4096/4 = 1024 var timeFrequencyPoints = CreateTimeFrequencyPoints(bufferSize, downsampledData); //[hash;(AbsAnchorTimes)] Dictionary <uint, List <uint> > fingerprint = CreateRecordAddresses(timeFrequencyPoints); return(new Tuple <Dictionary <uint, List <uint> >, int> (fingerprint, timeFrequencyPoints.Count)); }
protected override void SetupWriter(AudioData audio) { var parameters = new CodecParameters { Progress = Configuration.Progress }; switch (Codec) { case WaveCodec.Pcm16Bit: Pcm16 = audio.GetFormat <Pcm16Format>(parameters); AudioFormat = Pcm16; break; case WaveCodec.Pcm8Bit: Pcm8 = audio.GetFormat <Pcm8Format>(parameters); AudioFormat = Pcm8; break; } }
private void EncodeFiles(AudioFileStream file, Options options) { if (file.Channels != null) { IAudioFormat format = file.Audio.GetAllFormats().First(); file.Audio = new AudioData(format.GetChannels(file.Channels.ToArray())); } Audio = file.Audio; //Nothing to combine if (options.NoLoop) { Audio.SetLoop(false); } if (options.Loop) { Audio.SetLoop(options.Loop, options.LoopStart, options.LoopEnd); } }
private void EncodeFiles(JobFiles files, Options options) { foreach (AudioFile file in files.InFiles.Where(x => x.Channels != null)) { IAudioFormat format = file.Audio.GetAllFormats().First(); file.Audio = new AudioData(format.GetChannels(file.Channels.ToArray())); } AudioData[] toMerge = files.InFiles.Select(x => x.Audio).ToArray(); Audio = AudioData.Combine(toMerge); if (options.NoLoop) { Audio.SetLoop(false); } if (options.Loop) { Audio.SetLoop(options.Loop, options.LoopStart, options.LoopEnd); } }
public static AudioData Combine(params AudioData[] audio) { if (audio == null || audio.Length <= 0 || audio.Any(x => x == null)) { throw new ArgumentException("Audio cannot be null, empty, or have any null elements"); } List <Type> commonTypes = audio .Select(x => x.ListAvailableFormats()) .Aggregate((x, y) => x.Intersect(y)) .ToList(); Type formatToUse; if (commonTypes.Count == 0 || commonTypes.Count == 1 && commonTypes.Contains(typeof(Pcm16Format))) { formatToUse = typeof(Pcm16Format); foreach (AudioData a in audio) { a.CreatePcm16(); } } else { formatToUse = commonTypes.First(x => x != typeof(Pcm16Format)); } IAudioFormat combined = audio[0].Formats[formatToUse]; foreach (IAudioFormat format in audio.Select(x => x.Formats[formatToUse]).Skip(1)) { if (combined.TryAdd(format, out combined) == false) { throw new ArgumentException("Audio streams cannot be added together"); } } return(new AudioData(combined)); }
private void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e) { if (e.Button == MouseButtons.Left) { if (e.Node.Nodes.Count == 0) { var Size = (decimal)Rom.FileDict[$"/{e.Node.FullPath}"].DataLength; if (Size == 0) { label1.Text = $"Size: 0 bytes (empty file)"; } else if (Size > 1024 && Size < 1048576) { Size /= 1024; label1.Text = $"Size: {Size:0.00}KB"; } else if (Size > 1048576 && Size < 1073741824) { Size /= 1048576; label1.Text = $"Size: {Size:0.00}MB"; } else if (Size > 1073741824) { Size /= 1073741824; label1.Text = $"Size: {Size:0.00}GB"; } else { label1.Text = $"Size: {Size} bytes"; } var Name = Rom.FileDict[$"/{e.Node.FullPath}"].Name; label4.Text = Name; var Ext = Name.Split('.')[Name.Count(c => c == '.')].ToUpper(); if (CheckExtension(Ext, "bfstm")) { try { label6.Visible = true; label7.Visible = true; button2.Visible = true; button3.Visible = true; SoundFile = new VGAudio.Containers.NintendoWare.BCFstmReader().Read ( Rom.OpenFile(Rom.FileDict[$"/{e.Node.FullPath}"]) ) .GetAllFormats() .ToArray()[0]; label6.Text = $"Length: {new TimeSpan((SoundFile.SampleCount / SoundFile.SampleRate) * 10000000).ToString("mm':'ss")}"; label7.Text = $"Sample rate: {(decimal)(SoundFile.SampleRate / 1000)}KHz"; } catch (Exception) { } } else if (CheckExtension(Ext, "wav")) { try { label6.Visible = true; label7.Visible = true; button2.Visible = true; button3.Visible = true; SoundFile = new VGAudio.Containers.Wave.WaveReader().Read ( Rom.OpenFile(Rom.FileDict[$"/{e.Node.FullPath}"]) ) .GetAllFormats() .ToArray()[0]; label6.Text = $"Length: {new TimeSpan((SoundFile.SampleCount / SoundFile.SampleRate) * 10000000).ToString("mm':'ss")}"; label7.Text = $"Sample rate: {(decimal)(SoundFile.SampleRate / 1000)}KHz"; } catch (Exception) { } } else { label6.Visible = false; label7.Visible = false; button2.Visible = false; button3.Visible = false; } label5.Text = $"{Ext} file"; } else { if (e.Node.Nodes.Count == 1) { label5.Text = "1 file"; } else { label5.Text = $"{e.Node.Nodes.Count} files"; } label4.Text = e.Node.Name; label1.Text = "Folder"; } } else if (e.Button == MouseButtons.Right) { if (treeView1.SelectedNode.Nodes.Count == 0) { extractFileToolStripMenuItem.Text = "Extract file..."; contextMenuStrip1.Show(Cursor.Position); } else { extractFileToolStripMenuItem.Text = "Extract folder..."; contextMenuStrip1.Show(Cursor.Position); } } }
public AudioData(IAudioFormat audioFormat) { AddFormat(audioFormat); }
public Music CreateMusic([NotNull] string fileName, [NotNull] IAudioFormat format, float volume) { var audio = format.Read(fileName); return(CreateMusic(audio, volume, true)); }
/// <summary>Indicates whether the current object is equal to another object of the same type.</summary> /// <param name="other">An object to compare with this object.</param> /// <returns> /// <c>true</c> if the current object is equal to the other parameter; otherwise, <c>false</c>.</returns> public bool Equals(IAudioFormat <float> other) { return(other.BitDepth == 32 && other.Channels == Channels && other.SampleRate == SampleRate); }
public Sound LoadSound([NotNull] string fileName, [NotNull] IAudioFormat format) { return(LoadSound(fileName, format, true)); }
public Sound LoadSoundDirect([NotNull] string fileName, [NotNull] IAudioFormat format, bool manageResult) { using (var stream = format.Read(fileName)) { return(LoadSoundDirect(fileName, stream, manageResult)); } }
public Sound LoadSound([NotNull] string fileName, [NotNull] IAudioFormat format, bool manageResult) { if (!HasLoadedFile(fileName)) { return(LoadSoundDirect(fileName, format, manageResult)); } // Finds the first available (not in use) audio stream. Sound availableSound = null; Sound firstSound = null; foreach (var it in _loadedSounds) { if (it.FileName != fileName) { continue; } var source = it.Sound.Source; var state = source.State; if (firstSound == null) { firstSound = it.Sound; } switch (state) { case ALSourceState.Initial: case ALSourceState.Stopped: availableSound = it.Sound; break; case ALSourceState.Playing: case ALSourceState.Paused: break; default: throw new ArgumentOutOfRangeException(); } if (availableSound != null) { break; } } // If we are lucky... if (availableSound != null) { return(availableSound); } // Or we have to clone and create one... availableSound = firstSound; Debug.Assert(availableSound != null, nameof(availableSound) + " != null"); var newSound = LoadSoundDirect(fileName, availableSound.Data, availableSound.Format, false, manageResult); // ... and copies its params. newSound.Source.Volume = availableSound.Source.Volume; return(newSound); }
private bool UpdateMediaInfo(MediaFileInfo mediaFileInfo) { if (mediaFileInfo == null) { return(false); } string videoMimeType = "unknown"; string audioMimeType = "unknown"; videoFormat = mediaFileInfo.getVideoFormat(); audioFormat = mediaFileInfo.getAudioFormat(); try { videoMimeType = videoFormat.mimeType.asString(); } catch (Exception) { videoFormat = null; } try { audioMimeType = audioFormat.mimeType.asString(); } catch (Exception) { audioFormat = null; } if (videoFormat != null) { float frameRate = videoFormat.frameRate.Numerator; if (videoFormat.frameRate.Denominator != 0) { frameRate /= videoFormat.frameRate.Denominator; } VideoCodecText.Text = videoMimeType; ResolutionText.Text = String.Format("{0} x {1}", videoFormat.resolution.Width, videoFormat.resolution.Height); FramerateText.Text = String.Format("{0}", frameRate); BitrateVideoText.Text = String.Format("{0}", videoFormat.bitrate); } else { ResolutionText.Text = ""; FramerateText.Text = ""; BitrateVideoText.Text = ""; } if (audioFormat != null) { AudioCodecText.Text = audioMimeType; ChannelsText.Text = String.Format("{0}", audioFormat.channelCount); SamplerateText.Text = String.Format("{0}", audioFormat.sampleRate); BitspersampleText.Text = String.Format("{0}", audioFormat.bitsPerSample); BitrateAudioText.Text = String.Format("{0}", audioFormat.bitrate); } if (videoFormat == null && audioFormat == null) { return(false); } Info.Visibility = Visibility.Visible; return(true); }
private bool UpdateMediaInfo(MediaFileInfo mediaFileInfo) { if (mediaFileInfo == null) { return false; } string videoMimeType = "unknown"; string audioMimeType = "unknown"; videoFormat = mediaFileInfo.getVideoFormat(); audioFormat = mediaFileInfo.getAudioFormat(); try { videoMimeType = videoFormat.mimeType.asString(); } catch (Exception) { videoFormat = null; } try { audioMimeType = audioFormat.mimeType.asString(); } catch (Exception) { audioFormat = null; } if (videoFormat != null) { float frameRate = videoFormat.frameRate.Numerator; if (videoFormat.frameRate.Denominator != 0) { frameRate /= videoFormat.frameRate.Denominator; } VideoCodecText.Text = videoMimeType; ResolutionText.Text = String.Format("{0} x {1}", videoFormat.resolution.Width, videoFormat.resolution.Height); FramerateText.Text = String.Format("{0}", frameRate); BitrateVideoText.Text = String.Format("{0}", videoFormat.bitrate); } else { ResolutionText.Text = ""; FramerateText.Text = ""; BitrateVideoText.Text = ""; } if (audioFormat != null) { AudioCodecText.Text = audioMimeType; ChannelsText.Text = String.Format("{0}", audioFormat.channelCount); SamplerateText.Text = String.Format("{0}", audioFormat.sampleRate); BitspersampleText.Text = String.Format("{0}", audioFormat.bitsPerSample); BitrateAudioText.Text = String.Format("{0}", audioFormat.bitrate); } if (videoFormat == null && audioFormat == null) { return false; } Info.Visibility = Visibility.Visible; return true; }
public void WriteToStream(IAudioFormat audio, Stream stream, Configuration configuration = null) => WriteStream(new AudioData(audio), stream, configuration as TConfig);
public byte[] GetFile(IAudioFormat audio, Configuration configuration = null) => GetByteArray(new AudioData(audio), configuration as TConfig);