Skip to content

Commit

Permalink
BPM metadata enhancement (#1087)
Browse files Browse the repository at this point in the history
* BPM metadata enhancement

Related to #1036.

Adds BPM to the stored metadata about MediaFiles.

Displays BPM in the following locations:
- Listing songs in the song list (desktop, sortable)
- Listing songs in playlists (desktop, sortable)
- Listing songs in albums (desktop)
- Expanding song details

When listing, shows a blank field if no BPM is present. When showing song details, shows a question mark.

Updates test MP3 file to have BPM tag. Updated test to ensure tag is read correctly.

Updated localization files. Most languages just use "BPM" as discovered during research on Wikipedia. However, a couple use some different nomenclature. Spanish uses PPM and Japanese uses M.M.

* Enhances support for BPM metadata extraction

- Supports reading floating point BPM (still storing it as an integer) and FFmpeg as the extractor
- Replaces existing .ogg test file with one that shouldn't fail randomly
- Adds supporting tests for both FFmpeg and TagLib

* Addresses various issues with PR #1087.

- Adds index for BPM. Removes drop column as it's not supported by SQLite (duh).
- Removes localizations for BPM as those will be done in POEditor.
- Moves BPM before Comment in Song Details and removes BPM altogether if it's empty.
- Omits empty BPM in JSON responses, eliminating need for FunctionField.
- Fixes copy/paste error in ffmpeg_test.
  • Loading branch information
brianschrameck committed May 6, 2021
1 parent fb33aa4 commit 30bb3f7
Show file tree
Hide file tree
Showing 13 changed files with 90 additions and 14 deletions.
30 changes: 30 additions & 0 deletions db/migration/20210430212322_add_bpm_metadata.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package migrations

import (
"database/sql"

"github.com/pressly/goose"
)

func init() {
goose.AddMigration(upAddBpmMetadata, downAddBpmMetadata)
}

func upAddBpmMetadata(tx *sql.Tx) error {
_, err := tx.Exec(`
alter table media_file
add bpm integer;
create index if not exists media_file_bpm
on media_file (bpm);
`)
if err != nil {
return err
}
notice(tx, "A full rescan needs to be performed to import more tags")
return forceFullRescan(tx)
}

func downAddBpmMetadata(tx *sql.Tx) error {
return nil
}
1 change: 1 addition & 0 deletions model/mediafile.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ type MediaFile struct {
Compilation bool `json:"compilation"`
Comment string `json:"comment"`
Lyrics string `json:"lyrics"`
Bpm int `json:"bpm,omitempty"`
CatalogNum string `json:"catalogNum"`
MbzTrackID string `json:"mbzTrackId" orm:"column(mbz_track_id)"`
MbzAlbumID string `json:"mbzAlbumId" orm:"column(mbz_album_id)"`
Expand Down
2 changes: 1 addition & 1 deletion scanner/mapping.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func (s *mediaFileMapper) toMediaFile(md metadata.Metadata) model.MediaFile {
mf.MbzAlbumComment = md.MbzAlbumComment()
mf.Comment = s.policy.Sanitize(md.Comment())
mf.Lyrics = s.policy.Sanitize(md.Lyrics())

mf.Bpm = md.Bpm()
mf.CreatedAt = time.Now()
mf.UpdatedAt = md.ModificationTime()

Expand Down
17 changes: 17 additions & 0 deletions scanner/metadata/ffmpeg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,4 +286,21 @@ Input #0, mp3, from '/Users/deluan/Music/Music/Media/_/Wyclef Jean - From the Hu
Expect(args).To(Equal([]string{"ffmpeg", "-i", "/music library/one.mp3", "-i", "/music library/two.mp3", "-f", "ffmetadata"}))
})

It("parses an integer TBPM tag", func() {
const output = `
Input #0, mp3, from 'tests/fixtures/test.mp3':
Metadata:
TBPM : 123`
md, _ := e.extractMetadata("tests/fixtures/test.mp3", output)
Expect(md.Bpm()).To(Equal(123))
})

It("parses and rounds a floating point fBPM tag", func() {
const output = `
Input #0, ogg, from 'tests/fixtures/test.ogg':
Metadata:
FBPM : 141.7`
md, _ := e.extractMetadata("tests/fixtures/test.ogg", output)
Expect(md.Bpm()).To(Equal(142))
})
})
11 changes: 11 additions & 0 deletions scanner/metadata/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package metadata

