Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

mp3 reader passes test

  • Loading branch information...
commit 2b6ca0f22ddf24a246a9c7b3bc3fbb7eb7dcfd5d 1 parent d3a3056
@andreasjansson authored
Showing with 40 additions and 11 deletions.
  1. BIN  sine_440.mp3
  2. +20 −0 test_mp3reader.py
  3. +20 −11 util.py
View
BIN  sine_440.mp3
Binary file not shown
View
20 test_mp3reader.py
@@ -0,0 +1,20 @@
+from util import *
+from evaluate import Evaluator
+from scipy import fft
+import matplotlib.pylab as pylab
+import unittest
+
+class TestMp3Reader(unittest.TestCase):
+
+ def test_read(self):
+ reader = Mp3Reader()
+ samp_rate, signal = reader.read("sine_440.mp3")
+ signal = signal[0:samp_rate / 2] # 500ms
+ spectrum = abs(fft(signal))[0:len(signal) / 2]
+ peak = np.where(spectrum == max(spectrum))[0]
+ peak_freq = peak * samp_rate / len(signal)
+ self.assertGreater(442, peak_freq)
+ self.assertLess(438, peak_freq)
+
+if __name__ == '__main__':
+ unittest.main()
View
31 util.py
@@ -1,9 +1,8 @@
-from subprocess import call
import tempfile
import scipy.io.wavfile as wavfile
import scipy.signal as signal
+import numpy as np
import os
-from itertools import repeat
class Key:
def __init__(self, key, time):
@@ -22,19 +21,29 @@ def execute(self):
class Mp3Reader:
-
- def read_mono(self, mp3_filename):
- wav_filename = tempfile.NamedTemporaryFile('w+b', -1, '.wav', 'tmp', None, False)
+
+ def read(self, mp3_filename):
+ """
+ Returns (sampling_rate, data), where the sampling rate is the
+ original sampling rate, downsampled by a factor of 4, and
+ the data signal is a downsampled, mono (left channel) version
+ of the original signal.
+ """
+ wav_filename = tempfile.NamedTemporaryFile(suffix = '.wav', delete = False).name
self._mp3_to_wav(mp3_filename, wav_filename)
samp_rate, stereo = wavfile.read(wav_filename)
os.unlink(wav_filename)
- mono = stereo[:,0] + stereo[:,1]
+ mono = stereo[:,0]
# pad with zeroes before downsampling
- mono = mono + repeat(0, 4 - (len(mono) % 4))
+ mono = np.concatenate((mono, [0] * (4 - (len(mono) % 4))))
# downsample
- mono = signal.resample(mono, len(mono) / 4)
- return (samp_rate / 4, mono)
+ downsample_factor = 4
+ mono = signal.resample(mono, len(mono) / downsample_factor)
+ return (samp_rate / downsample_factor, mono)
def _mp3_to_wav(self, mp3_filename, wav_filename):
- # TODO: UPNEXT
- pass
+ if not os.path.exists(mp3_filename):
+ raise IOError('File not found')
+ os.system("mpg123 -w " + wav_filename + " " + mp3_filename + " &> /dev/null")
+ if not os.path.exists(wav_filename):
+ raise IOError('Failed to create wav file')
Please sign in to comment.
Something went wrong with that request. Please try again.