挑戦
精度: 音声から MIDI への変換は、必ずしも 100% 正確であるとは限りません。特に複雑な音声入力の場合、ピッチやリズムの誤解が発生する可能性があります。
Delay: 設定によっては、歌ってから MIDI ノートが生成されるまでにわずかな遅れ(遅延)が生じる場合があります。
ピッチ範囲: 一部のソフトウェアでは、非常に高い音または非常に低い音を検出するのが難しい場合があります。
より良い結果のためのヒント
歌ったりハミングしたりするときは、明確で独特な音程を使用してください。
単音のメロディーを変換する場合は、音間の混乱を避けるためにモノフォニック モードで作業してください。
ビートボクサーやパーカッショニストは、ボーカルを特定のドラム サウンドにマッピングする MIDI トランジションを設定することで、自分の声を使用してドラム サウンドをトリガーできます。
麗江 - 2024/10/14voice-to-MIDI converter using Python
1. **Capture Audio Input**: We'll use a microphone to record the voice input.
2. **Analyze Audio**: The audio needs to be analyzed for its frequency (pitch detection).
3. **Convert Frequency to MIDI Notes**: The detected frequency can be mapped to MIDI notes.
4. **Create MIDI File**: After converting the frequencies to MIDI notes, we can save them in a `.mid` file.
We'll need some Python libraries to accomplish this:
- `sounddevice`: For recording the audio.
- `numpy` and `scipy`: For working with and analyzing audio signals.
- `mido`: To create MIDI files.
- `aubio`: For pitch detection (aubio specializes in audio-to-note detection).
### Step-by-Step Process
#### Step 1: Install the Required Libraries
First, we need to install the necessary Python libraries:
```bash
pip install sounddevice numpy scipy mido python-rtmidi aubio
```
#### Step 2: Capture Audio Input
Here’s how you can capture audio input using `sounddevice`:
```python
import sounddevice as sd
import numpy as np
def record_audio(duration, sample_rate=44100):
print("Recording...")
audio = sd.rec(int(duration * sample_rate), samplerate=sample_rate, channels=1, dtype='float32')
sd.wait() # Wait until recording is finished
print("Recording finished.")
return audio.flatten()
```
This function records audio from the microphone for a specified duration.
#### Step 3: Analyze Audio for Pitch (Using `aubio`)
Now, let's detect the pitch of the recorded audio using the `aubio` library:
```python
import aubio
def detect_pitch(audio, sample_rate=44100):
pitch_detector = aubio.pitch("default", 2048, 512, sample_rate)
pitch_detector.set_unit("Hz")
pitch_detector.set_silence(-40) # Adjust silence threshold if needed
pitches = []
for i in range(0, len(audio), 512):
sample = audio[i:i + 512]
pitch = pitch_detector(sample)[0]
if pitch > 0: # Only consider non-zero pitch values
pitches.append(pitch)
return pitches
```
This function processes the audio and detects the pitch at regular intervals.
#### Step 4: Convert Pitch to MIDI Notes
MIDI notes correspond to specific frequencies. We can map detected frequencies to their nearest MIDI note numbers:
```python
def frequency_to_midi(frequency):
if frequency <= 0:
return None
midi_note = 69 + 12 * np.log2(frequency / 440.0) # A4 = 440Hz corresponds to MIDI note 69
return round(midi_note)
```
#### Step 5: Create a MIDI File
We can now convert the detected MIDI notes into a MIDI file using the `mido` library:
```python
from mido import MidiFile, MidiTrack, Message
def create_midi_from_pitches(pitches):
midi_file = MidiFile()
track = MidiTrack()
midi_file.tracks.append(track)
# Add some basic track information
track.append(Message('program_change', program=12, time=0))
# Convert each pitch to a MIDI note and add to the track
for pitch in pitches:
midi_note = frequency_to_midi(pitch)
if midi_note is not None:
track.append(Message('note_on', note=midi_note, velocity=64, time=100))
track.append(Message('note_off', note=midi_note, velocity=64, time=200))
return midi_file
```
This function takes the detected pitches, converts them to MIDI notes, and creates a MIDI file. The `note_on` and `note_off` events are added to simulate playing and releasing the note.
#### Step 6: Putting It All Together
Now, we can record audio, detect the pitch, convert it to MIDI notes, and save it as a `.mid` file:
```python
def voice_to_midi(duration):
# Step 1: Record audio
audio = record_audio(duration)
# Step 2: Detect pitch from the recorded audio
pitches = detect_pitch(audio)
# Step 3: Create a MIDI file from the detected pitches
midi_file = create_midi_from_pitches(pitches)
# Step 4: Save the MIDI file
midi_file.save('output.mid')
print("MIDI file saved as 'output.mid'")
# Record and convert voice to MIDI for 5 seconds
voice_to_midi(5)
```
### Explanation:
- **Step 1**: Records audio from your microphone for the given duration.
- **Step 2**: The recorded audio is split into frames, and each frame is analyzed for its pitch.
- **Step 3**: The detected pitch is converted to a MIDI note.
- **Step 4**: A MIDI file is created with `note_on` and `note_off` events for each detected note.
### Notes:
- The code is designed for monophonic melodies (i.e., one note at a time). For polyphonic melodies (multiple notes at once), more advanced techniques are required.
- **Noise sensitivity**: Ensure that the environment is relatively quiet, as background noise can interfere with pitch detection.
This script should give you a basic starting point for voice-to-MIDI conversion in Python.
離蒄-柔音美景@Youtube
. email: [email protected]