import (
"fmt"
"math"
"os"
"path"
"regexp"
Expand Down Expand Up @@ -66,6 +67,7 @@ type Metadata interface {
FilePath() string
Suffix() string
Size() int64
Bpm() int
}

type baseMetadata struct {
Expand Down Expand Up @@ -127,6 +129,15 @@ func (m *baseMetadata) Suffix() string {
func (m *baseMetadata) Duration() float32 { panic("not implemented") }
func (m *baseMetadata) BitRate() int { panic("not implemented") }
func (m *baseMetadata) HasPicture() bool { panic("not implemented") }
func (m *baseMetadata) Bpm() int {
var bpmStr = m.getTag("tbpm", "bpm", "fbpm")
var bpmFloat, err = strconv.ParseFloat(bpmStr, 64)
if err == nil {
return (int)(math.Round(bpmFloat))
} else {
return 0
}
}

func (m *baseMetadata) parseInt(tagName string) int {
if v, ok := m.tags[tagName]; ok {
Expand Down
23 changes: 12 additions & 11 deletions scanner/metadata/taglib_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,20 @@ var _ = Describe("taglibExtractor", func() {
Expect(m.BitRate()).To(Equal(192))
Expect(m.FilePath()).To(Equal("tests/fixtures/test.mp3"))
Expect(m.Suffix()).To(Equal("mp3"))
Expect(m.Size()).To(Equal(int64(60845)))
Expect(m.Size()).To(Equal(int64(51876)))
Expect(m.Comment()).To(Equal("Comment1\nComment2"))
Expect(m.Bpm()).To(Equal(123))

//TODO This file has some weird tags that makes the following tests fail sometimes.
//m = mds["tests/fixtures/test.ogg"]
//Expect(err).To(BeNil())
//Expect(m.Title()).To(BeEmpty())
//Expect(m.HasPicture()).To(BeFalse())
//Expect(m.Duration()).To(Equal(float32(3)))
//Expect(m.BitRate()).To(Equal(10))
//Expect(m.Suffix()).To(Equal("ogg"))
//Expect(m.FilePath()).To(Equal("tests/fixtures/test.ogg"))
//Expect(m.Size()).To(Equal(int64(4408)))
m = mds["tests/fixtures/test.ogg"]
Expect(err).To(BeNil())
Expect(m.Title()).To(BeEmpty())
Expect(m.HasPicture()).To(BeFalse())
Expect(m.Duration()).To(Equal(float32(1)))
Expect(m.BitRate()).To(Equal(39))
Expect(m.Suffix()).To(Equal("ogg"))
Expect(m.FilePath()).To(Equal("tests/fixtures/test.ogg"))
Expect(m.Size()).To(Equal(int64(5065)))
Expect(m.Bpm()).To(Equal(142)) // This file has a floating point BPM set to 141.7 under the fBPM tag. Ensure we parse and round correctly.
})
})
})
Binary file modified tests/fixtures/test.mp3
Binary file not shown.
Binary file modified tests/fixtures/test.ogg
Binary file not shown.
2 changes: 2 additions & 0 deletions ui/src/album/AlbumSongs.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
BulkActionsToolbar,
ListToolbar,
TextField,
NumberField,
useVersion,
useListContext,
} from 'react-admin'
Expand Down Expand Up @@ -128,6 +129,7 @@ const AlbumSongs = (props) => {
{isDesktop && <TextField source="artist" sortable={false} />}
<DurationField source="duration" sortable={false} />
{isDesktop && <QualityInfo source="quality" sortable={false} />}
{isDesktop && <NumberField source="bpm" sortable={false} />}
{isDesktop && config.enableStarRating && (
<RatingField
source="rating"
Expand Down
12 changes: 11 additions & 1 deletion ui/src/common/SongDetails.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,13 @@ import TableBody from '@material-ui/core/TableBody'
import TableCell from '@material-ui/core/TableCell'
import TableContainer from '@material-ui/core/TableContainer'
import TableRow from '@material-ui/core/TableRow'
import { BooleanField, DateField, TextField, useTranslate } from 'react-admin'
import {
BooleanField,
DateField,
TextField,
NumberField,
useTranslate,
} from 'react-admin'
import inflection from 'inflection'
import { BitrateField, SizeField } from './index'
import { MultiLineTextField } from './MultiLineTextField'
Expand All @@ -32,6 +38,7 @@ export const SongDetails = (props) => {
size: <SizeField record={record} source="size" />,
updatedAt: <DateField record={record} source="updatedAt" showTime />,
playCount: <TextField record={record} source="playCount" />,
bpm: <NumberField record={record} source="bpm" />,
comment: <MultiLineTextField record={record} source="comment" />,
}
if (!record.discSubtitle) {
Expand All @@ -40,6 +47,9 @@ export const SongDetails = (props) => {
if (!record.comment) {
delete data.comment
}
if (!record.bpm) {
delete data.bpm
}
if (record.playCount > 0) {
data.playDate = <DateField record={record} source="playDate" showTime />
}
Expand Down
3 changes: 2 additions & 1 deletion ui/src/i18n/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
"starred": "Favourite",
"rating": "Rating",
"comment": "Comment",
"quality": "Quality"
"quality": "Quality",
"bpm": "BPM"
},
"actions": {
"addToQueue": "Play Later",
Expand Down
2 changes: 2 additions & 0 deletions ui/src/playlist/PlaylistSongs.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
BulkActionsToolbar,
ListToolbar,
TextField,
NumberField,
useRefresh,
useDataProvider,
useNotify,
Expand Down Expand Up @@ -166,6 +167,7 @@ const PlaylistSongs = ({ playlistId, readOnly, actions, ...props }) => {
{isDesktop && <TextField source="artist" />}
<DurationField source="duration" className={classes.draggable} />
{isDesktop && <QualityInfo source="quality" sortable={false} />}
{isDesktop && <NumberField source="bpm" />}
<SongContextMenu
onAddToPlaylist={onAddToPlaylist}
showLove={false}
Expand Down
1 change: 1 addition & 0 deletions ui/src/song/SongList.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ const SongList = (props) => {
)}
{isDesktop && <QualityInfo source="quality" sortable={false} />}
<DurationField source="duration" />
{isDesktop && <NumberField source="bpm" />}
{config.enableStarRating && (
<RatingField
source="rating"
Expand Down

0 comments on commit 30bb3f7

Please sign in to comment.