Пример #1
0
def write(filepath, signal, samplerate=44100, bytedepth=2):
    """Write an audio signal to disk.

    Parameters
    ----------
    filepath: str
        Path to an audio file.

    signal : np.ndarray, ndim in [1,2]
        Audio signal to write to disk.

    samplerate: scalar, or None for file's default
        Samplerate for the returned audio signal.

    bytedepth : int, default=2
        Number of bytes for the audio signal; must be 2.
    """
    if bytedepth != 2:
        raise NotImplementedError("Currently only 16-bit audio is supported.")

    tmp_file = util.temp_file(formats.WAVE)
    if formats.WAVE == os.path.splitext(filepath)[-1].strip('.'):
        tmp_file = filepath

    signal = np.asarray(signal)
    signal = signal.reshape(-1, 1) if signal.ndim == 1 else signal

    fp = wave.open(tmp_file, 'w')
    fp.setnchannels(signal.shape[-1])
    fp.setsampwidth(bytedepth)
    fp.setframerate(samplerate)
    fp.writeframes(util.array_to_byte_string(signal, bytedepth))
    fp.close()

    if tmp_file != filepath:
        sox.convert(tmp_file, filepath)
Пример #2
0
 def test_array_to_byte_string_stereo_bytedepthE4(self):
     self.assertEqual(util.array_to_byte_string(self.stereo, 4),
                      self.stereo_str4)
Пример #3
0
 def test_array_to_byte_string_mono_bytedepthE2(self):
     self.assertEqual(util.array_to_byte_string(self.mono, 2),
                      self.mono_str2)