简体   繁体   English

TAG_ID3(来自Bass 2.4.4)无法正常工作

[英]TAG_ID3 (from Bass 2.4.4) doesn't work properly

I'm trying to read ID3v1 tags from mp3 files using "BASS.dll"(via bass.lib and bass.h). 我正在尝试使用“ BASS.dll”(通过bass.lib和bass.h)从mp3文件中读取ID3v1标签。
It works fine until .mp3 file has title (or artist) has 30 characters. 在.mp3文件的标题(或演出者)具有30个字符之前,它可以正常工作。
Instead Happy Times (Feat. Margaux Bos I get Happy Times (Feat. Margaux BosEmigrate 取而代之的是Happy Times (Feat. Margaux Bos Happy Times (Feat. Margaux BosEmigrate Happy Times (Feat. Margaux Bos我得到了Happy Times (Feat. Margaux BosEmigrate
with Emigrate added (that's artist tag). 并添加了Emigrate (这是艺术家标记)。

How to make it work properly, without adding artist tag? 如何在不添加艺术家标签的情况下使其正常工作?
Here is my source code: 这是我的源代码:

//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Unit2.h"
#include "bass.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm2 *Form2;

//---------------------------------------------------------------------------
__fastcall TForm2::TForm2(TComponent* Owner)
    : TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm2::Button1Click(TObject *Sender)
{
    BASS_Init(-1, 44000, 0, 0, 0);

    if(OpenDialog1->Execute())
    {
       HSTREAM stream = BASS_StreamCreateFile(false, OpenDialog1->FileName.c_str(), 0, 0, 0);
       TAG_ID3 *tags = (TAG_ID3*)BASS_ChannelGetTags(stream, BASS_TAG_ID3);
       Edit1->Text = tags->title;
    }
}

The text fields of the TAG_ID3 struct are not guaranteed to be null terminated, but your code is treating them as if they are, so it ends up reading into the next field when a null terminator is not present. 不能保证TAG_ID3结构的文本字段以null结尾,但是您的代码将它们视为是空值,因此当不存在null终止符时,它最终将读入下一个字段。 To fix that, you have to take their max lengths into account, eg: 要解决此问题,您必须考虑其最大长度,例如:

Edit1->Text = AnsiString().sprintf("%.*s", sizeof(tags->title), tags->title);

Or: 要么:

Edit1->Text = AnsiString(tags->title, sizeof(tags->title)).TrimRight();

Same with all of other text fields: 与所有其他文本字段相同:

  • id : 3 chars id :3个字符
  • title : 30 chars title :30个字符
  • artist : 30 chars artist :30个字符
  • album : 30 chars album :30个字符
  • year : 4 chars year :4个字符
  • comment : 30 chars comment :30个字符

You can use a simple template wrapper to help you: 您可以使用简单的模板包装程序来帮助您:

template<size_t N>
String toString(char (&arr)[N])
{
    return AnsiString().sprintf("%.*s", N, arr); 
    /* or:
    return AnsiString(arr, N).TrimRight();
    */
}

Edit1->Text = toString(tags->title);

Note that the comment field has an additional caveat to watch out for: 请注意, comment字段还需要注意以下事项:

If the 30th character is non-null whilst the 29th character is null, then the 30th character is the track number and the comment is limited to the first 28 characters. 如果第30个字符不为空,而第29个字符为空,则第30个字符为曲目号,并且注释仅限于前28个字符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM