Skip to main content

Bring melodies to life with pure Python. Input note strings, hear real sound.

Project description

Buy Me a Coffee


himig

himig is a simple, pure-Python music synthesis and playback package. It lets you generate, play, and save melodies as WAV files using a human-friendly note and duration format. It’s perfect for educational projects, demos, and creative coding.


Features

  • 🎵 Compose melodies using note names and durations (e.g., "C4:0.5")
  • 🔊 Play melodies directly on your system’s audio output
  • 💾 Save melodies as standard WAV files
  • 🧑‍💻 Generate in-memory WAV bytes for web apps (e.g., Streamlit)
  • 🎼 Sample built-in melodies: Happy Birthday, Twinkle Twinkle.
  • 🪶 Lightweight: Only depends on numpy

Installation

pip install himig

Or, for development:

git clone https://github.com/j-ncel/himig.git
cd himig

Quick Start

Play a Melody

from himig import play

melody = ["C4:0.5", "C4:0.5", "G4:1.0"]
play(melody)

Save a Melody as WAV

from himig import save

melody = ["C4:0.5", "C4:0.5", "G4:1.0"]
save(melody, "happy.wav")

Use Built-in Melodies

from himig import play, happy_birthday, twinkle_twinkle

play(happy_birthday)
play(twinkle_twinkle)

Use in Streamlit or Web Apps

import streamlit as st
from himig import generate_wav_bytes, happy_birthday

wav_bytes = generate_wav_bytes(happy_birthday)
st.audio(wav_bytes, format="audio/wav")

Melody Format

  • Each melody is a list of strings: "NOTE:DURATION"
    • NOTE: Note name (e.g., C4, F#5, Bb3, or R for rest)
    • DURATION: Length in seconds (float or int)
  • Example: ["C4:0.5", "G4:1.0", "R:0.25"]

Built-in Melodies

  • happy_birthday
  • twinkle_twinkle

You can import them directly:

from himig import happy_birthday, twinkle_twinkle

You can contribute more melodies to the project, it is welcome!


API Reference

play(melody, sample_rate=44100, amplitude=32767)

Play a melody through your system’s audio output.

  • melody: List of note-duration strings
  • sample_rate: Audio sample rate (Hz)
  • amplitude: Peak amplitude

save(melody, filename, sample_rate=44100, amplitude=32767)

Save a melody as a WAV file.

  • melody: List of note-duration strings
  • filename: Output WAV file path

generate_wav_bytes(melody, sample_rate=44100, amplitude=32767)

Generate a WAV file as an in-memory bytes buffer (for web apps).

  • Returns: io.BytesIO object

Adding Your Own Melodies

Just create a list of note-duration strings:

my_song = [
    "C4:0.5", "D4:0.5", "E4:1.0", "R:0.25", "E4:0.5", "D4:0.5", "C4:1.0"
]
play(my_song)

Project Structure

himig/
│
├── himig/
│   ├── __init__.py
│   ├── core.py
│   ├── audio_player.py
│   ├── constants.py
│   └── melodies.py
│
├── tests/
│   └── test_himig.py
├── README.md
├── pyproject.toml
└── LICENSE

Links

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

himig-1.0.1.tar.gz (6.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

himig-1.0.1-py3-none-any.whl (6.7 kB view details)

Uploaded Python 3

File details

Details for the file himig-1.0.1.tar.gz.

File metadata

  • Download URL: himig-1.0.1.tar.gz
  • Upload date:
  • Size: 6.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.2

File hashes

Hashes for himig-1.0.1.tar.gz
Algorithm Hash digest
SHA256 4a7143e05baeda1c41b1d05adabe45d0245f8b707baaed81a76dc820a57f5ceb
MD5 261c5d122615a52e26fc2ac418a29c27
BLAKE2b-256 0f3c048f8cf7ffb579b961f0201d6ab110fb7540bce0dac4c66a78fab6340d3e

See more details on using hashes here.

File details

Details for the file himig-1.0.1-py3-none-any.whl.

File metadata

  • Download URL: himig-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 6.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.2

File hashes

Hashes for himig-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 db89be5421451c4c66202a73388c235182a1f9d6aeef59abd85860ea91e64cdb
MD5 e9bb43c88d7c4997a04d6df4ae0307af
BLAKE2b-256 06f1d147766a1d27ed05bec210d4949ce674c00c06d7dfafa7987cac6ecec7e3

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