-
Notifications
You must be signed in to change notification settings - Fork 52
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Synchronised Lyrics SYLT support #64
base: main
Are you sure you want to change the base?
Changes from all commits
5a8e77e
648733c
e2a19cc
b78ae3c
d90e31d
66359bf
d13ef0e
9353f66
a2a1866
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
module github.com/bogem/id3v2 | ||
module github.com/tramhao/id3v2 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This by mistake? |
||
|
||
go 1.13 | ||
|
||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,136 @@ | ||||||
// Copyright 2016 Albert Nigmatzianov. All rights reserved. | ||||||
// Use of this source code is governed by a MIT-style | ||||||
// license that can be found in the LICENSE file. | ||||||
|
||||||
package id3v2 | ||||||
|
||||||
import ( | ||||||
// "bytes" | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
"encoding/binary" | ||||||
// "fmt" | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These shouldn't be left in here. |
||||||
"io" | ||||||
) | ||||||
|
||||||
// SynchronisedLyricsFrame is used to work with SYLT frames. | ||||||
// The information about how to add synchronised lyrics/text frame to tag | ||||||
// you can see in the docs to tag.AddSynchronisedLyricsFrame function. | ||||||
// | ||||||
// You must choose a three-letter language code from | ||||||
// ISO 639-2 code list: https://www.loc.gov/standards/iso639-2/php/code_list.php | ||||||
type SynchronisedLyricsFrame struct { | ||||||
Encoding Encoding | ||||||
Language string | ||||||
TimestampFormat byte | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
ContentType byte | ||||||
ContentDescriptor string | ||||||
SynchronizedTexts []SyncedText | ||||||
} | ||||||
|
||||||
type TimestampFormat int | ||||||
|
||||||
const ( | ||||||
Unknown TimestampFormat = iota | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. IMO
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please also prefix timestamp formats. Please also get rid of Unknown, I can't find such TimestampFormat in spec const (
SYLTAbsoluteMpegFramesTimestampFormat = iota + 1
SYLTAbsoluteMillisecondsTimestampFormat
) |
||||||
AbsoluteMpegFrames | ||||||
AbsoluteMilliseconds | ||||||
) | ||||||
|
||||||
var ( | ||||||
ContentType = map[int]string{ | ||||||
0: "Other", | ||||||
1: "Lyrics", | ||||||
2: "Transcription", | ||||||
3: "Movement", | ||||||
4: "Events", | ||||||
5: "Chord", | ||||||
6: "Trivia", | ||||||
7: "WebpageUrls", | ||||||
8: "ImageUrls", | ||||||
} | ||||||
) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please also use const (
SYLTOtherContentType = iota
SYLTLyricsContentType
SYLTTextTranscriptionContentType
SYLTMovementContentType
SYLTEventsContentType
SYLTChordContentType
SYLTTriviaContentType
SYLTWebpageURLsContentType
SYLTImageURLsContentType
) |
||||||
|
||||||
type SyncedText struct { | ||||||
Text string | ||||||
Timestamp uint32 | ||||||
} | ||||||
|
||||||
func (sylf SynchronisedLyricsFrame) Size() int { | ||||||
var s int | ||||||
for _, v := range sylf.SynchronizedTexts { | ||||||
s += encodedSize(v.Text, sylf.Encoding) | ||||||
s += len(sylf.Encoding.TerminationBytes) | ||||||
s += 4 | ||||||
} | ||||||
|
||||||
return 1 + len(sylf.Language) + encodedSize(sylf.ContentDescriptor, sylf.Encoding) + | ||||||
+len(sylf.Encoding.TerminationBytes) + s + | ||||||
+1 + 1 | ||||||
} | ||||||
|
||||||
func (sylf SynchronisedLyricsFrame) UniqueIdentifier() string { | ||||||
return sylf.Language + sylf.ContentDescriptor | ||||||
} | ||||||
|
||||||
func (sy SyncedText) uintToBytes() []byte { | ||||||
bs := make([]byte, 4) | ||||||
binary.BigEndian.PutUint32(bs, sy.Timestamp) | ||||||
return bs | ||||||
} | ||||||
|
||||||
func bytesToInt(timeStampBytes []byte) uint32 { | ||||||
return binary.BigEndian.Uint32(timeStampBytes) | ||||||
} | ||||||
|
||||||
func (sylf SynchronisedLyricsFrame) WriteTo(w io.Writer) (n int64, err error) { | ||||||
if len(sylf.Language) != 3 { | ||||||
return n, ErrInvalidLanguageLength | ||||||
} | ||||||
return useBufWriter(w, func(bw *bufWriter) { | ||||||
bw.WriteByte(sylf.Encoding.Key) | ||||||
bw.WriteString(sylf.Language) | ||||||
bw.WriteByte(sylf.TimestampFormat) | ||||||
bw.WriteByte(sylf.ContentType) | ||||||
bw.EncodeAndWriteText(sylf.ContentDescriptor, sylf.Encoding) | ||||||
bw.Write(sylf.Encoding.TerminationBytes) | ||||||
for _, v := range sylf.SynchronizedTexts { | ||||||
bw.EncodeAndWriteText(v.Text, sylf.Encoding) | ||||||
bw.Write(sylf.Encoding.TerminationBytes) | ||||||
bw.Write(v.uintToBytes()) | ||||||
} | ||||||
}) | ||||||
} | ||||||
|
||||||
func parseSynchronisedLyricsFrame(br *bufReader) (Framer, error) { | ||||||
encoding := getEncoding(br.ReadByte()) | ||||||
language := br.Next(3) | ||||||
timestampFormat := br.ReadByte() | ||||||
contentType := br.ReadByte() | ||||||
contentDescriptor := br.ReadText(encoding) | ||||||
|
||||||
if br.Err() != nil { | ||||||
return nil, br.Err() | ||||||
} | ||||||
|
||||||
var s []SyncedText | ||||||
for { | ||||||
textLyric, err := br.readTillDelims(encoding.TerminationBytes) | ||||||
if err != nil { | ||||||
break | ||||||
} | ||||||
t := SyncedText{Text: decodeText(textLyric, encoding)} | ||||||
br.Next(len(encoding.TerminationBytes)) | ||||||
timeStamp := br.Next(4) | ||||||
timeStampUint := bytesToInt(timeStamp) | ||||||
t.Timestamp = timeStampUint | ||||||
s = append(s, t) | ||||||
} | ||||||
sylf := SynchronisedLyricsFrame{ | ||||||
Encoding: encoding, | ||||||
Language: string(language), | ||||||
TimestampFormat: timestampFormat, | ||||||
ContentType: contentType, | ||||||
ContentDescriptor: decodeText(contentDescriptor, encoding), | ||||||
SynchronizedTexts: s, | ||||||
} | ||||||
|
||||||
return sylf, nil | ||||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It need to be reverted 🤔