[PYTHON] A story about creating a web application that automatically generates Minecraft sound block performances

What is {Title}?

https://mc-note-functioner.herokuapp.com/ "Data pack that allows you to play music in Micra with a single command" It is a web application to make.

What sets it apart from note block studio

Since it can be managed for each tick, tuplet that cannot be expressed by normal Minecraft can be reproduced. Since it is a data pack, it can be easily executed. Since it is a data pack, it can be easily used for BGM if it is managed by command blocks. You can edit midi files in note block studio, If you want to edit midi, I think most of the time you use the dedicated software.

Parse Midi files

I used pretty-midi from Python.

import pretty_midi
import numpy as np
PITCH = {0:0.66, 1:0.70, 2:0.74, 3:0.78, 4:0.84, 5:0.89, 6:0.5, 7:0.53, 8:0.56, 9:0.59, 10:0.62, 11:0.66, 12:0.7, 13:0.74, 14:0.8, 15:0.84, 16:0.9, 17:0.95, 18:1, 19:1.05, 20:1.13, 21:1.18, 22:1.25, 23:1.33, 24:1.4, 25:1.5, 26:1.57, 27:1.67, 28:1.8, 29:1.9, 30:3, 31:1.05, 32:1.13, 33:1.18, 34:1.25, 35:1.33}

file = input("MIDI FILE PATH:")

midi_data = pretty_midi.PrettyMIDI(file)

second = midi_data.get_end_time()
print( "midi file size:", second, "seconds")
print( "bpm:", midi_data.estimate_tempo())

tp = []

with open("./note/data/note/functions/ontick.mcfunction", "w") as f:
	for instrument in midi_data.instruments:
		if not instrument.is_drum:
			time = instrument.get_onsets()
			i = 0
			for note in instrument.notes:
				tp.append([time[i], pretty_midi.note_number_to_name(note.pitch)])
				f.write(f"execute at @a run execute if score Timer SECOND matches {int(time[i]*20)} run playsound minecraft:block.note_block.harp master @a ~ ~ ~ 1 {PITCH[int(note.pitch) % 36]}\n")

				i += 1

web framework

I used Flask.

@app.route('/', methods=['GET'])
def index():
	return '''
	<head>
		<link rel=”icon” href=“./favicon.ico”>
	</head>
	<title>MC Note Functioner</title>
	<form method="post" action="/upload" enctype="multipart/form-data">
	  <input type="file" name="file">
	  <button>upload</button>
	</form>
'''

@app.route('/upload', methods=['POST'])
def upload(): 
	if 'file' not in flask.request.files:
		return 'File not specified'
	fs = flask.request.files['file']
	fs.save("./midi_file.mid")

Deploy

Deployed to Heroku.

result

▼ Click to jump to youtube ▼ IMAGE ALT TEXT HERE

Recommended Posts

A story about creating a web application that automatically generates Minecraft sound block performances
The story of developing a web application that automatically generates catchphrases [MeCab]
Creating a web application using Flask ②
Creating a voice transcription web application
Creating a web application using Flask ①
Creating a web application using Flask ③
Creating a web application using Flask ④
A story about writing a program that automatically summarizes your own asset transitions
The story of making a web application that records extensive reading with Django
Create a web application that recognizes numbers with a neural network
A story about creating a UNIX / Linux compatible OS from scratch
A story about a GCP beginner building a Minecraft server on GCE
A story about everything from data collection to AI development and Web application release in Python (3. AI development)