-
-
Notifications
You must be signed in to change notification settings - Fork 65
3. Usage _ Code snippets
Zeugma440 edited this page Jan 14, 2018
·
42 revisions
Reading an audio file (audio data & metadata)
Reading embedded pictures in a tag
Updating metadata (textual fields)
Updating metadata (embedded pictures)
Listing supported file formats
Receiving ATL logs in your app
For much more use cases and working sample code, feel free to browse the ATL.test project (home of the many unit tests that ensure each commit still works as designed)
Feel free to drop a line through the Issues form if you need to add a specific use case here
using ATL.AudioData;
// Adapt this to whatever your file path needs to be
string fileName = "C:/temp/MP3/test.mp3";
// Load audio file information into memory
Track theTrack = new Track(fileName);
// That's it ! Now try and display classic 'supported' fields
System.Console.WriteLine("Title : " + theTrack.Title);
System.Console.WriteLine("Artist : " + theTrack.Artist);
System.Console.WriteLine("Album : " + theTrack.Album);
System.Console.WriteLine("Recording year : " + theTrack.Year);
System.Console.WriteLine("Track number : " + theTrack.TrackNumber);
System.Console.WriteLine("Disc number : " + theTrack.DiscNumber);
System.Console.WriteLine("Genre : " + theTrack.Genre);
System.Console.WriteLine("Comment : " + theTrack.Comment);
System.Console.WriteLine("Duration (s) : " + theTrack.Duration);
System.Console.WriteLine("Bitrate (KBps) : " + theTrack.Bitrate);
System.Console.WriteLine("Has this file variable bitrate audio : " + (theTrack.IsVBR ? "yes" : "no"));
System.Console.WriteLine("Has this file lossless audio : " + (AudioDataIOFactory.CF_LOSSLESS == theTrack.CodecFamily ? "yes" : "no"));
// Display any additional 'unsupported' fields (e.g. TXXX values in ID3v2, or any other custom tag)
foreach (System.Collections.Generic.KeyValuePair<string, string> field in theTrack.AdditionalFields)
{
System.Console.WriteLine("Field " + field.Key + " : value = " + field.Value);
}
using ATL.AudioData;
// Adapt this to whatever your file path needs to be
string fileName = "E:/temp/MP3/test.mp3";
// Load audio file information into memory
Track theTrack = new Track(fileName);
// Get picture list
System.Collections.Generic.IList<PictureInfo> embeddedPictures = theTrack.EmbeddedPictures;
// Transform them into .NET Image, if needed
foreach (PictureInfo pic in embeddedPictures)
{
System.Drawing.Image image = System.Drawing.Image.FromStream((new System.IO.MemoryStream(pic.PictureData)));
}
using ATL.AudioData;
// Adapt this to whatever your file path needs to be
string fileName = "C:/temp/MP3/test.mp3";
// Load audio file information into memory
Track theTrack = new Track(fileName);
// Modify metadata
theTrack.Artist = "Hey ho";
theTrack.Composer = "Oscar Wilde";
// Save modifications on the disc
theTrack.Save();
using ATL.AudioData;
// Adapt this to whatever your file path needs to be
string fileName = "E:/temp/MP3/test.mp3";
// Load audio file information into memory
Track theTrack = new Track(fileName);
// Delete first embedded picture (let's say it exists)
theTrack.EmbeddedPictures.RemoveAt(0);
// Add 'CD' embedded picture
PictureInfo newPicture = new PictureInfo(Commons.ImageFormat.Gif, PictureInfo.PIC_TYPE.CD);
newPicture.PictureData = System.IO.File.ReadAllBytes("E:/temp/_Images/pic1.gif");
theTrack.EmbeddedPictures.Add(newPicture);
// Save modifications on the disc
theTrack.Save();
using ATL.AudioData;
AudioDataManager theFile = new AudioDataManager(AudioDataIOFactory.GetInstance().GetDataReader(fileName));
TagData theTag = new TagData();
theTag.Chapters = new System.Collections.Generic.List<ChapterInfo>();
ChapterInfo ch = new ChapterInfo();
ch.StartTime = 123;
ch.StartOffset = 456;
ch.EndTime = 789;
ch.EndOffset = 101112;
ch.UniqueID = "";
ch.Title = "aaa";
ch.Subtitle = "bbb";
ch.Url = "ccc\0ddd";
theTag.Chapters.Add(ch);
ch = new ChapterInfo();
ch.StartTime = 1230;
ch.StartOffset = 4560;
ch.EndTime = 7890;
ch.EndOffset = 1011120;
ch.UniqueID = "002";
ch.Title = "aaa0";
ch.Subtitle = "bbb0";
ch.Url = "ccc\0ddd0";
theTag.Chapters.Add(ch);
// Persists the chapters
theFile.UpdateTagInFile(theTag, MetaDataIOFactory.TAG_ID3V2);
// Reads them
theFile.ReadFromFile(false, true);
foreach (ChapterInfo chap in theFile.ID3v2.Chapters)
{
System.Console.WriteLine(chap.Title + "(" + chap.StartTime + ")");
}
using ATL.PlaylistReaders;
IPlaylistReader theReader = PlaylistReaderFactory.GetInstance().GetPlaylistReader(playlistPath);
foreach (string s in theReader.GetFiles())
{
System.Console.WriteLine(s);
}
using ATL.CatalogDataReaders;
ICatalogDataReader theReader = CatalogDataReaderFactory.GetInstance().GetCatalogDataReader("../../Resources/duck hunt.cue");
System.Console.WriteLine(theReader.Artist);
System.Console.WriteLine(theReader.Title);
foreach(Track t in theReader.Tracks)
{
System.Console.WriteLine(">" + t.Title);
}
using ATL.AudioData;
StringBuilder filter = new StringBuilder("");
foreach (Format f in ATL.PlaylistReaders.PlaylistReaderFactory.GetInstance().getFormats())
{
if (f.Readable)
{
foreach (String extension in f)
{
filter.Append(extension).Append(";");
}
}
}
// Removes the last separator
filter.Remove(filter.Length - 1, 1);
using ATL.Logging;
public class LoggingTest : ILogDevice
{
Log theLog = new Log();
IList<Log.LogItem> messages = new List<Log.LogItem>();
public LoggingTest()
{
LogDelegator.SetLog(ref theLog);
theLog.Register(this);
}
public void TestSyncMessage()
{
messages.Clear();
LogDelegator.GetLocateDelegate()("file name");
LogDelegator.GetLogDelegate()(Log.LV_DEBUG, "test message");
System.Console.WriteLine(messages[0](0).Message);
}
public void DoLog(Log.LogItem anItem)
{
messages.Add(anItem);
}
}