/// <summary> /// Gives you all available metadata blocks in the flac file. /// </summary> /// <param name="path"></param> /// <returns></returns> public static List<MetadataBlock> GetMetaData(string path) { using (FlacFile flac = new FlacFile(path)) { return flac.Metadata; } }
/// <summary> /// Gets the duration of the track in seconds. /// </summary> /// <param name="path"></param> /// <returns>The duration of the track in seconds or 0 if the duration is not known (sample count is missing from streaminfo metadata)</returns> public static int GetDuration(string path) { using (FlacFile file = new FlacFile(path)) { return file.StreamInfo.Duration; } }
/// <summary> /// Gives you the StreamInfo metadata /// </summary> /// <param name="path"></param> /// <returns>The StreamInfo metadata or null if no StreamInfo metadata is found.</returns> public static StreamInfo GetStreamInfo(string path) { using (FlacFile flac = new FlacFile(path)) { return flac.StreamInfo; } }
public void OpenAndCloseFlacFileWithFilePath() { using (FlacFile file = new FlacFile(@"Data\testfile1.flac")) { // Doing nothing } }
/// <summary> /// Gets the duration of the track in seconds. /// </summary> /// <param name="path"></param> /// <returns>The duration of the track in seconds or 0 if the duration is not known (sample count is missing from streaminfo metadata)</returns> public static int GetDuration(string path) { using (FlacFile file = new FlacFile(path)) { return(file.StreamInfo.Duration); } }
/// <summary> /// Gives you all available metadata blocks in the flac file. /// </summary> /// <param name="path"></param> /// <returns></returns> public static List <MetadataBlock> GetMetaData(string path) { using (FlacFile flac = new FlacFile(path)) { return(flac.Metadata); } }
/// <summary> /// Gives you the StreamInfo metadata /// </summary> /// <param name="path"></param> /// <returns>The StreamInfo metadata or null if no StreamInfo metadata is found.</returns> public static StreamInfo GetStreamInfo(string path) { using (FlacFile flac = new FlacFile(path)) { return(flac.StreamInfo); } }
/// <summary> /// Gives you the vorbis comment metadata (ID3V2 tags). /// </summary> /// <param name="path"></param> /// <returns>The vorbis comment metadata or null if none is available.</returns> public static VorbisComment GetVorbisComment(string path) { using (FlacFile flac = new FlacFile(path)) { return(flac.VorbisComment); } }
/// <summary> /// Gets the specific vorbis field name (example = ARTIST) if it is available. /// </summary> /// <param name="path"></param> /// <param name="fieldName"></param> /// <returns>The value of the field or an empty string if the field is not available.</returns> public static VorbisCommentValues GetVorbisField(string path, string fieldName) { using (FlacFile flac = new FlacFile(path)) { if (flac.VorbisComment != null && flac.VorbisComment.ContainsField(fieldName)) { return(flac.VorbisComment[fieldName]); } return(new VorbisCommentValues()); } }
public void OpenAndCloseFlacFileWithStream() { // To avoid CA2202 (http://msdn.microsoft.com/query/dev12.query?appId=Dev12IDEF1&l=EN-US&k=k%28CA2202%29;k%28TargetFrameworkMoniker-.NETFramework) // we only use "using" for the inner resource. This ensure the Dispose of the Stream isn't called twice. Stream stream = null; try { stream = File.OpenRead(@"Data\testfile1.flac"); using (FlacFile file = new FlacFile(stream)) { stream = null; // Doing nothing, just testing an open and close action. } } finally { if (stream != null) { stream.Dispose(); } } }
public void OpenFlacFileAndCheckPadding() { using (FlacFile file = new FlacFile(@"Data\testfile1.flac")) { //Assert.IsTrue(file.Metadata.Count > 0, "No metadata blocks were found for the test file, this is not correct!"); foreach (MetadataBlock block in file.Metadata) { if (block.Header.Type == MetadataBlockHeader.MetadataBlockType.Padding) { Assert.AreEqual(block.Header.MetaDataBlockLength, ((Padding)block).EmptyBitCount / 8); } } } }
/// <summary> /// Gives you the vorbis comment metadata (ID3V2 tags). /// </summary> /// <param name="path"></param> /// <returns>The vorbis comment metadata or null if none is available.</returns> public static VorbisComment GetVorbisComment(string path) { using (FlacFile flac = new FlacFile(path)) { return flac.VorbisComment; } }
public void OpenFlacFileAndCheckSeekTable() { using (FlacFile file = new FlacFile(@"Data\testfile3.flac")) { var seekTable = file.SeekTable; // There's a seekpoint every 2 seconds so there should be 20 seekpoints ... Assert.AreEqual(20, seekTable.SeekPoints.Count); // We know seekpoint 0 should start at sample number 0, with an offset of 0 and number of samples = 4096 Assert.AreEqual<ulong>(0, seekTable.SeekPoints.Values[0].FirstSampleNumber); Assert.AreEqual<ulong>(0, seekTable.SeekPoints.Values[0].ByteOffset); Assert.AreEqual<ulong>(4096, seekTable.SeekPoints.Values[0].NumberOfSamples); // We know seekpoint 2 should start at sample number 176128, with an offset of 121744 and number of samples = 4096 Assert.AreEqual<ulong>(176128, seekTable.SeekPoints.Values[2].FirstSampleNumber); Assert.AreEqual<ulong>(121744, seekTable.SeekPoints.Values[2].ByteOffset); Assert.AreEqual<ushort>(4096, seekTable.SeekPoints.Values[2].NumberOfSamples); // We know seekpoint 5 should start at sample number 438272, with an offset of 302152 and number of samples = 4096 Assert.AreEqual<ulong>(438272, seekTable.SeekPoints.Values[5].FirstSampleNumber); Assert.AreEqual<ulong>(302152, seekTable.SeekPoints.Values[5].ByteOffset); Assert.AreEqual<ushort>(4096, seekTable.SeekPoints.Values[5].NumberOfSamples); // We know seekpoint 19 should start at sample number 1675264, with an offset of 1451579 and number of samples = 4096 Assert.AreEqual<ulong>(1675264, seekTable.SeekPoints.Values[19].FirstSampleNumber); Assert.AreEqual<ulong>(1451579, seekTable.SeekPoints.Values[19].ByteOffset); Assert.AreEqual<ushort>(4096, seekTable.SeekPoints.Values[19].NumberOfSamples); // Not testing ALL seekpoints ... } }
public void OpenFlacFileAndCheckPicture() { using (FlacFile file = new FlacFile(@"Data\testfile2.flac")) { //Assert.IsTrue(file.Metadata.Count > 0, "No metadata blocks were found for the test file, this is not correct!"); foreach (MetadataBlock block in file.Metadata) { if (block.Header.Type == MetadataBlockHeader.MetadataBlockType.Picture) { Picture info = (Picture)block; Assert.AreEqual<UInt32>(213, info.Height); Assert.AreEqual<UInt32>(400, info.Width); Assert.AreEqual(info.PictureType, PictureType.CoverFront); Assert.AreEqual(info.MIMEType, "image/jpeg"); } } } }
public void OpenFlacFileAndCheckStreamInfo() { using (FlacFile file = new FlacFile(@"Data\testfile1.flac")) { //Assert.IsTrue(file.Metadata.Count > 0, "No metadata blocks were found for the test file, this is not correct!"); foreach (MetadataBlock block in file.Metadata) { if (block.Header.Type == MetadataBlockHeader.MetadataBlockType.StreamInfo) { StreamInfo info = (StreamInfo)block; string md5sum = Helpers.ByteHelper.ByteArrayToString(info.MD5Signature); Assert.AreEqual("1d2e54a059ea776787ef66f1f93d3e34", md5sum); Assert.AreEqual(4096, info.MinimumBlockSize); Assert.AreEqual(4096, info.MaximumBlockSize); Assert.AreEqual<uint>(1427, info.MinimumFrameSize); Assert.AreEqual<uint>(7211, info.MaximumFrameSize); Assert.AreEqual<uint>(44100, info.SampleRateHz); Assert.AreEqual(1, info.Channels); Assert.AreEqual(16, info.BitsPerSample); Assert.AreEqual(1703592, info.Samples); } } } }
public void OpenFlacFileAndCheckVorbisComment() { using (FlacFile file = new FlacFile(@"Data\testfile1.flac")) { //Assert.IsTrue(file.Metadata.Count > 0, "No metadata blocks were found for the test file, this is not correct!"); foreach (MetadataBlock block in file.Metadata) { if (block.Header.Type == MetadataBlockHeader.MetadataBlockType.VorbisComment) { VorbisComment info = (VorbisComment)block; Assert.AreEqual("Ziggystar", info["ARTIST"].Value); Assert.AreEqual("Ziggystar", info.Artist.Value); Assert.AreEqual("Roland jx3p demo", info["TITLE"].Value); Assert.AreEqual("Roland jx3p demo", info.Title.Value); Assert.AreEqual("Wiki Commons", info["ALBUM"].Value); Assert.AreEqual("Wiki Commons", info.Album.Value); Assert.AreEqual("2005", info["DATE"].Value); Assert.AreEqual("2005", info.Date.Value); Assert.AreEqual("01", info["TRACKNUMBER"].Value); Assert.AreEqual("01", info.TrackNumber.Value); Assert.AreEqual("Electronic", info["GENRE"].Value); Assert.AreEqual("Electronic", info.Genre.Value); Assert.IsFalse(info.ContainsField("UNEXISTINGKEY")); } } } }
public void OpenInvalidFlacFile() { using (FlacFile file = new FlacFile(@"Data\noflacfile.ogg")) { // Doing nothing } }
/// <summary> /// Gets the specific vorbis field name (example = ARTIST) if it is available. /// </summary> /// <param name="path"></param> /// <param name="fieldName"></param> /// <returns>The value of the field or an empty string if the field is not available.</returns> public static VorbisCommentValues GetVorbisField(string path, string fieldName) { using (FlacFile flac = new FlacFile(path)) { if (flac.VorbisComment != null && flac.VorbisComment.ContainsField(fieldName)) { return flac.VorbisComment[fieldName]; } return new VorbisCommentValues(); } }
public void OpenFlacFileAndCheckMetadata() { using (FlacFile file = new FlacFile(@"Data\testfile1.flac")) { Assert.IsTrue(file.Metadata.Count > 0, "No metadata blocks were found for the test file, this is not correct!"); } }
private static void UpdateFlacTagsAsync(this MusicDb musicDb, MusicFile mf) { const string vorbisSeparators = ";\r\n\t"; using (var file = new fsl.FlacFile(mf.File)) { var vorbisComment = file.VorbisComment; if (vorbisComment != null) { foreach (var tag in vorbisComment) { try { var values = tag.Value.Select(x => x.Trim()).ToList(); if (values.Any(x => x.IndexOfAny(vorbisSeparators.ToCharArray()) >= 0)) { var subValues = new List <string>(); foreach (var item in values) { var parts = item.Split(vorbisSeparators.ToCharArray()); subValues.AddRange(parts.Select(x => x.Trim())); } values = subValues; } //var value = string.Join("|", tag.Value.Select(x => x.Trim()).ToArray()); var value = string.Join("|", values); var idTag = new IdTag { MusicFile = mf, Name = tag.Key, Value = value }; mf.IdTags.Add(idTag); //await musicDb.IdTags.AddAsync(idTag); } catch (Exception xe) { log.Error(xe); throw; } } } var pictures = file.GetAllPictures(); var picture = pictures.FirstOrDefault(x => x.PictureType == FlacLibSharp.PictureType.CoverFront); if (picture != null) { var idTag = new IdTag { MusicFile = mf, Name = "Pictures", PictureMimeType = picture.MIMEType, PictureData = picture.Data }; mf.IdTags.Add(idTag); //await musicDb.IdTags.AddAsync(idTag); } else { log.Debug($"{mf.File} {pictures.Count()} pictures found - but no FlacLibSharp.PictureType.CoverFront"); } if (mf.IdTags.SingleOrDefault(x => string.Compare(x.Name, "COMPOSITION", true) == 0) == null) { var w = mf.IdTags.SingleOrDefault(x => string.Compare(x.Name, "Work", true) == 0); if (w == null) { var title = mf.IdTags.SingleOrDefault(x => string.Compare(x.Name, "Title", true) == 0); if (title != null) { var parts = title.Value.Split(':'); var idTag = new IdTag { MusicFile = mf, Name = "COMPOSITION", Value = parts.First().Trim() }; mf.IdTags.Add(idTag); if (parts.Length > 1) { title.Value = string.Join(":", parts.Skip(1)).Trim(); } //else //{ // log.Information("pause"); //} } //if (title != null && title.Value.Contains(":")) //{ // var parts = title.Value.Split(':'); // var idTag = new IdTag // { // MusicFile = mf, // Name = "COMPOSITION", // Value = parts.First().Trim() // }; // mf.IdTags.Add(idTag); // //await musicDb.IdTags.AddAsync(idTag); //} } else { var work = w.Value.Split(':'); var idTag = new IdTag { MusicFile = mf, Name = "COMPOSITION", Value = work.First().Trim() }; mf.IdTags.Add(idTag); //await musicDb.IdTags.AddAsync(idTag); } } if (mf.IdTags.SingleOrDefault(x => string.Compare(x.Name, "Orchestra", true) == 0) == null) { //var performers = mf.GetTagValue("Performer") ?? mf.GetTagValue("Performers"); var performers = mf.GetTag("Performer") ?? mf.GetTag("Performers"); if (performers != null) { var orchestra = Extract("orchestra", performers.Value); if (orchestra != null) { performers.Value = Remove("orchestra", performers.Value); var idTag = new IdTag { MusicFile = mf, Name = "ORCHESTRA", Value = orchestra }; mf.IdTags.Add(idTag); //await musicDb.IdTags.AddAsync(idTag); } } } if (mf.IdTags.SingleOrDefault(x => string.Compare(x.Name, "Conductor", true) == 0) == null) { var performers = mf.GetTag("Performer") ?? mf.GetTag("Performers"); if (performers != null) { var orchestra = Extract("conductor", performers.Value); if (orchestra != null) { performers.Value = Remove("conductor", performers.Value); var idTag = new IdTag { MusicFile = mf, Name = "CONDUCTOR", Value = orchestra }; mf.IdTags.Add(idTag); //await musicDb.IdTags.AddAsync(idTag); } } } } }
public void OpenFlacFileAndCheckCueSheet() { using (FlacFile file = new FlacFile(@"Data\testfile4.flac")) { var cueSheet = file.CueSheet; Assert.IsNotNull(cueSheet, "No cuesheet found."); Assert.AreEqual<UInt32>(600, cueSheet.Header.MetaDataBlockLength); Assert.AreEqual<ulong>(88200, cueSheet.LeadInSampleCount); Assert.AreEqual(4, cueSheet.TrackCount); Assert.AreEqual(String.Empty, cueSheet.MediaCatalog); Assert.AreEqual(cueSheet.TrackCount, cueSheet.Tracks.Count); Assert.AreEqual<ulong>(0, cueSheet.Tracks[0].TrackOffset); Assert.AreEqual(1, cueSheet.Tracks[0].IndexPointCount); Assert.AreEqual(1, cueSheet.Tracks[0].TrackNumber); Assert.AreEqual(cueSheet.Tracks[0].IndexPoints.Count, cueSheet.Tracks[0].IndexPointCount); Assert.AreEqual(true, cueSheet.Tracks[0].IsAudioTrack); Assert.AreEqual(false, cueSheet.Tracks[0].IsPreEmphasis); Assert.AreEqual(String.Empty, cueSheet.Tracks[0].ISRC); Assert.AreEqual<ulong>(661500, cueSheet.Tracks[1].TrackOffset); Assert.AreEqual(2, cueSheet.Tracks[1].IndexPointCount); Assert.AreEqual(2, cueSheet.Tracks[1].TrackNumber); Assert.AreEqual(false, cueSheet.IsCDCueSheet); Assert.AreEqual(cueSheet.Tracks[1].IndexPoints.Count, cueSheet.Tracks[1].IndexPointCount, cueSheet.Tracks[1].IndexPoints.Count); Assert.AreEqual(true, cueSheet.Tracks[1].IsAudioTrack); Assert.AreEqual(false, cueSheet.Tracks[1].IsPreEmphasis); Assert.AreEqual(String.Empty, cueSheet.Tracks[1].ISRC); Assert.AreEqual<ulong>(1102500, cueSheet.Tracks[2].TrackOffset); Assert.AreEqual(2, cueSheet.Tracks[2].IndexPointCount); Assert.AreEqual(3, cueSheet.Tracks[2].TrackNumber); Assert.AreEqual(cueSheet.Tracks[2].IndexPoints.Count, cueSheet.Tracks[2].IndexPointCount); Assert.AreEqual(true, cueSheet.Tracks[2].IsAudioTrack); Assert.AreEqual(false, cueSheet.Tracks[2].IsPreEmphasis); Assert.AreEqual(String.Empty, cueSheet.Tracks[2].ISRC); Assert.AreEqual<ulong>(1703592, cueSheet.Tracks[3].TrackOffset); Assert.AreEqual(170, cueSheet.Tracks[3].TrackNumber); // Lead-out } }