def _convertTmp(self, tmpCacheEntry): """ Moves a tmp file to the upload dir, resampling it if necessary, and then deleting the tmp entries. :param tmpCacheEntry: the cache entry. :return: """ from analyser.common.signal import loadSignalFromWav tmpCacheEntry['status'] = 'converting' logger.info("Loading " + tmpCacheEntry['path']) signal = loadSignalFromWav(tmpCacheEntry['path']) logger.info("Loaded " + tmpCacheEntry['path']) if Path(tmpCacheEntry['path']).exists(): logger.info('Deleting ' + tmpCacheEntry['path']) os.remove(tmpCacheEntry['path']) else: logger.warning('Tmp cache file does not exist: ' + tmpCacheEntry['path']) self._tmpCache.remove(tmpCacheEntry) self._conversionCache.append(tmpCacheEntry) srcFs = signal.fs completeSamples = signal.samples outputFileName = os.path.join(self._uploadDir, tmpCacheEntry['name']) if srcFs > 1024: self.writeOutput(outputFileName, completeSamples, srcFs, 1000) else: self.writeOutput(outputFileName, completeSamples, srcFs, srcFs) tmpCacheEntry['status'] = 'loaded' self._conversionCache.remove(tmpCacheEntry) self._uploadCache.append(self._extractMeta(outputFileName, 'loaded'))
def spec(self): measurementPath = os.path.join(os.path.dirname(__file__), '../test/data', 'eot.wav') measurement = ms.loadSignalFromWav(measurementPath) # y_1000 = librosa.resample(measurement.samples, measurement.fs, 1000, res_type='kaiser_fast') # measurement = Signal(y_1000, 1000) f, Pxx = measurement.peakSpectrum(ref=1 / (2**0.5)) print(str(np.max(Pxx))) plt.semilogx(f, Pxx) plt.show()
def test_whenWavIsFullScale_AndNoScalingIsRequired_OutputIsUnchanged(): measurementPath = os.path.join(str(Path(__file__).resolve().parents[2]), 'full_scale_sine.wav') calibrationPath = os.path.join(str(Path(__file__).resolve().parents[2]), 'full_scale_sine.wav') measurement = ms.loadSignalFromWav(measurementPath, calibrationSignalFile=calibrationPath, calibrationRealWorldValue=1) assert measurement is not None assert measurement.fs == 48000 assert np.max(measurement.samples) == pytest.approx(1.0)
def test_whenWavIsHalfScale_AndScalingDownIsRequired_OutputIsCorrect(): measurementPath = os.path.join(str(Path(__file__).resolve().parents[2]), 'half_scale_sine.wav') calibrationPath = os.path.join(str(Path(__file__).resolve().parents[2]), 'half_scale_sine.wav') measurement = ms.loadSignalFromWav(measurementPath, calibrationSignalFile=calibrationPath, calibrationRealWorldValue=0.02) assert measurement is not None assert measurement.fs == 48000 assert np.max(measurement.samples) == pytest.approx(0.02)
def resam(self): measurementPath = os.path.join(os.path.dirname(__file__), '../test/data', 'white.wav') measurement = ms.loadSignalFromWav(measurementPath) y_1000 = librosa.resample(measurement.samples, measurement.fs, 1000, res_type='kaiser_fast') measurementPath_1000 = os.path.join(os.path.dirname(__file__), '../test/data', 'white_1000.wav') maxv = np.iinfo(np.int32).max librosa.output.write_wav(measurementPath_1000, (y_1000 * maxv).astype(np.int32), 1000) # librosa.output.write_wav(measurementPath_1000, y_1000, 1000) measurement_1000 = ms.loadSignalFromWav(measurementPath_1000) y_1000 = Signal(y_1000, 1000) f, Pxx = measurement_1000.spectrum(ref=1.0) plt.semilogx(f, Pxx) f, Pxx = y_1000.spectrum(ref=1.0) plt.semilogx(f, Pxx) plt.show()
def loadSignal(self, name, start=None, end=None): """ Loads the named entry from the upload cache as a signal. :param name: the name. :param start: the time to start from in HH:mm:ss.SSS format :param end: the time to end at in HH:mm:ss.SSS format. :return: the signal if the named upload exists. """ entry = self._getCacheEntry(name) if entry is not None: from analyser.common.signal import loadSignalFromWav return loadSignalFromWav(entry['path'], start=start, end=end) else: return None
def showSpectrum(self): # measurementPath = 'C:\\Users\\\Matt\\OneDrive\\Documents\\eot\\Edge of Tomorrow - Opening.wav' # measurementPath = os.path.join(os.path.dirname(__file__), '../test/data', 'eot.wav') # measurementPath = os.path.join(os.path.dirname(__file__), '../test/data', 'white_0_50_1.wav') measurementPath = os.path.join(os.path.dirname(__file__), '../test/data', 'PinkNoise_10_50_1.wav') measurement1 = ms.loadSignalFromWav(measurementPath) # measurement1 = ms.loadSignalFromWav('C:\\Users\\Matt\\.vibe\\upload\\The Admiral Roaring Currents.wav') # measurement2 = ms.loadSignalFromWav('C:\\Users\\Matt\\.vibe\\upload\\How to Train Your Dragon - Dragon Crash.wav') plt.xlim(1, 100) plt.ylim(-60, 0) plt.grid() plt.xlabel('frequency [Hz]') f, Pxx_spec = measurement1.peakSpectrum(ref=1.0) plt.semilogx(f, Pxx_spec) # f, Pxx_spec = measurement2.spectrum(ref=1.0) # plt.semilogx(f, Pxx_spec) plt.show()
def showSpectro(self): # measurementPath = 'C:\\Users\\\Matt\\OneDrive\\Documents\\eot\\Edge of Tomorrow - Opening.wav' measurementPath = os.path.join(os.path.dirname(__file__), 'data', 'eot.wav') measurement = ms.loadSignalFromWav(measurementPath) # t, f, Sxx_spec = measurement.spectrogram() # plt.pcolormesh(f, t, Sxx_spec) # plt.ylim(0, 160) cmap = plt.get_cmap('viridis') cmap.set_under(color='k', alpha=None) plt.specgram(measurement.samples, NFFT=measurement.getSegmentLength(), Fs=measurement.fs, detrend=mlab.detrend_none, mode='magnitude', noverlap=measurement.getSegmentLength() / 2, window=mlab.window_hanning, vmin=-60, cmap=plt.cm.gist_heat) plt.ylim(0, 100) plt.show()