Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from flask import Flask, request, jsonify
- import mido # MIDI processing
- from music21 import converter, instrument, note, stream # Melody playback
- import os
- import wave
- import numpy as np
- app = Flask(__name__)
- @app.route('/submit', methods=['POST'])
- def submit():
- data = request.json
- title = data['title']
- bpm = int(data['bpm'])
- voice_path = data['voicePath']
- melody = data['melody']
- microtones = data['microtones']
- # Handle voice file playback (placeholder for actual voice processing)
- if os.path.exists(voice_path):
- play_voice(voice_path)
- # Parse melody
- melody_notes = parse_melody(melody)
- microtone_notes = parse_microtones(microtones)
- # Play the melody
- play_melody(melody_notes, microtone_notes, bpm)
- return jsonify({"status": "success", "message": "Melody and microtones played"})
- def parse_melody(melody_str):
- """Parse the melody string into notes and durations."""
- melody_list = melody_str.split()
- notes_list = []
- for i in range(0, len(melody_list), 2):
- pitch = melody_list[i]
- duration = melody_list[i+1]
- n = note.Note(pitch)
- n.quarterLength = parse_duration(duration)
- notes_list.append(n)
- return notes_list
- def parse_microtones(microtones_str):
- """Parse microtones into frequency and duration pairs."""
- if not microtones_str:
- return []
- microtones_list = microtones_str.split()
- return [(float(microtones_list[i][:-2]), parse_duration(microtones_list[i+1])) for i in range(0, len(microtones_list), 2)]
- def parse_duration(duration_str):
- """Convert string durations like '1/4' into float values."""
- if '/' in duration_str:
- num, denom = map(int, duration_str.split('/'))
- return num / denom
- return float(duration_str)
- def play_melody(notes, microtones, bpm):
- """Create a stream and play notes and microtones at the specified BPM."""
- s = stream.Stream()
- s.append(instrument.Piano())
- # Add regular notes
- s.append(notes)
- # Add microtones as custom frequencies
- for freq, duration in microtones:
- sine_wave = generate_sine_wave(freq, duration * 60 / bpm)
- play_wave(sine_wave)
- s.metronomeMarkBoundaries(0, bpm)
- s.show('midi') # Plays the melody as a MIDI file
- def generate_sine_wave(freq, duration, sample_rate=44100):
- """Generate a sine wave for a specific frequency and duration."""
- t = np.linspace(0, duration, int(sample_rate * duration), False)
- sine_wave = 0.5 * np.sin(2 * np.pi * freq * t)
- return sine_wave
- def play_wave(data, sample_rate=44100):
- """Play a sine wave by writing to a WAV file and playing it."""
- with wave.open('temp.wav', 'w') as wf:
- wf.setnchannels(1)
- wf.setsampwidth(2)
- wf.setframerate(sample_rate)
- wf.writeframes((data * 32767).astype(np.int16).tobytes())
- os.system("aplay temp.wav") # On Linux; for other platforms, adjust playback command
- def play_voice(voice_path):
- """Play a voice file using an external player or custom playback."""
- os.system(f"aplay {voice_path}") # Modify for other platforms (use a suitable library like Pygame for portability)
- if __name__ == '__main__':
- app.run(debug=True)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement