Skip to main content

Map data to sound allowing it to be interpreted it in an auditory manner

Project description

sonify

Map data to sound allowing it to be interpreted it in an auditory manner

To install: pip install sonify

Examples

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 sonify_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 = sonify_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()

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

sonification-0.0.8.tar.gz (7.4 kB view details)

Uploaded Source

Built Distribution

sonification-0.0.8-py3-none-any.whl (8.7 kB view details)

Uploaded Python 3

File details

Details for the file sonification-0.0.8.tar.gz.

File metadata

  • Download URL: sonification-0.0.8.tar.gz
  • Upload date:
  • Size: 7.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.10.13

File hashes

Hashes for sonification-0.0.8.tar.gz
Algorithm Hash digest
SHA256 81d63c01e48b359a21a941ce0cc7df1570853d87ec67b6acecb7e807635db30d
MD5 de9d641ca0e3bbba4c02606766c81a25
BLAKE2b-256 e17d5eb349ae0911f9fe749e58c375fd08fa4e1ed84e437d8e3b8c50e53f681e

See more details on using hashes here.

File details

Details for the file sonification-0.0.8-py3-none-any.whl.

File metadata

  • Download URL: sonification-0.0.8-py3-none-any.whl
  • Upload date:
  • Size: 8.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.10.13

File hashes

Hashes for sonification-0.0.8-py3-none-any.whl
Algorithm Hash digest
SHA256 6d2c222a28ccc446ffcd035c62ab6e18561d0019444862a2f6e886969e9e6b7f
MD5 a73cd674c8e136081eef2bdb596e94d1
BLAKE2b-256 bed91c4d90980bb6e05a132281c4ef95a530d41da0036fe8dd67ba9702711fc0

See more details on using hashes here.

Supported by

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