Author Topic: Tags Library Question  (Read 137 times)

trojannemo

  • Posts: 143
Tags Library Question
« on: 31 Jul '24 - 04:24 »
I found a way to do it with my own code without needing the Tags Library. We can disregard this post.

So this is regarding the Tags Library add on, not the Tags add on that is on the main page. I went to post in the original thread but it said it had been over a year since any replies and I should make a new post. Hopefully the developer sees this.

I'm trying to attach album art to ogg output files. I got all other metadata down with Bass and its add-ons, but don't see a way to set the album art. Tags Library seems to do it, but I keep getting a memory error no matter what I try. And I have tried quite a bit over the last 3-4 hours of non stop variations of code.

So I'm hoping the developer or someone with more experience can tell me what I'm doing wrong. This is the relevant C# code:

Code: [Select]
if (out_ext == "ogg" && File.Exists(art))
{
    var Tags = TagsLib.TagsLibrary_Create();
    TagsLib.TagsLibrary_Load(Tags, output, TTagType.ttOpusVorbis, true);
    if (Tags.HTags != IntPtr.Zero)
    {
        var imgBytes = File.ReadAllBytes(art);
        var img = Image.FromFile(art);                   
        var coverArt = new TCoverArtData();                   
        coverArt.Index = 0;
        if (ImageFormat.Png.Equals(img.RawFormat))
        {
            coverArt.PictureFormat = TTagPictureFormat.tpfPNG;
            coverArt.MIMETypeSPtr = Marshal.StringToHGlobalAuto("image/png");
        }
        else if (ImageFormat.Bmp.Equals(img.RawFormat))
        {
            coverArt.PictureFormat = TTagPictureFormat.tpfBMP;
            coverArt.MIMETypeSPtr = Marshal.StringToHGlobalAuto("image/bmp");
        }
        else if (ImageFormat.Gif.Equals(img.RawFormat))
        {
            coverArt.PictureFormat = TTagPictureFormat.tpfGIF;
            coverArt.MIMETypeSPtr = Marshal.StringToHGlobalAuto("image/gif");
        }   
        else
        {
            coverArt.PictureFormat = TTagPictureFormat.tpfJPEG;
            coverArt.MIMETypeSPtr = Marshal.StringToHGlobalAuto("image/jpeg");
        }                   
        coverArt.Width = img.Width;
        coverArt.Height = img.Height;
        switch (img.PixelFormat)
        {
            case PixelFormat.Format1bppIndexed:
                coverArt.ColorDepth = 1;
                break;
            case PixelFormat.Format4bppIndexed:
                coverArt.ColorDepth = 4;
                break;
            case PixelFormat.Format8bppIndexed:
                coverArt.ColorDepth = 8;
                break;
            case PixelFormat.Format16bppGrayScale:
            case PixelFormat.Format16bppRgb555:
            case PixelFormat.Format16bppRgb565:
            case PixelFormat.Format16bppArgb1555:
                coverArt.ColorDepth = 16;
                break;                       
            case PixelFormat.Format32bppRgb:
            case PixelFormat.Format32bppArgb:
            case PixelFormat.Format32bppPArgb:
                coverArt.ColorDepth = 32;
                break;
            case PixelFormat.Format24bppRgb:
            default:
                coverArt.ColorDepth = 24;
                break;
        }
        coverArt.NoOfColors = 0;
        coverArt.CoverType = TCoverTypes.ctCoverFront;
        IntPtr imagePtr = TagsLib_API.Utils.Utils.GetPointerfromByteArray(imgBytes);
        coverArt.Data = imagePtr;
        long length = 0;
        using (MemoryStream memoryStream = new MemoryStream())
        {
            if (ImageFormat.Png.Equals(img.RawFormat))
            {
                img.Save(memoryStream, ImageFormat.Png);
            }
            else if (ImageFormat.Bmp.Equals(img.RawFormat))
            {
                img.Save(memoryStream, ImageFormat.Bmp);
            }
            else if (ImageFormat.Gif.Equals(img.RawFormat))
            {
                img.Save(memoryStream, ImageFormat.Gif);
            }
            else
            {
                img.Save(memoryStream, ImageFormat.Jpeg);
            }
            length = memoryStream.Length;
        }
        coverArt.DataSize = length;
        try
        {
            TagsLib.TagsLibrary_AddCoverArt(Tags, TTagType.ttOpusVorbis, coverArt);                       
        }
        finally
        {
            TagsLib.TagsLibrary_Free(Tags);
        }             
    }

The memory error always happens at this line:

Code: [Select]
TagsLib.TagsLibrary_AddCoverArt(Tags, TTagType.ttOpusVorbis, coverArt);

So there's something I'm missing.

Any help is appreciated. Thanks!


EDIT:

I've also tried:

Code: [Select]
TagsLib.TagsLibrary_SetCoverArt(Tags, TTagType.ttOpusVorbis, 0, ref coverArt);

and

Code: [Select]
TagsLib.TagsLibrary_SetCoverArtFromFile(Tags, TTagType.ttOpusVorbis, 0, art, coverArt);

Both of those proceed without any error but they do not save the album art to the resulting file. Maybe I'm missing a step there or I'm doing it wrong too. At least there's no memory error.[/s]
« Last Edit: 3 Aug '24 - 00:53 by trojannemo »

trojannemo

  • Posts: 143
Re: Tags Library Question
« Reply #1 on: 1 Aug '24 - 05:56 »
I've changed the code a ton and still encountering problems.

I've narrowed it down to the following:

Code: [Select]
var index = TagsLib.TagsLibrary_AddCoverArt(Tags, TTagType.ttAutomatic, coverArt);

If I change the TTgType to ANY value between 1 and 9, I get the AccessViolationException. If I put 0 (none) or any number above 9, it returns a -1 error code so it doesn't do what it's supposed to do, but the AccessViolationException goes away.

So, it stands to reason that the problem is coming from the TTagType - but I don't know how to get around that. I've even tried directly importing the DLL and doing my own p/invoke of the function. Same behavior.

Very frustrating.