1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

raspberry pi 3 でstretch その15

Last updated at Posted at 2017-11-18

#概要
raspberry pi 3 でstretchやってみた。
pythonでmidi再生やってみた。

#環境
Raspberry Pi 3 model B v1.2 element14
2017-09-07-raspbian-stretch

#インストール
sudo aptitude install python-pyaudio
sudo pip install softsynth

#サンプルコード

import sys
import argparse
from synth.synthesizer import Synthesizer
from synth.wave_writer import WaveWriter
from synth.options import Options
from synth import stream
from synth.instruments.OvertoneInstrument import OvertoneInstrument
from synth.note_envelopes import MidiTrackNoteEnvelope

def profile_call():
	opts = Options()
	instr = OvertoneInstrument(opts, ArpeggioNoteEnvelope())
	input = instr
	input.get_samples_in_byte_rate(44100)

def profile(opts):
	if opts.profile_application:
		import cProfile
		import pstats
		cProfile.run('profile_call()', 'restats')
		p = pstats.Stats('restats')
		p.strip_dirs().sort_stats('time').print_stats()
		sys.exit(0)

def get_args():
	parser = argparse.ArgumentParser(prog = 'synth', description = 'Programmable synth')
	parser.add_argument('input', metavar = 'INPUT', nargs=1, help = 'the input file path')
	parser.add_argument('output', metavar = 'OUTPUT', nargs='?', help = 'the optional output file path. Default is INPUT.wav')
	parser.add_argument('--bpm', '-b')
	parser.add_argument('-w', '--wave', action = 'store_true', help = 'Output wave file.')
	parser.add_argument('--profile', action = 'store_true', help = 'Profile the application to find hot spots.')
	parser.add_argument('--stdout', action = 'store_true', help = 'Also write PCM data to stdout. Only valid in conjunction with the --wave flag.')
	args = parser.parse_args()
	opts = Options(args.input[0], args.output, args.bpm)
	opts.write_wave = args.wave
	opts.write_wave_to_stdout = args.stdout
	opts.profile_application = args.profile
	return opts

def main():
	opts = get_args()
	synth = Synthesizer(opts).load_from_midi(opts.input)
	profile(opts)
	wave = None
	if stream.PYAUDIO and not opts.write_wave:
		stream.stream_to_pyaudio(opts, synth)
	else: 
		WaveWriter(opts).output(synth)

if __name__ == '__main__':
	main()

#実行
python synth.py -b 120 test1.mid

以上。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?