BPM metadata enhancement (#1087)

* 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.
This commit is contained in:
Brian Schrameck 2021-05-05 21:35:01 -04:00 committed by GitHub
parent fb33aa4496
commit 30bb3f7b43
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 90 additions and 14 deletions

View File

@ -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
}

View File

@ -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)"`

View File

@ -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()

View File

@ -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))
})
})

View File

@ -2,6 +2,7 @@ package metadata
import (
"fmt"
"math"
"os"
"path"
"regexp"
@ -66,6 +67,7 @@ type Metadata interface {
FilePath() string
Suffix() string
Size() int64
Bpm() int
}
type baseMetadata struct {
@ -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 {

View File

@ -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 not shown.

Binary file not shown.

View File

@ -3,6 +3,7 @@ import {
BulkActionsToolbar,
ListToolbar,
TextField,
NumberField,
useVersion,
useListContext,
} from 'react-admin'
@ -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"

View File

@ -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'
@ -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) {
@ -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 />
}

View File

@ -22,7 +22,8 @@
"starred": "Favourite",
"rating": "Rating",
"comment": "Comment",
"quality": "Quality"
"quality": "Quality",
"bpm": "BPM"
},
"actions": {
"addToQueue": "Play Later",

View File

@ -3,6 +3,7 @@ import {
BulkActionsToolbar,
ListToolbar,
TextField,
NumberField,
useRefresh,
useDataProvider,
useNotify,
@ -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}

View File

@ -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"