Answered step by step
Verified Expert Solution
Link Copied!

Question

00
1 Approved Answer

music box file: import pygame import time import pygame.midi class MusicBox: def __init__(self, instrument=0): Creates a new MusicBox variable. An optional parameter from 0 to

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

music box file:

import pygame import time import pygame.midi class MusicBox: def __init__(self, instrument=0): """Creates a new MusicBox variable. An optional parameter from 0 to 127 inclusive sets the instrument ID. Experiment with different IDs to play different instruments.""" try: pygame.init() pygame.midi.init() id = pygame.midi.get_default_output_id() except e: id = 0 if id >= 0: self.__player = pygame.midi.Output(id, 1) self.__instrument = instrument self.__enabled = True else: self.__enabled = False def close(self): if self.__enabled: self.__player.close() def play_note(self, note, duration): """Plays a single note for a given duration. The note is in integer notation.""" print("".format(note, duration)) if self.__enabled: self.__player.set_instrument(self.__instrument, 0) self.__player.note_on(note, 127, 0) time.sleep(duration / 1000) self.__player.note_off(note, 127, 0) def play_chord(self, chord, duration): """Plays a list of notes at the same time, for the given duration.""" print("".format(chord, duration)) if self.__enabled: channel = 0 for note in chord: self.__player.set_instrument(self.__instrument, channel) self.__player.note_on(note, 127, channel) channel += 1 time.sleep(duration / 1000) channel = 0 for note in chord: self.__player.note_off(note, 127, channel) channel += 1 def demo(): # Example of how to use the MusicBox m = MusicBox() m.play_note(60, 500) m.play_note(63, 500) m.play_scale([60, 63, 67], 500) m.play_chord([60, 63, 67], 1000) 
Overview In this project you will develop an application that plays musical notes, chords, and scales according to a user's request. I will provide a Python module called musicbox that contains code for playing musical notes through your computer's MIDI synthesizer. Your program will gather input from the user, validate it, and then "drive" the musicbox module with commands to play appropriate notes and scales according to the user's request. The musicbox module will output information about what notes it has been asked to play in addition to generating those sounds through your speakers. You will work with functions, lists, and strings in this assignment, in addition to previous topics like loops and arithmetic. Introduction In music theory, a note is a sound with a particular frequency, that is, the sound wave generated by the note oscillates a certain number of times per second. For example, the note "middle A" has a frequency of 440 Hz, and cycles 440 times per second. Notes with higher frequencies sound "higher" in pitch and oscillate more frequently than notes with lower frequencies/pitch. In Western music, an octave (a range of sound frequencies, where the ending frequency is exactly 2 times the starting frequency) is broken into 12 equal parts called temperments. 7 capital letters and a pair of accent marks ("sharp" and "flat") are used to name each of the 12 parts, traditionally starting with the letter C as such: C CSDDE EFFIG GAA AB (C) The "distance between two adjacent notes in this arrangement is called a "half step", and the distance between every other note is a "whole step. The # ("sharp") symbol denotes a half step above the preceding letter. Alternatively, sharps can be replaced by equivalent b ("flats") which are a half-step below a preceding letter. For example, the notation above with flats instead of sharps looks like C D EEF GG AS A Bb B (C) and you should understand that saying Do is the same as saying C#. If this seems complicated, it is, and only after a few courses in music theory would one really appreciate how this works. In a computer, however, music is represented in a different way using numbers instead of letters. Overview In this project you will develop an application that plays musical notes, chords, and scales according to a user's request. I will provide a Python module called musicbox that contains code for playing musical notes through your computer's MIDI synthesizer. Your program will gather input from the user, validate it, and then "drive" the musicbox module with commands to play appropriate notes and scales according to the user's request. The musicbox module will output information about what notes it has been asked to play in addition to generating those sounds through your speakers. You will work with functions, lists, and strings in this assignment, in addition to previous topics like loops and arithmetic. Introduction In music theory, a note is a sound with a particular frequency, that is, the sound wave generated by the note oscillates a certain number of times per second. For example, the note "middle A" has a frequency of 440 Hz, and cycles 440 times per second. Notes with higher frequencies sound "higher" in pitch and oscillate more frequently than notes with lower frequencies/pitch. In Western music, an octave (a range of sound frequencies, where the ending frequency is exactly 2 times the starting frequency) is broken into 12 equal parts called temperments. 7 capital letters and a pair of accent marks ("sharp" and "flat") are used to name each of the 12 parts, traditionally starting with the letter C as such: C CSDDE EFFIG GAA AB (C) The "distance between two adjacent notes in this arrangement is called a "half step", and the distance between every other note is a "whole step. The # ("sharp") symbol denotes a half step above the preceding letter. Alternatively, sharps can be replaced by equivalent b ("flats") which are a half-step below a preceding letter. For example, the notation above with flats instead of sharps looks like C D EEF GG AS A Bb B (C) and you should understand that saying Do is the same as saying C#. If this seems complicated, it is, and only after a few courses in music theory would one really appreciate how this works. In a computer, however, music is represented in a different way using numbers instead of letters

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions