# 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.
```pydocstring
>>> 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
```pydocstring
>>> 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.
```pydocstring
>>> 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.
```pydocstring
>>> @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)
```
Raw data
{
"_id": null,
"home_page": "https://github.com/otosense/hear",
"name": "hear",
"maintainer": "",
"docs_url": null,
"requires_python": "",
"maintainer_email": "",
"keywords": "",
"author": "OtoSense",
"author_email": "",
"download_url": "https://files.pythonhosted.org/packages/43/c5/d5c8c94332f1a04fd914784e3c04b557ba8facab809ad542a6f126f0104f/hear-0.1.20.tar.gz",
"platform": "any",
"description": "# hear\n\nEasy access to audio data.\n\nTo install:\t```pip install hear```\n\n\n# Examples\n\n\nA wav serialization/deserialization transformer.\n\nFirst let's make a very short waveform.\n\n```pydocstring\n>>> from hear import WavSerializationTrans\n>>> from numpy import sin, arange, pi\n>>> n_samples = 5; sr = 44100;\n>>> wf = sin(arange(n_samples) * 2 * pi * 440 / sr)\n>>> wf\narray([0. , 0.06264832, 0.12505052, 0.18696144, 0.24813785])\n```\n\nAn instance of ``WavSerializationTrans`` will allow you to\n\n```pydocstring\n>>> trans = WavSerializationTrans(assert_sr=sr) # if you want to write data you NEED to specify assert_sr\n>>> wav_bytes = trans._data_of_obj(wf)\n>>> wav_bytes[:44] # the header bytes\nb'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'\n>>> wav_bytes[44:] # the data bytes (5 * 2 = 10 bytes)\nb'\\x00\\x00\\x04\\x08\\x01\\x10\\xee\\x17\\xc2\\x1f'\n\n>>> wf_read_from_bytes = trans._obj_of_data(wav_bytes)\n>>> wf_read_from_bytes\narray([ 0, 2052, 4097, 6126, 8130], dtype=int16)\n```\n\n\nNote that we've serialized floats, but they were deserialized as int16.\nThis is the default behavior, but is cusomizable through dtype, subtype, etc.\nWith this default dtype=int16 setting though, if you serialize int16 arrays, you'll recover them exactly.\n\n```pydocstring\n>>> assert all(trans._obj_of_data(trans._data_of_obj(wf_read_from_bytes)) == wf_read_from_bytes)\n```\n\nThe most common use of WavSerializationTrans through, is to make a class decorator for a store that\nprovides wav bytes.\n\n```pydocstring\n>>> @WavSerializationTrans.wrapper(assert_sr=sr)\n... class MyWavStore(dict):\n... pass\n>>> my_wav_store = MyWavStore(just_one=wav_bytes)\n>>> my_wav_store['just_one']\narray([ 0, 2052, 4097, 6126, 8130], dtype=int16)\n```",
"bugtrack_url": null,
"license": "apache-2.0",
"summary": "Easy access to audio data",
"version": "0.1.20",
"project_urls": {
"Homepage": "https://github.com/otosense/hear"
},
"split_keywords": [],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "43c5d5c8c94332f1a04fd914784e3c04b557ba8facab809ad542a6f126f0104f",
"md5": "466200ed5212d2688279698ab758eb24",
"sha256": "27778d520d87c4e4207ad2128f3393ea5c933ff3a0a371d7fc70129147b1edfd"
},
"downloads": -1,
"filename": "hear-0.1.20.tar.gz",
"has_sig": false,
"md5_digest": "466200ed5212d2688279698ab758eb24",
"packagetype": "sdist",
"python_version": "source",
"requires_python": null,
"size": 21079,
"upload_time": "2024-02-08T15:51:16",
"upload_time_iso_8601": "2024-02-08T15:51:16.707277Z",
"url": "https://files.pythonhosted.org/packages/43/c5/d5c8c94332f1a04fd914784e3c04b557ba8facab809ad542a6f126f0104f/hear-0.1.20.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-02-08 15:51:16",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "otosense",
"github_project": "hear",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"lcname": "hear"
}