voluxaudio


Namevoluxaudio JSON
Version 0.9.0 PyPI version JSON
download
home_pagehttps://gitlab.com/volux/voluxaudio
SummaryAudio kit for Volux
upload_time2023-03-19 11:58:29
maintainer
docs_urlNone
authorDrTexx
requires_python>=3.8.1,<4.0.0
licenseAGPLv3
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # Volux Audio <!-- omit in toc -->

[![PyPI](https://img.shields.io/pypi/v/voluxaudio?logo=python)](https://pypi.org/project/voluxaudio)
[![PyPI - Downloads](https://img.shields.io/pypi/dm/voluxaudio?logo=Python)](https://pypi.org/project/voluxaudio)
[![PyPI - License](https://img.shields.io/pypi/l/voluxaudio?color=orange&logo=Python)](https://pypi.org/project/voluxaudio)

---

☠️ **EXPERIMENTAL:** - Beware all ye who enter here ☠️

---

## Table of Contents <!-- omit in toc -->

- [Installation](#installation)
- [Usage](#usage)
  - [`VoluxAudioStream` - Source](#voluxaudiostream---source)
    - [Non-buffered Example](#non-buffered-example)
    - [Buffered Example](#buffered-example)
    - [Audio-visualization Example](#audio-visualization-example)
- [Todo List](#todo-list)
- [Links](#links)


## Installation 

```bash
pip install voluxaudio
```

## Usage

### `VoluxAudioStream` - Source

#### Non-buffered Example

This Python code uses the `voluxaudio` library to open a new audio stream and calls the `on_data` function every time it gets a new chunk of audio data. The `on_data` function simply prints a message indicating that a new chunk of audio data has been received, along with the current timestamp.

The `CHUNK_SIZE` variable defines the size of each chunk of audio data in bytes, and the `CHANNEL_COUNT` variable defines the number of audio channels.

The code creates a `VoluxAudioStream` object with the `on_data` function, `chunk_size`, and `channels` as arguments. It then enters an infinite loop that keeps the audio stream open and calls `sleep(1)` to wait for one second before checking for new audio data again.

```python
from voluxaudio import VoluxAudioStream
from time import sleep, perf_counter

CHUNK_SIZE = 1024
CHANNEL_COUNT = 2

def on_data(
    in_data, # audio data from this chunk
    frame_count, # number of samples chunk
    time_info,
    status_flags,
    buffer_read_dtype, # buffer's datatype
    channel_count, # number of channels
    sample_rate, # number of samples collected per second
):
    """Do this every time we get a new chunk of data."""
    print(f"got a new chunk of audio data! (seconds: {perf_counter()})")

with VoluxAudioStream(
    on_data=on_data,
    chunk_size=CHUNK_SIZE,
    channels=CHANNEL_COUNT
) as audio_stream:
    # keep stream open until script closed
    while True:
        sleep(1)
```

#### Buffered Example

This Python code continuously prints the current buffer of audio data being read from an audio stream at a specified rate.

```python
from voluxaudio import VoluxAudioStream
from time import sleep

CHUNK_SIZE=1024
CHANNEL_COUNT=2
BUFFER_SIZE_IN_SECONDS = 1
BUFFER_READS_PER_SECOND = 60

with VoluxAudioStream(
    chunk_size=CHUNK_SIZE,
    channels=CHANNEL_COUNT,
    buffer_size_in_seconds=BUFFER_SIZE_IN_SECONDS
) as audio_stream:
    while True:
        print(f"buffer: {audio_stream.buffer}")
        sleep(1/BUFFER_READS_PER_SECOND)
```

#### Audio-visualization Example

This Python code opens an audio stream using the `voluxaudio` library with buffering enabled. It defines a function `on_data()` that does nothing and is called every time new samples are gathered. Within the `while` loop, it calculates the weakly approximated amplitude of the audio stream by taking the last 2048 samples of the left and right channels and averaging their absolute values. It then prints a bar representing the amplitude by printing a number followed by a horizontal bar, where the length of the bar is proportional to the amplitude. The script waits for approximately 50 milliseconds before repeating the loop.

This audio stream comes from your system's default recording device, which could either be a microphone or your computer/desktop audio.

![Voluxaudio Amplitude Example](assets/voluxaudio-amplitude-example.gif)

```python
from voluxaudio import VoluxAudioStream
import numpy as np
from time import sleep

# call this every time new samples gathered
def on_data(*args, **kwargs):
    return

# open an audio stream with buffering enabled
with VoluxAudioStream(
    on_data=on_data,
    chunk_size=2048,
    channels=2,
    buffer_size_in_seconds=2,
) as audio_stream:

    # repeat until script stopped
    while True:

        # weakly approximate amplitude
        samples = audio_stream.buffer
        sample_count = len(samples)
        samples_per_channel = np.swapaxes(samples, 0, 1)
        L_channel_e = np.average(np.abs(samples_per_channel[0][-2048:])) / sample_count
        R_channel_e = np.average(np.abs(samples_per_channel[1][-2048:])) / sample_count
        e = (L_channel_e + R_channel_e) / 2

        # print bar
        print(f"{e:<3.3f} " + int(e*100) * '|')

        # wait ~50ms
        sleep(1 / 20)
```

- This is a simple example, but more complex audio processing is possible.
- A bar is printed every 50ms, changing length based on the approximated amplitude.
- The system volume may affect results, to test this dramatically increase/decrease your system volume to see if the bar length is affected. This applies to computer/desktop audio, not microphones.
- Buffering is used as `buffer_size_in_seconds` is specified.

## Todo List

- [x] Get simple `VoluxAudioStream` class working
- [x] Make `VoluxAudioStream` class highly configurable
- [x] Add basic example to documentation
- [x] Add basic documentation
- [x] Add more examples to documentation
- [x] Add links
- [ ] Add more detailed documentation

## Links

<!-- TODO: add website link -->
- 📖 &nbsp;[Documentation](https://gitlab.com/volux/voluxaudio)
- 🐍 &nbsp;[Latest Release](https://pypi.org/project/voluxaudio)
- 🧰 &nbsp;[Source Code](https://gitlab.com/volux/voluxaudio)
- 🐞 &nbsp;[Issue Tracker](https://gitlab.com/volux/voluxaudio/-/issues)
- `🐦 Twitter` &nbsp;[@DrTexx](https://twitter.com/DrTexx)
- `📨 Email` &nbsp;[denver.opensource@tutanota.com](mailto:denver.opensource@tutanota.com)


            

Raw data

            {
    "_id": null,
    "home_page": "https://gitlab.com/volux/voluxaudio",
    "name": "voluxaudio",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.8.1,<4.0.0",
    "maintainer_email": "",
    "keywords": "",
    "author": "DrTexx",
    "author_email": "denver.opensource@tutanota.com",
    "download_url": "https://files.pythonhosted.org/packages/33/20/1af84af1e209746ba17ebfa80b41abff37ff5658d19d15e4f32ec43a45e1/voluxaudio-0.9.0.tar.gz",
    "platform": null,
    "description": "# Volux Audio <!-- omit in toc -->\n\n[![PyPI](https://img.shields.io/pypi/v/voluxaudio?logo=python)](https://pypi.org/project/voluxaudio)\n[![PyPI - Downloads](https://img.shields.io/pypi/dm/voluxaudio?logo=Python)](https://pypi.org/project/voluxaudio)\n[![PyPI - License](https://img.shields.io/pypi/l/voluxaudio?color=orange&logo=Python)](https://pypi.org/project/voluxaudio)\n\n---\n\n\u2620\ufe0f **EXPERIMENTAL:** - Beware all ye who enter here \u2620\ufe0f\n\n---\n\n## Table of Contents <!-- omit in toc -->\n\n- [Installation](#installation)\n- [Usage](#usage)\n  - [`VoluxAudioStream` - Source](#voluxaudiostream---source)\n    - [Non-buffered Example](#non-buffered-example)\n    - [Buffered Example](#buffered-example)\n    - [Audio-visualization Example](#audio-visualization-example)\n- [Todo List](#todo-list)\n- [Links](#links)\n\n\n## Installation \n\n```bash\npip install voluxaudio\n```\n\n## Usage\n\n### `VoluxAudioStream` - Source\n\n#### Non-buffered Example\n\nThis Python code uses the `voluxaudio` library to open a new audio stream and calls the `on_data` function every time it gets a new chunk of audio data. The `on_data` function simply prints a message indicating that a new chunk of audio data has been received, along with the current timestamp.\n\nThe `CHUNK_SIZE` variable defines the size of each chunk of audio data in bytes, and the `CHANNEL_COUNT` variable defines the number of audio channels.\n\nThe code creates a `VoluxAudioStream` object with the `on_data` function, `chunk_size`, and `channels` as arguments. It then enters an infinite loop that keeps the audio stream open and calls `sleep(1)` to wait for one second before checking for new audio data again.\n\n```python\nfrom voluxaudio import VoluxAudioStream\nfrom time import sleep, perf_counter\n\nCHUNK_SIZE = 1024\nCHANNEL_COUNT = 2\n\ndef on_data(\n    in_data, # audio data from this chunk\n    frame_count, # number of samples chunk\n    time_info,\n    status_flags,\n    buffer_read_dtype, # buffer's datatype\n    channel_count, # number of channels\n    sample_rate, # number of samples collected per second\n):\n    \"\"\"Do this every time we get a new chunk of data.\"\"\"\n    print(f\"got a new chunk of audio data! (seconds: {perf_counter()})\")\n\nwith VoluxAudioStream(\n    on_data=on_data,\n    chunk_size=CHUNK_SIZE,\n    channels=CHANNEL_COUNT\n) as audio_stream:\n    # keep stream open until script closed\n    while True:\n        sleep(1)\n```\n\n#### Buffered Example\n\nThis Python code continuously prints the current buffer of audio data being read from an audio stream at a specified rate.\n\n```python\nfrom voluxaudio import VoluxAudioStream\nfrom time import sleep\n\nCHUNK_SIZE=1024\nCHANNEL_COUNT=2\nBUFFER_SIZE_IN_SECONDS = 1\nBUFFER_READS_PER_SECOND = 60\n\nwith VoluxAudioStream(\n    chunk_size=CHUNK_SIZE,\n    channels=CHANNEL_COUNT,\n    buffer_size_in_seconds=BUFFER_SIZE_IN_SECONDS\n) as audio_stream:\n    while True:\n        print(f\"buffer: {audio_stream.buffer}\")\n        sleep(1/BUFFER_READS_PER_SECOND)\n```\n\n#### Audio-visualization Example\n\nThis Python code opens an audio stream using the `voluxaudio` library with buffering enabled. It defines a function `on_data()` that does nothing and is called every time new samples are gathered. Within the `while` loop, it calculates the weakly approximated amplitude of the audio stream by taking the last 2048 samples of the left and right channels and averaging their absolute values. It then prints a bar representing the amplitude by printing a number followed by a horizontal bar, where the length of the bar is proportional to the amplitude. The script waits for approximately 50 milliseconds before repeating the loop.\n\nThis audio stream comes from your system's default recording device, which could either be a microphone or your computer/desktop audio.\n\n![Voluxaudio Amplitude Example](assets/voluxaudio-amplitude-example.gif)\n\n```python\nfrom voluxaudio import VoluxAudioStream\nimport numpy as np\nfrom time import sleep\n\n# call this every time new samples gathered\ndef on_data(*args, **kwargs):\n    return\n\n# open an audio stream with buffering enabled\nwith VoluxAudioStream(\n    on_data=on_data,\n    chunk_size=2048,\n    channels=2,\n    buffer_size_in_seconds=2,\n) as audio_stream:\n\n    # repeat until script stopped\n    while True:\n\n        # weakly approximate amplitude\n        samples = audio_stream.buffer\n        sample_count = len(samples)\n        samples_per_channel = np.swapaxes(samples, 0, 1)\n        L_channel_e = np.average(np.abs(samples_per_channel[0][-2048:])) / sample_count\n        R_channel_e = np.average(np.abs(samples_per_channel[1][-2048:])) / sample_count\n        e = (L_channel_e + R_channel_e) / 2\n\n        # print bar\n        print(f\"{e:<3.3f} \" + int(e*100) * '|')\n\n        # wait ~50ms\n        sleep(1 / 20)\n```\n\n- This is a simple example, but more complex audio processing is possible.\n- A bar is printed every 50ms, changing length based on the approximated amplitude.\n- The system volume may affect results, to test this dramatically increase/decrease your system volume to see if the bar length is affected. This applies to computer/desktop audio, not microphones.\n- Buffering is used as `buffer_size_in_seconds` is specified.\n\n## Todo List\n\n- [x] Get simple `VoluxAudioStream` class working\n- [x] Make `VoluxAudioStream` class highly configurable\n- [x] Add basic example to documentation\n- [x] Add basic documentation\n- [x] Add more examples to documentation\n- [x] Add links\n- [ ] Add more detailed documentation\n\n## Links\n\n<!-- TODO: add website link -->\n- \ud83d\udcd6 &nbsp;[Documentation](https://gitlab.com/volux/voluxaudio)\n- \ud83d\udc0d &nbsp;[Latest Release](https://pypi.org/project/voluxaudio)\n- \ud83e\uddf0 &nbsp;[Source Code](https://gitlab.com/volux/voluxaudio)\n- \ud83d\udc1e &nbsp;[Issue Tracker](https://gitlab.com/volux/voluxaudio/-/issues)\n- `\ud83d\udc26 Twitter` &nbsp;[@DrTexx](https://twitter.com/DrTexx)\n- `\ud83d\udce8 Email` &nbsp;[denver.opensource@tutanota.com](mailto:denver.opensource@tutanota.com)\n\n",
    "bugtrack_url": null,
    "license": "AGPLv3",
    "summary": "Audio kit for Volux",
    "version": "0.9.0",
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "1350654fb11f329db5ccd4761fa58cc74165cc9d175206c5639e4f3418a7ddb4",
                "md5": "7657c97da21bef746ba6f19db46b7569",
                "sha256": "e2f0b470d002047af9b38232e27ba03807d28133afcf3153995bee9225368227"
            },
            "downloads": -1,
            "filename": "voluxaudio-0.9.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "7657c97da21bef746ba6f19db46b7569",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.8.1,<4.0.0",
            "size": 6491,
            "upload_time": "2023-03-19T11:58:26",
            "upload_time_iso_8601": "2023-03-19T11:58:26.952629Z",
            "url": "https://files.pythonhosted.org/packages/13/50/654fb11f329db5ccd4761fa58cc74165cc9d175206c5639e4f3418a7ddb4/voluxaudio-0.9.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "33201af84af1e209746ba17ebfa80b41abff37ff5658d19d15e4f32ec43a45e1",
                "md5": "758e8f69ef55169d41cad676aacb5730",
                "sha256": "8fc1be441bcacb4fddf2fd0f436b1980abcb885e8f0ffd6ae6de5500d23a307a"
            },
            "downloads": -1,
            "filename": "voluxaudio-0.9.0.tar.gz",
            "has_sig": false,
            "md5_digest": "758e8f69ef55169d41cad676aacb5730",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.8.1,<4.0.0",
            "size": 5871,
            "upload_time": "2023-03-19T11:58:29",
            "upload_time_iso_8601": "2023-03-19T11:58:29.705858Z",
            "url": "https://files.pythonhosted.org/packages/33/20/1af84af1e209746ba17ebfa80b41abff37ff5658d19d15e4f32ec43a45e1/voluxaudio-0.9.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-03-19 11:58:29",
    "github": false,
    "gitlab": true,
    "bitbucket": false,
    "gitlab_user": "volux",
    "gitlab_project": "voluxaudio",
    "lcname": "voluxaudio"
}
        
Elapsed time: 0.16137s