Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

standardise program/sox.py formatting, add test case, docstring #53

Merged
merged 6 commits into from
Oct 21, 2016
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ install:
- sudo pip install musicbrainzngs pycdio

# Testing dependencies
- sudo apt-get install -qq gstreamer0.10-tools python-gst0.10
- sudo apt-get install -qq gstreamer0.10-tools python-gst0.10 sox
- sudo pip install twisted

# Build bundled C utils
Expand Down
28 changes: 18 additions & 10 deletions morituri/program/sox.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,24 @@
import os
import logging
import os
from subprocess import Popen, PIPE

SOX = 'sox'

def peak_level(track_path):
if not os.path.exists(track_path):
logging.warning("SoX peak detection failed: file not found")
return None
sox = Popen([SOX, track_path, "-n", "stat"], stderr=PIPE)
out, err = sox.communicate()
if sox.returncode:
logging.warning("SoX peak detection failed: " + s.returncode)
return None
return float(err.split('\n')[3].split()[2]) # Maximum amplitude: 0.123456
"""
Accepts a path to a sox-decodable audio file.

Returns track peak level from sox ('maximum amplitude') as a float.
Returns None on error.
"""
if not os.path.exists(track_path):
logging.warning("SoX peak detection failed: file not found")
return None
sox = Popen([SOX, track_path, "-n", "stat"], stderr=PIPE)
out, err = sox.communicate()
if sox.returncode:
logging.warning("SoX peak detection failed: " + str(sox.returncode))
return None
# relevant captured line looks like:
# Maximum amplitude: 0.123456
return float(err.split('\n')[3].split()[2])
13 changes: 13 additions & 0 deletions morituri/test/test_program_sox.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# -*- Mode: Python; test-case-name: morituri.test.test_program_sox -*-

import os

from morituri.program import sox
from morituri.test import common

class PeakLevelTestCase(common.TestCase):
def setUp(self):
self.path = os.path.join(os.path.dirname(__file__), 'track.flac')

def testParse(self):
self.assertEquals(0.800018, sox.peak_level(self.path))