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)
}
ANNOTATE = 'annotate:req_id="{}",type="{}",artist="{}",title="{}",game="{}":{}'
ANNOTATE = (
'annotate:req_id="{}",'
'type="{}",'
'artist="{}",'
'title="{}",'
'game="{}",'
'replay_gain="{}":{}'
)
logging.basicConfig(
handlers=[
@ -93,11 +100,12 @@ def next_request():
title = clean_quotes(song['title'])
game = clean_quotes(song['game'])
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'],
artist,
title,
game,
song['replaygain'],
song['path']
)
annotate_string = ANNOTATE.format(
@ -106,6 +114,7 @@ def next_request():
artist,
title,
game,
song['replaygain'],
song['path']
)
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,
ModelSerializer, Serializer,
SerializerMethodField,
@ -120,12 +120,13 @@ class RadioSongSerializer(ModelSerializer):
decimal_places=2,
source='active_store.length'
)
replaygain = CharField(source='active_store.replaygain')
path = SerializerMethodField()
class Meta:
model = Song
fields = ('album', 'artists', 'game', 'song_type', 'title', 'length',
'path')
'replaygain', 'path')
def get_path(self, obj):
'''Converts the IRI into a filesystem path.'''

View file

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

View file

@ -101,11 +101,24 @@ class Command(BaseCommand):
else:
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(
iri=iri,
mime_type=song['store']['mime'],
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.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,
null=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):
return self.iri