Skip to content

Commit

Permalink
V2.1 (#68)
Browse files Browse the repository at this point in the history
* Add v2 folder and revert changes in root

* Update README
  • Loading branch information
n10v authored Nov 23, 2021
1 parent 033cd25 commit da76e5a
Show file tree
Hide file tree
Showing 50 changed files with 4,555 additions and 61 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Supported ID3 versions: 2.3, 2.4
## Installation

```
go get -u github.com/bogem/id3v2
go get -u github.com/bogem/id3v2/v2
```

## Usage example
Expand All @@ -17,7 +17,7 @@ import (
"fmt"
"log"

"github.com/bogem/id3v2"
"github.com/bogem/id3v2/v2"
)

func main() {
Expand Down Expand Up @@ -67,7 +67,7 @@ for _, f := range pictures {

## Encodings

For example, if you want to set comment frame with custom encoding,
For example, if you want to set comment frame with custom encoding,
you may do the following:

```go
Expand Down
2 changes: 1 addition & 1 deletion comment_frame.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func (cf CommentFrame) WriteTo(w io.Writer) (n int64, err error) {
})
}

func parseCommentFrame(br *bufReader, version byte) (Framer, error) {
func parseCommentFrame(br *bufReader) (Framer, error) {
encoding := getEncoding(br.ReadByte())
language := br.Next(3)
description := br.ReadText(encoding)
Expand Down
5 changes: 1 addition & 4 deletions common_ids.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import "strings"
var (
V23CommonIDs = map[string]string{
"Attached picture": "APIC",
"Chapters": "CHAP",
"Comments": "COMM",
"Album/Movie/Show title": "TALB",
"BPM": "TBPM",
Expand Down Expand Up @@ -63,7 +62,6 @@ var (

V24CommonIDs = map[string]string{
"Attached picture": "APIC",
"Chapters": "CHAP",
"Comments": "COMM",
"Album/Movie/Show title": "TALB",
"BPM": "TBPM",
Expand Down Expand Up @@ -137,9 +135,8 @@ var (
// if strings.HasPrefix(id, "T") {
// ...
// }
var parsers = map[string]func(*bufReader, byte) (Framer, error){
var parsers = map[string]func(*bufReader) (Framer, error){
"APIC": parsePictureFrame,
"CHAP": parseChapterFrame,
"COMM": parseCommentFrame,
"POPM": parsePopularimeterFrame,
"TXXX": parseUserDefinedTextFrame,
Expand Down
2 changes: 1 addition & 1 deletion encoding_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func TestUnsynchronisedLyricsFrameWithUTF16(t *testing.T) {
t.Fatal(err)
}

parsed, err := parseUnsynchronisedLyricsFrame(newBufReader(buf), 4)
parsed, err := parseUnsynchronisedLyricsFrame(newBufReader(buf))
if err != nil {
t.Fatal(err)
}
Expand Down
6 changes: 3 additions & 3 deletions parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func (tag *Tag) parseFrames(opts Options) error {
}

br.Reset(bodyRd)
frame, err := parseFrameBody(id, br, tag.version)
frame, err := parseFrameBody(id, br)
if err != nil && err != io.EOF {
return err
}
Expand Down Expand Up @@ -174,13 +174,13 @@ func skipReaderBuf(rd io.Reader, buf []byte) error {
return nil
}

func parseFrameBody(id string, br *bufReader, version byte) (Framer, error) {
func parseFrameBody(id string, br *bufReader) (Framer, error) {
if id[0] == 'T' && id != "TXXX" {
return parseTextFrame(br)
}

if parseFunc, exists := parsers[id]; exists {
return parseFunc(br, version)
return parseFunc(br)
}

return parseUnknownFrame(br)
Expand Down
5 changes: 2 additions & 3 deletions picture_frame.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
package id3v2

import (
"fmt"
"io"
)

Expand All @@ -23,7 +22,7 @@ type PictureFrame struct {
}

func (pf PictureFrame) UniqueIdentifier() string {
return fmt.Sprintf("%02X%s", pf.PictureType, pf.Description)
return pf.Description
}

func (pf PictureFrame) Size() int {
Expand All @@ -43,7 +42,7 @@ func (pf PictureFrame) WriteTo(w io.Writer) (n int64, err error) {
})
}

func parsePictureFrame(br *bufReader, version byte) (Framer, error) {
func parsePictureFrame(br *bufReader) (Framer, error) {
encoding := getEncoding(br.ReadByte())
mimeType := br.ReadText(EncodingISO)
pictureType := br.ReadByte()
Expand Down
2 changes: 1 addition & 1 deletion popularimeter_frame.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func (pf PopularimeterFrame) WriteTo(w io.Writer) (n int64, err error) {
})
}

func parsePopularimeterFrame(br *bufReader, version byte) (Framer, error) {
func parsePopularimeterFrame(br *bufReader) (Framer, error) {
email := br.ReadText(EncodingISO)
rating := br.ReadByte()

Expand Down
51 changes: 14 additions & 37 deletions sequence_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,13 @@ func TestSequenceCommentFramesUniqueness(t *testing.T) {
defer putSequence(s)

s.AddFrame(CommentFrame{Language: "A", Description: "A"})
testSequenceCount(t, s, 1)
testFrameUniqueIdentifier(t, s.Frames()[0], "AA")
testSequenceA(t, s)

s.AddFrame(CommentFrame{Language: "B", Description: "B"})
testSequenceCount(t, s, 2)
testFrameUniqueIdentifier(t, s.Frames()[0], "AA")
testFrameUniqueIdentifier(t, s.Frames()[1], "BB")
testSequenceAB(t, s)

s.AddFrame(CommentFrame{Language: "B", Description: "B"})
testSequenceCount(t, s, 2)
testFrameUniqueIdentifier(t, s.Frames()[0], "AA")
testFrameUniqueIdentifier(t, s.Frames()[1], "BB")
testSequenceAB(t, s)
}

func TestSequencePictureFramesUniqueness(t *testing.T) {
Expand All @@ -33,27 +28,14 @@ func TestSequencePictureFramesUniqueness(t *testing.T) {
s := getSequence()
defer putSequence(s)

s.AddFrame(PictureFrame{Description: "A", PictureType: 0x00})
testSequenceCount(t, s, 1)
testFrameUniqueIdentifier(t, s.Frames()[0], "00A")
s.AddFrame(PictureFrame{Description: "A"})
testSequenceA(t, s)

// Test against https://github.com/bogem/id3v2/issues/65 regression.
s.AddFrame(PictureFrame{Description: "A", PictureType: 0x01})
testSequenceCount(t, s, 2)
testFrameUniqueIdentifier(t, s.Frames()[0], "00A")
testFrameUniqueIdentifier(t, s.Frames()[1], "01A")

s.AddFrame(PictureFrame{Description: "B", PictureType: 0x00})
testSequenceCount(t, s, 3)
testFrameUniqueIdentifier(t, s.Frames()[0], "00A")
testFrameUniqueIdentifier(t, s.Frames()[1], "01A")
testFrameUniqueIdentifier(t, s.Frames()[2], "00B")

s.AddFrame(PictureFrame{Description: "B", PictureType: 0x00})
testSequenceCount(t, s, 3)
testFrameUniqueIdentifier(t, s.Frames()[0], "00A")
testFrameUniqueIdentifier(t, s.Frames()[1], "01A")
testFrameUniqueIdentifier(t, s.Frames()[2], "00B")
s.AddFrame(PictureFrame{Description: "B"})
testSequenceAB(t, s)

s.AddFrame(PictureFrame{Description: "B"})
testSequenceAB(t, s)
}

func TestSequenceUSLFsUniqueness(t *testing.T) {
Expand All @@ -63,18 +45,13 @@ func TestSequenceUSLFsUniqueness(t *testing.T) {
defer putSequence(s)

s.AddFrame(UnsynchronisedLyricsFrame{Language: "A", ContentDescriptor: "A"})
testSequenceCount(t, s, 1)
testFrameUniqueIdentifier(t, s.Frames()[0], "AA")
testSequenceA(t, s)

s.AddFrame(UnsynchronisedLyricsFrame{Language: "B", ContentDescriptor: "B"})
testSequenceCount(t, s, 2)
testFrameUniqueIdentifier(t, s.Frames()[0], "AA")
testFrameUniqueIdentifier(t, s.Frames()[1], "BB")
testSequenceAB(t, s)

s.AddFrame(UnsynchronisedLyricsFrame{Language: "B", ContentDescriptor: "B"})
testSequenceCount(t, s, 2)
testFrameUniqueIdentifier(t, s.Frames()[0], "AA")
testFrameUniqueIdentifier(t, s.Frames()[1], "BB")
testSequenceAB(t, s)
}

func TestSequenceUDTFsUniqueness(t *testing.T) {
Expand Down Expand Up @@ -119,7 +96,7 @@ func testSequenceCount(t *testing.T, s *sequence, expected int) {
}

func testFrameUniqueIdentifier(t *testing.T, f Framer, expected string) {
got := f.UniqueIdentifier()
got := f.UniqueIdentifier()[:1]
if got != expected {
t.Errorf("Expected frame with unique identifier %v, got %v", expected, got)
}
Expand Down
5 changes: 0 additions & 5 deletions tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,6 @@ func (tag *Tag) AddAttachedPicture(pf PictureFrame) {
tag.AddFrame(tag.CommonID("Attached picture"), pf)
}

// AddChapterFrame adds the chapter frame to tag.
func (tag *Tag) AddChapterFrame(cf ChapterFrame) {
tag.AddFrame(tag.CommonID("Chapters"), cf)
}

// AddCommentFrame adds the comment frame to tag.
func (tag *Tag) AddCommentFrame(cf CommentFrame) {
tag.AddFrame(tag.CommonID("Comments"), cf)
Expand Down
2 changes: 1 addition & 1 deletion ufid_frame.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func (ufid UFIDFrame) WriteTo(w io.Writer) (n int64, err error) {
})
}

func parseUFIDFrame(br *bufReader, version byte) (Framer, error) {
func parseUFIDFrame(br *bufReader) (Framer, error) {
owner := br.ReadText(EncodingISO)
ident := br.ReadAll()

Expand Down
2 changes: 1 addition & 1 deletion unsynchronised_lyrics_frame.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func (uslf UnsynchronisedLyricsFrame) WriteTo(w io.Writer) (n int64, err error)
})
}

func parseUnsynchronisedLyricsFrame(br *bufReader, version byte) (Framer, error) {
func parseUnsynchronisedLyricsFrame(br *bufReader) (Framer, error) {
encoding := getEncoding(br.ReadByte())
language := br.Next(3)
contentDescriptor := br.ReadText(encoding)
Expand Down
2 changes: 1 addition & 1 deletion user_defined_text_frame.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func (udtf UserDefinedTextFrame) WriteTo(w io.Writer) (n int64, err error) {
})
}

func parseUserDefinedTextFrame(br *bufReader, version byte) (Framer, error) {
func parseUserDefinedTextFrame(br *bufReader) (Framer, error) {
encoding := getEncoding(br.ReadByte())
description := br.ReadText(encoding)

Expand Down
120 changes: 120 additions & 0 deletions v2/bench_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
// 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"
"io/ioutil"
"testing"
)

var frontCoverPicture = mustReadFile(frontCoverPath)

func BenchmarkParseAllFrames(b *testing.B) {
writeTag(b, EncodingUTF8)
musicContent := mustReadFile(mp3Path)
b.ResetTimer()

for n := 0; n < b.N; n++ {
tag, err := ParseReader(bytes.NewReader(musicContent), parseOpts)
if tag == nil || err != nil {
b.Fatal("Error while opening mp3 file:", err)
}
}
}

func BenchmarkParseAllFramesISO(b *testing.B) {
writeTag(b, EncodingISO)
musicContent := mustReadFile(mp3Path)
b.ResetTimer()

for n := 0; n < b.N; n++ {
tag, err := ParseReader(bytes.NewReader(musicContent), parseOpts)
if tag == nil || err != nil {
b.Fatal("Error while opening mp3 file:", err)
}
}
}

func BenchmarkParseArtistAndTitle(b *testing.B) {
writeTag(b, EncodingUTF8)
musicContent := mustReadFile(mp3Path)
b.ResetTimer()

for n := 0; n < b.N; n++ {
opts := Options{Parse: true, ParseFrames: []string{"Artist", "Title"}}
tag, err := ParseReader(bytes.NewReader(musicContent), opts)
if tag == nil || err != nil {
b.Fatal("Error while opening mp3 file:", err)
}
}
}

func BenchmarkWrite(b *testing.B) {
for n := 0; n < b.N; n++ {
benchWrite(b, EncodingUTF8)
}
}

func BenchmarkWriteISO(b *testing.B) {
for n := 0; n < b.N; n++ {
benchWrite(b, EncodingISO)
}
}

func benchWrite(b *testing.B, encoding Encoding) {
tag := NewEmptyTag()
setFrames(tag, encoding)
if _, err := tag.WriteTo(ioutil.Discard); err != nil {
b.Error("Error while writing a tag:", err)
}
}

func writeTag(b *testing.B, encoding Encoding) {
tag, err := Open(mp3Path, Options{Parse: false})
if tag == nil || err != nil {
b.Fatal("Error while opening mp3 file:", err)
}
defer tag.Close()

setFrames(tag, encoding)

if err = tag.Save(); err != nil {
b.Error("Error while saving a tag:", err)
}
}

func setFrames(tag *Tag, encoding Encoding) {
tag.SetTitle("Title")
tag.SetArtist("Artist")
tag.SetAlbum("Album")
tag.SetYear("2016")
tag.SetGenre("Genre")

pic := PictureFrame{
Encoding: encoding,
MimeType: "image/jpeg",
PictureType: PTFrontCover,
Description: "Front cover",
Picture: frontCoverPicture,
}
tag.AddAttachedPicture(pic)

uslt := UnsynchronisedLyricsFrame{
Encoding: encoding,
Language: "eng",
ContentDescriptor: "Content descriptor",
Lyrics: "bogem/id3v2",
}
tag.AddUnsynchronisedLyricsFrame(uslt)

comm := CommentFrame{
Encoding: encoding,
Language: "eng",
Description: "Short description",
Text: "The actual text",
}
tag.AddCommentFrame(comm)
}
Loading

0 comments on commit da76e5a

Please sign in to comment.