# sonification
Map data to sound allowing it to be interpreted it in an auditory manner
To install: ```pip install sonification```
# Examples
```python
import numpy as np
import pandas as pd
from sklearn.preprocessing import MinMaxScaler, LabelEncoder
import simpleaudio as sa
def preprocess_dataframe(df):
df = df.copy()
# Normalize numerical columns
scaler = MinMaxScaler()
for column in df.select_dtypes(include=np.number).columns:
df[column] = scaler.fit_transform(df[[column]])
# Ensure durations are not zero
if 'duration' in df.columns:
df['duration'] = df['duration'] + 0.01 # Adding a small value to ensure durations are not zero
# Encode categorical columns
label_encoders = {}
for column in df.select_dtypes(include='object').columns:
le = LabelEncoder()
df[column] = le.fit_transform(df[column])
label_encoders[column] = le
return df, label_encoders
def generate_tone(frequency, duration, volume, sample_rate=44100):
t = np.linspace(0, duration, int(sample_rate * duration), False)
wave = volume * np.sin(frequency * t * 2 * np.pi)
return wave
def map_features_to_audio(df, pitch_col, duration_col, volume_col, sample_rate=44100):
waveform = np.array([])
for index, row in df.iterrows():
pitch = row[pitch_col]
duration = row[duration_col]
volume = row[volume_col]
frequency = 440 + pitch * 440 # Example: Map pitch to frequency
wave = generate_tone(frequency, duration, volume, sample_rate)
# print(f"Row {index}: pitch={pitch}, duration={duration}, volume={volume}, frequency={frequency}, wave_len={len(wave)}")
waveform = np.concatenate([waveform, wave])
print(f"Final waveform length: {len(waveform)}")
return waveform, sample_rate
def save_or_return_audio(waveform, sample_rate, filepath=None):
if filepath:
# Normalize waveform to int16 range
waveform_int16 = np.int16(waveform / np.max(np.abs(waveform)) * 32767)
sa.WaveObject(waveform_int16, 1, 2, sample_rate).save(filepath)
else:
return waveform, sample_rate
def sonification_dataframe(df, pitch_col, duration_col, volume_col, sample_rate=44100, filepath=None):
df, label_encoders = preprocess_dataframe(df)
waveform, sr = map_features_to_audio(df, pitch_col, duration_col, volume_col, sample_rate)
return save_or_return_audio(waveform, sr, filepath)
# Example usage:
df = pd.DataFrame({
'pitch': [0.2, 0.4, 0.6, 0.8],
'duration': [0.5, 0.5, 0.5, 0.5],
'volume': [0.5, 0.7, 0.9, 1.0]
})
waveform, sr = sonification_dataframe(df, 'pitch', 'duration', 'volume')
# To play the audio
if waveform is not None and len(waveform) > 0:
play_obj = sa.play_buffer(np.int16(waveform / np.max(np.abs(waveform)) * 32767), 1, 2, sr)
play_obj.wait_done()
```
Raw data
{
"_id": null,
"home_page": "https://github.com/thorwhalen/sonification",
"name": "sonification",
"maintainer": null,
"docs_url": null,
"requires_python": null,
"maintainer_email": null,
"keywords": null,
"author": "Thor Whalen",
"author_email": null,
"download_url": "https://files.pythonhosted.org/packages/cd/dd/383a6294fbc1631c101cb426008fe210fbfdb2c5e6dd3fd3cb0093816838/sonification-0.0.9.tar.gz",
"platform": "any",
"description": "\n# sonification\n Map data to sound allowing it to be interpreted it in an auditory manner\n\n\nTo install:\t```pip install sonification```\n\n# Examples\n\n```python\nimport numpy as np\nimport pandas as pd\nfrom sklearn.preprocessing import MinMaxScaler, LabelEncoder\nimport simpleaudio as sa\n\ndef preprocess_dataframe(df):\n df = df.copy()\n # Normalize numerical columns\n scaler = MinMaxScaler()\n for column in df.select_dtypes(include=np.number).columns:\n df[column] = scaler.fit_transform(df[[column]])\n \n # Ensure durations are not zero\n if 'duration' in df.columns:\n df['duration'] = df['duration'] + 0.01 # Adding a small value to ensure durations are not zero\n \n # Encode categorical columns\n label_encoders = {}\n for column in df.select_dtypes(include='object').columns:\n le = LabelEncoder()\n df[column] = le.fit_transform(df[column])\n label_encoders[column] = le\n \n return df, label_encoders\n\ndef generate_tone(frequency, duration, volume, sample_rate=44100):\n t = np.linspace(0, duration, int(sample_rate * duration), False)\n wave = volume * np.sin(frequency * t * 2 * np.pi)\n return wave\n\ndef map_features_to_audio(df, pitch_col, duration_col, volume_col, sample_rate=44100):\n waveform = np.array([])\n \n for index, row in df.iterrows():\n pitch = row[pitch_col]\n duration = row[duration_col]\n volume = row[volume_col]\n \n frequency = 440 + pitch * 440 # Example: Map pitch to frequency\n wave = generate_tone(frequency, duration, volume, sample_rate)\n \n # print(f\"Row {index}: pitch={pitch}, duration={duration}, volume={volume}, frequency={frequency}, wave_len={len(wave)}\")\n \n waveform = np.concatenate([waveform, wave])\n \n print(f\"Final waveform length: {len(waveform)}\")\n return waveform, sample_rate\n\ndef save_or_return_audio(waveform, sample_rate, filepath=None):\n if filepath:\n # Normalize waveform to int16 range\n waveform_int16 = np.int16(waveform / np.max(np.abs(waveform)) * 32767)\n sa.WaveObject(waveform_int16, 1, 2, sample_rate).save(filepath)\n else:\n return waveform, sample_rate\n\ndef sonification_dataframe(df, pitch_col, duration_col, volume_col, sample_rate=44100, filepath=None):\n df, label_encoders = preprocess_dataframe(df)\n waveform, sr = map_features_to_audio(df, pitch_col, duration_col, volume_col, sample_rate)\n return save_or_return_audio(waveform, sr, filepath)\n\n# Example usage:\ndf = pd.DataFrame({\n 'pitch': [0.2, 0.4, 0.6, 0.8],\n 'duration': [0.5, 0.5, 0.5, 0.5],\n 'volume': [0.5, 0.7, 0.9, 1.0]\n})\nwaveform, sr = sonification_dataframe(df, 'pitch', 'duration', 'volume')\n\n# To play the audio\nif waveform is not None and len(waveform) > 0:\n play_obj = sa.play_buffer(np.int16(waveform / np.max(np.abs(waveform)) * 32767), 1, 2, sr)\n play_obj.wait_done()\n```\n",
"bugtrack_url": null,
"license": "mit",
"summary": "Map data to sound allowing it to be interpreted it in an auditory manner",
"version": "0.0.9",
"project_urls": {
"Homepage": "https://github.com/thorwhalen/sonification"
},
"split_keywords": [],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "d37708a7cb836bc781c00b19ca9edb53a9e7d916c0c170aac8204409eaa2a7f1",
"md5": "986dcb3cc361969c9a739be760e969dd",
"sha256": "e06844d5fecdf9bbd6fa452892b79dc5ae55d5345d3e350b316e6048b5f946dd"
},
"downloads": -1,
"filename": "sonification-0.0.9-py3-none-any.whl",
"has_sig": false,
"md5_digest": "986dcb3cc361969c9a739be760e969dd",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": null,
"size": 14154,
"upload_time": "2024-08-30T08:48:39",
"upload_time_iso_8601": "2024-08-30T08:48:39.481773Z",
"url": "https://files.pythonhosted.org/packages/d3/77/08a7cb836bc781c00b19ca9edb53a9e7d916c0c170aac8204409eaa2a7f1/sonification-0.0.9-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "cddd383a6294fbc1631c101cb426008fe210fbfdb2c5e6dd3fd3cb0093816838",
"md5": "de449a405b717ba887c1356f2d6374c1",
"sha256": "f289ec7a3818e3f0ff1f0b82baaf5e9c73b5d9dc13b525660deeab2e97d9bcf8"
},
"downloads": -1,
"filename": "sonification-0.0.9.tar.gz",
"has_sig": false,
"md5_digest": "de449a405b717ba887c1356f2d6374c1",
"packagetype": "sdist",
"python_version": "source",
"requires_python": null,
"size": 11106,
"upload_time": "2024-08-30T08:48:40",
"upload_time_iso_8601": "2024-08-30T08:48:40.715987Z",
"url": "https://files.pythonhosted.org/packages/cd/dd/383a6294fbc1631c101cb426008fe210fbfdb2c5e6dd3fd3cb0093816838/sonification-0.0.9.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-08-30 08:48:40",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "thorwhalen",
"github_project": "sonification",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"lcname": "sonification"
}