This repository was archived by the owner on Feb 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 153
[SIGNAL] Changed audio loading backend to soundfile #221
Closed
adrian-stepien
wants to merge
2
commits into
python-acoustics:master
from
adrian-stepien:dev/soundfile
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,7 +2,7 @@ cimport cython | |
| cimport numpy | ||
| import numpy as np | ||
| import matplotlib.pyplot as plt | ||
| from scipy.io import wavfile | ||
| import soundfile as sf | ||
| from scipy.signal import detrend, lfilter, bilinear, spectrogram, filtfilt, resample, fftconvolve | ||
| import acoustics | ||
| import itertools | ||
|
|
@@ -1026,43 +1026,42 @@ class Signal(numpy.ndarray): | |
| By default a 6 decibel gap is used. | ||
|
|
||
| """ | ||
| factor = (self.max() * 10.0**(gap/20.0)) | ||
| factor = (np.abs(self).max() * 10.0**(gap/20.0)) | ||
| if inplace: | ||
| self /= factor | ||
| self /= factor[..., None] | ||
| return self | ||
| else: | ||
| return self / factor | ||
| return self / factor[..., None] | ||
|
|
||
|
|
||
| def to_wav(self, filename, depth=16): | ||
| def to_wav(self, filename, depth=16, format="WAV"): | ||
| """Save signal as WAV file. | ||
|
|
||
| :param filename: Name of file to save to. | ||
| :param depth: If given, convert to integer with specified depth. Else, try to store using the original data type. | ||
| :param format: It can be either WAV or FLAC | ||
|
|
||
| By default, this function saves a normalized 16-bit version of the signal with at least 6 dB range till clipping occurs. | ||
| Note that the floating point samples are not yet supported. | ||
|
|
||
| """ | ||
| data = self | ||
| dtype = data.dtype if not depth else 'int'+str(depth) | ||
| if depth: | ||
| data = (data * 2**(depth-1)-1).astype(dtype) | ||
| wavfile.write(filename, int(self.fs), data.T) | ||
| #wavfile.write(filename, int(self.fs), self._data/np.abs(self._data).max() * 0.5) | ||
| #wavfile.write(filename, int(self.fs), np.int16(self._data/(np.abs(self._data).max()) * 32767) ) | ||
| subtype = "PCM_{}".format(depth) | ||
| to_save = data | ||
| sf.write(filename, to_save.T, int(self.fs), format=format, subtype=subtype) | ||
|
|
||
| @classmethod | ||
| def from_wav(cls, filename): | ||
| def from_wav(cls, filename, normalize=False): | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Separate commit. Don't change the default behavior. I'm not yet sure whether I agree on offering this, because typically the level of a WAV file is irrelevant; one always wants to scale with a reference.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In certain applications, knowing the level simplifies a lot. Currently that information is lost if the file is being saved. An example of that is given in #222. |
||
| """ | ||
| Create an instance of `Signal` from a WAV file. | ||
|
|
||
| :param filename: Filename | ||
| :param normalize: Optionally normalize the input samples | ||
|
|
||
| """ | ||
| fs, data = wavfile.read(filename) | ||
| data = data.astype(np.float32, copy=False).T | ||
| data /= np.max(np.abs(data)) | ||
| return cls(data, fs=fs) | ||
| data, fs = sf.read(filename) | ||
| if normalize: | ||
| data /= np.max(np.abs(data)) | ||
| return cls(data.T, fs=fs) | ||
|
|
||
|
|
||
| _PLOTTING_PARAMS = { | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,3 +5,4 @@ six | |
| cython | ||
| pandas | ||
| tabulate | ||
| pysoundfile | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -25,6 +25,7 @@ install_requires = | |
| cython | ||
| pandas >= 0.15 | ||
| tabulate | ||
| pysoundfile | ||
| tests_require = | ||
| pytest | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.