Show HN: A small, simple music theory library in C99
Source: Hacker News
mahler.c
Simple library for Western music theory in C99.
Repository:
Tests:
Documentation:
Clang format file:
Features
- Small & easy‑to‑use
- Interval, chord, scale, and key‑signature functions
- No internal memory allocation
- Supports theoretical keys (e.g., Fb+)
- No accidental limit (e.g., G 20th sharp)
- Enharmonically correct (e.g., minor 6th of D is Bb, not A#)
- 100 % test coverage
Example
/* Create the C4 Blues Scale, ascending */
struct mah_note notes[7];
struct mah_scale scale = mah_get_scale(
(struct mah_note){ MAH_C, MAH_NATURAL, 4 },
&MAH_BLUES_SCALE,
notes,
MAH_ASCEND,
NULL
);
/* Print the scale */
char buf[MAH_DISP_LEN];
for (int i = 0; i
Who’s Mahler?
Gustav Mahler is one of my favourite composers. If you enjoy the emotions of Wagner and the ideas of Stravinsky, Mahler offers a perfect middle ground. Check out his Symphony No. 5 in C♯ minor, The Song of the Earth, and Symphony No. 6 in A minor.
Documentation
See the full documentation here:
Unit Tests
The test suite is available at:
Compiling
You can build the library with CMake. Be sure to add the src and inc directories to the include path.
cmake_minimum_required(VERSION 3.10)
project(example)
set(MAHLER_PATH "${PROJECT_SOURCE_DIR}/mahler.c")
add_executable(${PROJECT_NAME} ${PROJECT_NAME}.c)
target_include_directories(${PROJECT_NAME} PUBLIC
"${PROJECT_BINARY_DIR}"
"${MAHLER_PATH}/inc"
"${MAHLER_PATH}/src"
)
add_subdirectory(${MAHLER_PATH})
target_link_libraries(${PROJECT_NAME} PUBLIC mahler)
Alternatively, you can compile directly from the command line by including the source and header directories.