Skip to main content

Easy access to audio data

Project description

hear

Easy access to audio data.

To install: pip install hear

Examples

A wav serialization/deserialization transformer.

First let's make a very short waveform.

>>> from hear import WavSerializationTrans
>>> from numpy import sin, arange, pi
>>> n_samples = 5; sr = 44100;
>>> wf = sin(arange(n_samples) * 2 * pi * 440 / sr)
>>> wf
array([0.        , 0.06264832, 0.12505052, 0.18696144, 0.24813785])

An instance of WavSerializationTrans will allow you to

>>> trans = WavSerializationTrans(assert_sr=sr)  # if you want to write data you NEED to specify assert_sr
>>> wav_bytes = trans._data_of_obj(wf)
>>> wav_bytes[:44]  # the header bytes
b'RIFF.\x00\x00\x00WAVEfmt \x10\x00\x00\x00\x01\x00\x01\x00D\xac\x00\x00\x88X\x01\x00\x02\x00\x10\x00data\n\x00\x00\x00'
>>> wav_bytes[44:]  # the data bytes (5 * 2 = 10 bytes)
b'\x00\x00\x04\x08\x01\x10\xee\x17\xc2\x1f'

>>> wf_read_from_bytes = trans._obj_of_data(wav_bytes)
>>> wf_read_from_bytes
array([   0, 2052, 4097, 6126, 8130], dtype=int16)

Note that we've serialized floats, but they were deserialized as int16. This is the default behavior, but is cusomizable through dtype, subtype, etc. With this default dtype=int16 setting though, if you serialize int16 arrays, you'll recover them exactly.

>>> assert all(trans._obj_of_data(trans._data_of_obj(wf_read_from_bytes)) == wf_read_from_bytes)

The most common use of WavSerializationTrans through, is to make a class decorator for a store that provides wav bytes.

>>> @WavSerializationTrans.wrapper(assert_sr=sr)
... class MyWavStore(dict):
...     pass
>>> my_wav_store = MyWavStore(just_one=wav_bytes)
>>> my_wav_store['just_one']
array([   0, 2052, 4097, 6126, 8130], dtype=int16)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

hear-0.1.20.tar.gz (21.1 kB view details)

Uploaded Source

File details

Details for the file hear-0.1.20.tar.gz.

File metadata

  • Download URL: hear-0.1.20.tar.gz
  • Upload date:
  • Size: 21.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.8.18

File hashes

Hashes for hear-0.1.20.tar.gz
Algorithm Hash digest
SHA256 27778d520d87c4e4207ad2128f3393ea5c933ff3a0a371d7fc70129147b1edfd
MD5 466200ed5212d2688279698ab758eb24
BLAKE2b-256 43c5d5c8c94332f1a04fd914784e3c04b557ba8facab809ad542a6f126f0104f

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page