sintetizador_fm_para_archivos_midi_en_python

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
sintetizador_fm_para_archivos_midi_en_python [2025/01/13 16:16] ososintetizador_fm_para_archivos_midi_en_python [2025/01/13 21:25] (current) – [Overview] oso
Line 10: Line 10:
  
 <code python:fm_synthesizer.py> <code python:fm_synthesizer.py>
- 
-<code python> 
 import mido import mido
 import numpy as np import numpy as np
Line 254: Line 252:
  
  
-===== Appendix: Monophonic Synthesizer =====  +====== Appendix: Monophonic Synthesizer ======  
  
 This section covers a **simple mono synthesizer** that processes a single piano track from a MIDI file, synthesizing FM-based audio for each note and playing it in real time.   This section covers a **simple mono synthesizer** that processes a single piano track from a MIDI file, synthesizing FM-based audio for each note and playing it in real time.  
  
 <code python>   <code python>  
-def read_midi(file_path):   +import mido  # For MIDI parsing 
-    midi = mido.MidiFile(file_path)   +import numpy as np  # For waveform synthesis 
-    return midi  +import sounddevice as sd  # For real-time audio playback
  
-def fm_synth(note, velocity, duration, sample_rate=44100):   +def read_midi(file_path): 
-    carrier_freq = 440 * 2**((note - 69) / 12)   +    midi = mido.MidiFile(file_path) 
-    modulator_freq = carrier_freq * 2   +    return midi 
-    modulation_index = 0.33  + 
 +def fm_synth(note, velocity, duration, sample_rate=44100): 
 +    # Example parameters for FM synthesis 
 +    carrier_freq = 440 * 2**((note - 69) / 12)  # MIDI note to frequency 
 +    modulator_freq = carrier_freq * 2 + carrier_freq * 4 + carrier_freq * 6 + carrier_freq * 8 
 +    modulation_index = 0.33
          
-    t = np.linspace(0, duration, int(sample_rate * duration), endpoint=False)   +    t = np.linspace(0, duration, int(sample_rate * duration), endpoint=False) 
-    modulator = np.sin(2 * np.pi * modulator_freq * t) * modulation_index   +    modulator = np.sin(2 * np.pi * modulator_freq * t) * modulation_index 
-    carrier = np.sin(2 * np.pi * carrier_freq * t + modulator)  +    carrier = np.sin(2 * np.pi * carrier_freq * t + modulator)
          
-    return carrier * velocity / 127  +    return carrier * velocity / 127 
 + 
 +def process_piano_track(midi): 
 +    sample_rate = 44100 
 +    for track in midi.tracks: 
 +        for msg in track: 
 +            if msg.type == 'note_on' and msg.velocity > 0:  # Note on 
 +                duration = msg.time / midi.ticks_per_beat  # Estimate duration from MIDI timing 
 +                note_audio = fm_synth(msg.note, msg.velocity, duration, sample_rate) 
 +                 
 +                # Play the synthesized audio for the note 
 +                sd.play(note_audio, samplerate=sample_rate) 
 +                sd.wait()  # Wait until the audio is finished playing
  
-def process_piano_track(midi):   +midi = read_midi("C:/temp/canyon.mid"
-    sample_rate 44100   +process_piano_track(midi)
-    for track in midi.tracks  +
-        for msg in track:   +
-            if msg.type == 'note_on' and msg.velocity > 0:   +
-                duration = msg.time midi.ticks_per_beat   +
-                note_audio = fm_synth(msg.note, msg.velocity, duration, sample_rate  +
-                sd.play(note_audio, samplerate=sample_rate)   +
-                sd.wait()  +
 </code>   </code>  
  
 ===== Overview =====   ===== Overview =====  
  
-**Purpose:** To demonstrate a basic FM synthesis-based mono synthesizer that processes MIDI piano tracks. Each note is synthesized and played one at a time.   +  * **Purpose:** To demonstrate a basic FM synthesis-based mono synthesizer that processes MIDI piano tracks. Each note is synthesized and played one at a time.   
-**Limitations:**   +  **Limitations:**   
-  - **Mono Only:** The synthesizer processes and plays a single note at a time. Overlapping notes or chords are not supported.   +    - **Mono Only:** The synthesizer processes and plays a single note at a time. Overlapping notes or chords are not supported.   
-  - **No Envelope Control:** The FM synthesis does not include dynamic ADSR envelopes, which might result in abrupt starts/stops.   +    - **No Envelope Control:** The FM synthesis does not include dynamic ADSR envelopes, which might result in abrupt starts/stops.   
-  - **Simplistic Waveform Modulation:** Only sine waves are used as the carrier and modulator.  +    - **Simplistic Waveform Modulation:** Only sine waves are used as the carrier and modulator.  
  
 ===== Customizing the Modulation Signal =====   ===== Customizing the Modulation Signal =====  
sintetizador_fm_para_archivos_midi_en_python.1736784972.txt.gz · Last modified: 2025/01/13 16:16 by oso