1. Introduction

The CR-Counter  is an advanced music-generation engine designed to accelerate compositional workflows for both Western counterpoint and Carnatic raga-based music.

Unlike traditional tools, CR-Counter v2 provides:

  • Strict pitch safety in both tonal and raga contexts.
  • Configurable counterpoint species (Western).
  • Raga-fugal imitation engine (Carnatic).
  • Realistic audio preview with SoundFonts (SF2/SF3).
  • Seamless MusicXML and MIDI export for professional notation and DAW use.

This blog dives into the mathematics, algorithms, user guide, and test cases powering the system.


2. System Architecture

Pipeline Overview

  1. UI Layer (Streamlit) – Key, Raga, Style, Instruments, and Species selection.
  2. Generator Engine
    • Western Mode → Counterpoint Species (1–5, Classical).
    • Raga Mode → Raga-safe degree imitation.
  3. Voice-leading Optimizer → Avoid parallels, accented dissonance.
  4. Export Layer → MusicXML, MIDI.
  5. Preview Renderer → Sine synth (fallback) or SoundFont-based instruments.

3. Music Theory Foundations

3.1 Western Counterpoint

The system implements simplified species counterpoint:

  • Species I (Note-against-note): 1:1 consonances.
  • Species II (Two notes vs. one): stepwise passing tones.
  • Species III (Four notes vs. one): stepwise motion, alternating consonances.
  • Species IV (Suspensions): prepared → dissonance → resolution.
  • Species V (Mixed/Classical): free counterpoint with rules.

Consonance Function:

3.2 Carnatic Raga Logic

Each raga is represented as a set of scale degrees (relative semitones from tonic):

Example: Hindolam → {0, 3, 5, 8, 11}

Imitation Mapping:
Instead of intervallic transposition (Western fugue), the engine uses degree-based mapping within the raga graph:

This ensures all voices remain strictly inside the raga.


4. Mathematical Core


5. Technology Stack

  • Python Core
  • Streamlit → UI and deployment.
  • music21 → Score parsing, MusicXML handling.
  • Mido → MIDI export.
  • FluidSynth / pyFluidSynth → Real-instrument SoundFont rendering.
  • NumPy → Efficient audio synthesis.

6. User Guide

Step 1: Installation

pip install streamlit music21 mido pyFluidSynth numpy

brew install fluidsynth # (macOS)

apt-get install fluidsynth # (Linux)

Step 2: Run App

streamlit run cr_counter_app_viz_full.py

Step 3: Configure Input

  • Key: C, G, D, etc.
  • Mode: Major / Minor (auto-disabled in Raga mode).
  • Raga: Choose Hindolam, Kalyani, etc.
  • Arrangement Style: Pop, Rock, Classical, Jazz…
  • Species (Western only): 1–5 or Classical.
  • Instruments: Lead, Counter, Bass, Pad.
  • Bars & Tempo: Length of piece.

Step 4: Export

  • MusicXML → Open in MuseScore, Sibelius, Finale.
  • MIDI → Import into DAW (Logic, Ableton, FL).

Step 5: Preview

  • Upload a SoundFont (SF2/SF3) for realism.
  • If none, fallback = sine tones.

7. Test Cases

 Pitch Safety Test

  • Generate in Hindolam.
  • Verify no Pa (Perfect 5th) appears.

 Counterpoint Consonance Test

  • Species I, C major.
  • All intervals consonant.

 Cadence Test

  • Last measure resolves to tonic.

 Export Integrity Test

  • Load MusicXML in MuseScore: Clefs, Key Signature, Time Signature correct.

 Audio Rendering Test

  • With MuseScore_General.sf3 → preview matches instrument patches.

8. Suggested Visuals

  • MusicXML Example: Open in MuseScore.
  • Waveform Snippet: Audio preview visualization.

9. Best Practices

  • Use SoundFonts for realism (FluidR3_GM.sf2, MuseScore_General.sf3).
  • In Raga mode, select instruments with natural sustain (flute, strings).
  • For Western species, use short phrases (8–16 bars) to check contrapuntal flow.
  • Always export MIDI + XML → refine in DAW or notation software.

10. Roadmap (v3 Ideas)

  • Strict Counterpoint Checker: detect hidden/direct 5ths, dissonances on beats.
  • Raga Grammar Engine: JSON-based pakad + aro/ava transitions.
  • Gamaka Modeling: MIDI pitch bends for ornaments.
  • Tāla Awareness: integrate cycle-based phrasing.

11. Conclusion

The CR-Counter  engine bridges Western classical rigor and Carnatic raga creativity. With mathematical rigor, musicological awareness, and production-ready exports, it offers composers a powerful assistant for ideation, sketching, and full-score preparation.

✍️ Written by CR SRIKANTH
(Composer, Developer, Researcher)