挑戰
準確性:語音到 MIDI 的轉換並不總是 100% 準確。可能會對音高或節奏產生誤解,尤其是在複雜的聲音輸入時。
延遲:根據您的設置,您唱歌和產生 MIDI 音符之間可能會有輕微的延遲(延遲)。
音調範圍:某些軟體可能難以偵測非常高或非常低的音調。
獲得更好結果的技巧
唱歌或哼唱時使用清晰、獨特的音高。
如果您要轉換單音符旋律,請在單音模式下工作以避免音符之間的混淆。
透過設定 MIDI 轉換,將人聲映射到特定的鼓聲,Beatbox 演奏者或打擊樂演奏者可以使用他們的聲音來觸發鼓聲。
離蒄 - 2024/10/14
voice-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]