Add replaygain support.

This commit is contained in:
RecursiveGreen 2019-07-03 11:57:46 -04:00
parent b30003847d
commit 813531be26
6 changed files with 80 additions and 7 deletions

View file

@ -26,7 +26,14 @@ HEADERS = {
'Authorization': 'Token {}'.format(DJ_TOKEN) 'Authorization': 'Token {}'.format(DJ_TOKEN)
} }
ANNOTATE = 'annotate:req_id="{}",type="{}",artist="{}",title="{}",game="{}":{}' ANNOTATE = (
'annotate:req_id="{}",'
'type="{}",'
'artist="{}",'
'title="{}",'
'game="{}",'
'replay_gain="{}":{}'
)
logging.basicConfig( logging.basicConfig(
handlers=[ handlers=[
@ -93,11 +100,12 @@ def next_request():
title = clean_quotes(song['title']) title = clean_quotes(song['title'])
game = clean_quotes(song['game']) game = clean_quotes(song['game'])
LOGGER.info( LOGGER.info(
'Req_ID: %s, Artist[s]: %s, Title: %s, Game: %s, Path: %s', 'ID: %s, Artist[s]: %s, Title: %s, Game: %s, Gain: %s, Path: %s',
song_request['id'], song_request['id'],
artist, artist,
title, title,
game, game,
song['replaygain'],
song['path'] song['path']
) )
annotate_string = ANNOTATE.format( annotate_string = ANNOTATE.format(
@ -106,6 +114,7 @@ def next_request():
artist, artist,
title, title,
game, game,
song['replaygain'],
song['path'] song['path']
) )
LOGGER.debug(annotate_string) LOGGER.debug(annotate_string)

View file

@ -1,4 +1,4 @@
from rest_framework.serializers import (BooleanField, DecimalField, from rest_framework.serializers import (BooleanField, CharField, DecimalField,
IntegerField, ListField, IntegerField, ListField,
ModelSerializer, Serializer, ModelSerializer, Serializer,
SerializerMethodField, SerializerMethodField,
@ -120,12 +120,13 @@ class RadioSongSerializer(ModelSerializer):
decimal_places=2, decimal_places=2,
source='active_store.length' source='active_store.length'
) )
replaygain = CharField(source='active_store.replaygain')
path = SerializerMethodField() path = SerializerMethodField()
class Meta: class Meta:
model = Song model = Song
fields = ('album', 'artists', 'game', 'song_type', 'title', 'length', fields = ('album', 'artists', 'game', 'song_type', 'title', 'length',
'path') 'replaygain', 'path')
def get_path(self, obj): def get_path(self, obj):
'''Converts the IRI into a filesystem path.''' '''Converts the IRI into a filesystem path.'''

View file

@ -114,14 +114,20 @@ class StoreAdmin(admin.ModelAdmin):
list_display = ('iri', list_display = ('iri',
'mime_type', 'mime_type',
'file_size', 'file_size',
'length') 'length',
'_replaygain')
search_fields = ['iri'] search_fields = ['iri']
# Edit Form display # Edit Form display
readonly_fields = ('created_date', 'modified_date') readonly_fields = ('created_date', 'modified_date')
fieldsets = ( fieldsets = (
('Main', { ('Main', {
'fields': ('iri', 'mime_type', 'file_size', 'length') 'fields': ('iri',
'mime_type',
'file_size',
'length',
'track_gain',
'track_peak')
}), }),
('Stats', { ('Stats', {
'classes': ('collapse',), 'classes': ('collapse',),

View file

@ -101,11 +101,24 @@ class Command(BaseCommand):
else: else:
iri = song['store']['path'] iri = song['store']['path']
if song['store']['track_gain']:
gain_str = re.sub(r'[dB\+ ]', '', song['store']['track_gain'])
gain = decimal.Decimal(gain_str)
else:
gain = None
if song['store']['track_peak']:
peak = decimal.Decimal(song['store']['track_peak'])
else:
peak = None
new_store = Store.objects.create( new_store = Store.objects.create(
iri=iri, iri=iri,
mime_type=song['store']['mime'], mime_type=song['store']['mime'],
file_size=song['store']['filesize'], file_size=song['store']['filesize'],
length=song['store']['length'] length=song['store']['length'],
track_gain=gain,
track_peak=peak
) )
new_song.stores.add(new_store) new_song.stores.add(new_store)
new_song.active_store = new_store new_song.active_store = new_store

View file

@ -0,0 +1,23 @@
# Generated by Django 2.2.2 on 2019-07-03 13:16
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('radio', '0004_new_song_path_structure'),
]
operations = [
migrations.AddField(
model_name='store',
name='track_gain',
field=models.DecimalField(blank=True, decimal_places=2, max_digits=6, null=True, verbose_name='recommended replaygain adjustment'),
),
migrations.AddField(
model_name='store',
name='track_peak',
field=models.DecimalField(blank=True, decimal_places=6, max_digits=10, null=True, verbose_name='highest volume level in the track'),
),
]

View file

@ -117,6 +117,27 @@ class Store(Timestampable, models.Model):
decimal_places=2, decimal_places=2,
null=True, null=True,
blank=True) blank=True)
track_gain = models.DecimalField(_('recommended replaygain adjustment'),
max_digits=6,
decimal_places=2,
null=True,
blank=True)
track_peak = models.DecimalField(_('highest volume level in the track'),
max_digits=10,
decimal_places=6,
null=True,
blank=True)
def _replaygain(self):
'''
String representation of the recommended amplitude adjustment.
'''
if self.track_gain is None:
return '+0.00 dB'
if self.track_gain > 0:
return '+{} dB'.format(str(self.track_gain))
return '{} dB'.format(str(self.track_gain))
replaygain = property(_replaygain)
def __str__(self): def __str__(self):
return self.iri return self.iri