-
-
Notifications
You must be signed in to change notification settings - Fork 61
3. Usage _ Code snippets
Zeugma440 edited this page Apr 28, 2019
·
42 revisions
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
Reading an audio file (audio data & metadata)
Reading embedded pictures in a tag
Updating metadata (textual fields)
Updating metadata (embedded pictures)
Reading and writing BEXT, INFO and iXML metadata to a WAV file
Listing supported file formats
Receiving ATL logs in your app
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("Number of channels : " + theTrack.ChannelsArrangement.NbChannels);
System.Console.WriteLine("Channels arrangement : " + theTrack.ChannelsArrangement.Description);
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;
// Note : if you target ID3v2 chapters, it is highly advised to use Settings.ID3v2_tagSubVersion = 3
// as most readers only support ID3v2.3 chapters
Track theFile = new Track(audioFilePath);
theFile.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";
theFile.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";
// Add a picture to the 2nd chapter
ch.Picture = new PictureInfo(Commons.ImageFormat.Jpeg, PictureInfo.PIC_TYPE.Generic);
byte[] data = System.IO.File.ReadAllBytes(imagePath);
ch.Picture.PictureData = data;
theFile.Chapters.Add(ch);
// Persists the chapters
theFile.Save();
// Reads the file again from sratch
theFile = new Track(audioFilePath);
IList<PictureInfo> pics = theFile.EmbeddedPictures; // Hack to load chapter pictures
// Display chapters
foreach (ChapterInfo chap in theFile.Chapters)
{
System.Console.WriteLine(chap.Title + "(" + chap.StartTime + ")");
}
using ATL.AudioData;
// Load audio file information into memory
Track theTrack = new Track(audioFilePath);
// Display BEXT, LIST INFO and iXML data
string originator = "", engineer = "", scene = "";
if (theTrack.AdditionalFields.ContainsKey("bext.originator")) originator = theTrack.AdditionalFields["bext.originator"];
if (theTrack.AdditionalFields.ContainsKey("info.IENG")) engineer = theTrack.AdditionalFields["info.IENG"];
if (theTrack.AdditionalFields.ContainsKey("ixml.SCENE")) scene = theTrack.AdditionalFields["ixml.SCENE"];
System.Console.WriteLine("Originator : " + originator);
System.Console.WriteLine("Engineer : " + engineer);
System.Console.WriteLine("Scene : " + scene);
// Modify data
theTrack.AdditionalFields["bext.originator"] = "Dave Johnson";
theTrack.AdditionalFields["info.IENG"] = "John Jackman";
theTrack.AdditionalFields["ixml.SCENE"] = "42";
theTrack.Save();
using ATL.Playlist;
IPlaylistIO theReader = PlaylistIOFactory.GetInstance().GetPlaylistIO(playlistPath);
foreach (string s in theReader.FilePaths)
{
System.Console.WriteLine(s);
}
using ATL.Playlist;
IPlaylistIO pls = PlaylistIOFactory.GetInstance().GetPlaylistIO(playlistFilePath);
// Writing file paths
IList<string> pathsToWrite = new List<string>();
pathsToWrite.Add("aaa.mp3");
pathsToWrite.Add("bbb.mp3");
pls.FilePaths = pathsToWrite;
// Writing tracks
IList<Track> tracksToWrite = new List<Track>();
tracksToWrite.Add(new Track(TestUtils.GetResourceLocationRoot() + "MP3/empty.mp3"));
tracksToWrite.Add(new Track(TestUtils.GetResourceLocationRoot() + "MOD/mod.mod"));
pls.Tracks = tracksToWrite;
using ATL.CatalogDataReaders;
ICatalogDataReader theReader = CatalogDataReaderFactory.GetInstance().GetCatalogDataReader(cuesheetPath);
System.Console.WriteLine(theReader.Artist);
System.Console.WriteLine(theReader.Title);
System.Console.WriteLine(theReader.Comments);
foreach (Track t in theReader.Tracks)
{
System.Console.WriteLine(">" + t.Title);
}
using ATL.AudioData;
using ATL.Playlist;
System.Text.StringBuilder filter = new System.Text.StringBuilder("");
foreach (Format f in PlaylistIOFactory.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();
System.Collections.Generic.IList<Log.LogItem> messages = new System.Collections.Generic.IList<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 1");
LogDelegator.GetLogDelegate()(Log.LV_WARNING, "test message 2");
System.Console.WriteLine(messages[0].Message);
}
public void DoLog(Log.LogItem anItem)
{
messages.Add(anItem);
}
}