Programming with Python: Math, Music and Art in Web Based ... · Programming with Python: Math,...

52
Programming with Python: Math, Music and Art in Web Based Environments GISA 2016 Session 233 Marist School Christopher Michaud

Transcript of Programming with Python: Math, Music and Art in Web Based ... · Programming with Python: Math,...

Page 1: Programming with Python: Math, Music and Art in Web Based ... · Programming with Python: Math, Music and Art in Web Based Environments GISA 2016 Session 233. Marist School. ... Disney,

Programming with Python: Math, Music and Art in Web

Based EnvironmentsGISA 2016

Session 233Marist School

Christopher Michaud

Page 2: Programming with Python: Math, Music and Art in Web Based ... · Programming with Python: Math, Music and Art in Web Based Environments GISA 2016 Session 233. Marist School. ... Disney,

Why Computer Science and Art?

• Intersection between the process of creating Art

• Language of Mathematics

• Tools of Computer Science

Page 3: Programming with Python: Math, Music and Art in Web Based ... · Programming with Python: Math, Music and Art in Web Based Environments GISA 2016 Session 233. Marist School. ... Disney,

Process of Creating Art

• Iterative interaction between the Artist and the Materials of Art• “What If . . . .?”• Artist creates art to embody non-verbal ideas, motion, flow• Artist reflects and refines the art during creation

• From Music Education• A Philosophy of Music Education, Bennett Reimer

Page 4: Programming with Python: Math, Music and Art in Web Based ... · Programming with Python: Math, Music and Art in Web Based Environments GISA 2016 Session 233. Marist School. ... Disney,

Language of Mathematics

• Spatial Relationships in 1D, 2D, 3D spaces• Temporal Thinking – how do properties change through time (rate of

change, derivative . . .)• Sequential Flow of Logic• Dynamics – how are parts of the system changing? How can we

control the change?

Page 5: Programming with Python: Math, Music and Art in Web Based ... · Programming with Python: Math, Music and Art in Web Based Environments GISA 2016 Session 233. Marist School. ... Disney,

Tools of Computer Science

• Data types• Variables• Arrays• Control• Functions• Classes• Recursion

Page 6: Programming with Python: Math, Music and Art in Web Based ... · Programming with Python: Math, Music and Art in Web Based Environments GISA 2016 Session 233. Marist School. ... Disney,

What is Programming?Allows humans to encode, refine, reflect, and share logical

thought in a dynamic moving system that changes over time.

Music and Programming (Computational Thinking) are the same Human Skill expressed to two different domains: nonverbal feelings in art/music and logical/motion thought in Programming or Computational Thinking

Page 7: Programming with Python: Math, Music and Art in Web Based ... · Programming with Python: Math, Music and Art in Web Based Environments GISA 2016 Session 233. Marist School. ... Disney,

Part One: Music Mixing with Python and EarSketch

Large population of students through which music is the primary vehicle for personal expression.

In CS – we tend to focus on:Graphics / Games / VisualText Processing / DatabaseLogic / Algorithmic / Mathematical Robotics and Kinesthetic

EarSketch seeks to engage students in CS through the art of music.

Page 8: Programming with Python: Math, Music and Art in Web Based ... · Programming with Python: Math, Music and Art in Web Based Environments GISA 2016 Session 233. Marist School. ... Disney,

Description of EarSketch•Programming Environment•Python Based•Web Based App or Installed System of Software•API built in Python for Music Mixing•NSF Funded project to encourage computational interest through the mixing and sharing of music.

•Curriculum and Social Media Site•http://earsketch.gatech.edu

Page 9: Programming with Python: Math, Music and Art in Web Based ... · Programming with Python: Math, Music and Art in Web Based Environments GISA 2016 Session 233. Marist School. ... Disney,

AuthenticRelevantExpressive

Page 10: Programming with Python: Math, Music and Art in Web Based ... · Programming with Python: Math, Music and Art in Web Based Environments GISA 2016 Session 233. Marist School. ... Disney,

EarSketch Website: Web based IDE and DAW

Coding Window: Programming

Music View: Display and PlaybackDocumentation and CurriculumSound Library

Toggle Views

Page 11: Programming with Python: Math, Music and Art in Web Based ... · Programming with Python: Math, Music and Art in Web Based Environments GISA 2016 Session 233. Marist School. ... Disney,

What is Python?•General purpose Computer Programming Language•Web development: Google and Yahoo•Game Development, Science, Graphics•In Business: National Weather Service, NASA, IBM, Disney, and Nokia

•Used in Development of the Google Car and other Robotic Systems

Page 12: Programming with Python: Math, Music and Art in Web Based ... · Programming with Python: Math, Music and Art in Web Based Environments GISA 2016 Session 233. Marist School. ... Disney,

How is Music Organized? How do we encode musical clips?

Tracks: Vertical Layers of Music

Layers: Music exists through time

Page 13: Programming with Python: Math, Music and Art in Web Based ... · Programming with Python: Math, Music and Art in Web Based Environments GISA 2016 Session 233. Marist School. ... Disney,

Data Types and Variables

Page 14: Programming with Python: Math, Music and Art in Web Based ... · Programming with Python: Math, Music and Art in Web Based Environments GISA 2016 Session 233. Marist School. ... Disney,

EarSketch Python FunctionsEarSketch Function Description

init() Start new Earsketch Mix and clear the DAW

setTempo(120) Sets the beats per minute (speed) of the Mix

println(“Hello”) Prints message to console

Page 15: Programming with Python: Math, Music and Art in Web Based ... · Programming with Python: Math, Music and Art in Web Based Environments GISA 2016 Session 233. Marist School. ... Disney,

EarSketch Python FunctionsMusic Mixing Functions

fitMedia(file, track, start, end)

makeBeat(file, track, measure, beatString)

setEffect(track, effect, parameter, sV, sM, eV, eM)

rhythmEffects(track, effect, parameter, list, measure)

Page 16: Programming with Python: Math, Music and Art in Web Based ... · Programming with Python: Math, Music and Art in Web Based Environments GISA 2016 Session 233. Marist School. ... Disney,

“fitMedia” FunctionfitMedia(file, track, start, end)

Location of MediaSound

Which Track in Reaper

Start measure.

End Measure

fitMedia(HIP_HOP_DRUMS1_2M, 1, 1, 9)

Example:

Page 17: Programming with Python: Math, Music and Art in Web Based ... · Programming with Python: Math, Music and Art in Web Based Environments GISA 2016 Session 233. Marist School. ... Disney,

Setting Volume EffectssetEffect(track, VOLUME, GAIN, level, start, level2, end)

• Example

setEffect(1, VOLUME, GAIN, -40, 1, 10, 5)

Page 18: Programming with Python: Math, Music and Art in Web Based ... · Programming with Python: Math, Music and Art in Web Based Environments GISA 2016 Session 233. Marist School. ... Disney,

Selected List of Effects and Parameters

Effect Parameter Min to Max ValuesVOLUME GAIN -60 to 12

DELAY DELAY_TIME 0 to 300.0

CHORUS CHORUS_LENGTH 1.0 to 15.0

CHORUS CHORUS_NUMVOICES 1.0 to 8.0

DISTORTION DISTO_GAIN 0.0 to 50.0

FILTER FILTER_FREQ 20.0 to 20000.0

PAN LEFT_RIGHT -100 to 100 (Left to Right)

Complete Effect list at: http://earsketch.gatech.edu/category/learning/reference/every-effect-explained

Page 19: Programming with Python: Math, Music and Art in Web Based ... · Programming with Python: Math, Music and Art in Web Based Environments GISA 2016 Session 233. Marist School. ... Disney,

“makeBeat” MethodmakeBeat(file, track, measure, BeatString)

Location of MediaSound

Which Track in Reaper

What measure.

Example: “0+++0+++0+0+0+++”

makeBeat(drums, 1, 1, “0+0+0+++00-00+++”)

Example:

Page 20: Programming with Python: Math, Music and Art in Web Based ... · Programming with Python: Math, Music and Art in Web Based Environments GISA 2016 Session 233. Marist School. ... Disney,

Beat String notation“0, 1, 2, 3 . . . “ = Which Media Sound you want for the segment

of beat. Correspond to placement in a List that is one based.

Note: 0 will refer to a sound if it is the only media file in the argument.

“+” Means extend or loop the Media sound 1/16th of a measure.

“-” Means 1/16th measure of rest.

Page 21: Programming with Python: Math, Music and Art in Web Based ... · Programming with Python: Math, Music and Art in Web Based Environments GISA 2016 Session 233. Marist School. ... Disney,

For Loops: Skip CountingfillDrum = HIP_HOP_SYNTHDRUMS2_2Mbeat = "0+++0+++0-000+00"

for measure in range(1, 9, 2):makeBeat(fillDrum, 1, measure, beat)

measure is the “index variable” = assigned values from therange()

(1, 9, 2) means start counting at 1, end before 9 [meaning 8] and skip count by 2:

(1, 3, 5, 7)

Page 22: Programming with Python: Math, Music and Art in Web Based ... · Programming with Python: Math, Music and Art in Web Based Environments GISA 2016 Session 233. Marist School. ... Disney,

Functions: Recycle and Reuse!

Now I can use this section anywhere!sectionA(1, 9)sectionA(17, 25)

Page 23: Programming with Python: Math, Music and Art in Web Based ... · Programming with Python: Math, Music and Art in Web Based Environments GISA 2016 Session 233. Marist School. ... Disney,

Exercises for Classroom

•Create EarSketch Account•Mix1: (AB Section Exercise)

• Use fitMedia and setEffect• Music in sections

•Mix2: (makeBeat and For Loop Exercise)• makeBeat• For Loop Structure

•Mix3: (Defining Functions Exercise)•Final Mix

Page 24: Programming with Python: Math, Music and Art in Web Based ... · Programming with Python: Math, Music and Art in Web Based Environments GISA 2016 Session 233. Marist School. ... Disney,

Control and Data StructuresIf Statement:

Page 25: Programming with Python: Math, Music and Art in Web Based ... · Programming with Python: Math, Music and Art in Web Based Environments GISA 2016 Session 233. Marist School. ... Disney,

Lists

Page 26: Programming with Python: Math, Music and Art in Web Based ... · Programming with Python: Math, Music and Art in Web Based Environments GISA 2016 Session 233. Marist School. ... Disney,

Part Two: Programming with Java and Processing

What is Processing?

● Java based programming environment (IDE) from MIT● Free!!!● Designed for creating interactive Art● Data Visualization● Prototyping Tool● Many libraries for interfacing with other hardware● Very High Ceiling and Clean interface● http://nebomusic.net/processinglessons/

Page 27: Programming with Python: Math, Music and Art in Web Based ... · Programming with Python: Math, Music and Art in Web Based Environments GISA 2016 Session 233. Marist School. ... Disney,

Screenshots: IntelliJ compared to Processing

Page 28: Programming with Python: Math, Music and Art in Web Based ... · Programming with Python: Math, Music and Art in Web Based Environments GISA 2016 Session 233. Marist School. ... Disney,

Processing Java Topics•Data Types•Two Functions in Processing•Comments•Canvas•Shape Drawing•Pen•Colors•Control Structures

A Side Note: Comments

•Comments are for humans

•Computer ignores comments

•Use lots of them in programming

–Graded on Comments

–Good style

–Allows code to be used by others

•// Single Line Comment

•/* Multi line comments

•*/

Page 29: Programming with Python: Math, Music and Art in Web Based ... · Programming with Python: Math, Music and Art in Web Based Environments GISA 2016 Session 233. Marist School. ... Disney,

Java Data Types

•int: Whole Numbers•float: Decimal Point Numbers

•String: Characters and Letters

•boolean: 1 Bit value storing ‘true’ or ‘false’ (Sometimes ‘1’ or ‘0’)

•char: 16 Bit Unicode character ranging from \u0000 to \uffff (Hexidecimal)

–This is a single Letter or character•String: (This is actually a class)–java.lang.String–‘string’ of characters: like “Mr.

Michaud”

Page 30: Programming with Python: Math, Music and Art in Web Based ... · Programming with Python: Math, Music and Art in Web Based Environments GISA 2016 Session 233. Marist School. ... Disney,

Operators and Logic•String name = “Bob”; // Used to Assign a value•name == “Bob” // ‘is equal to’•4 * 5 // multiply•20 / 4 // Divide•4 + 5 // Addition•4 – 5 // Subtraction•5 % 4 // Modulo•5 > 4 •4 < 5 •5 >= 4•4 <= 5

Logic:

&& // Means “and”|| // Means “or”

Page 31: Programming with Python: Math, Music and Art in Web Based ... · Programming with Python: Math, Music and Art in Web Based Environments GISA 2016 Session 233. Marist School. ... Disney,

Two Functions in Processing

•All Processing Programs must have these two functions:

–void setup()–void draw()

Page 32: Programming with Python: Math, Music and Art in Web Based ... · Programming with Python: Math, Music and Art in Web Based Environments GISA 2016 Session 233. Marist School. ... Disney,

setup() and draw()

•void setup()–Runs one time–Initialize Objects–Set Canvas size and Color–Still Images

•void draw()–Runs in a loop–Used in Animation–Moving Images by changing

properties over time

Page 33: Programming with Python: Math, Music and Art in Web Based ... · Programming with Python: Math, Music and Art in Web Based Environments GISA 2016 Session 233. Marist School. ... Disney,

Canvas

Page 34: Programming with Python: Math, Music and Art in Web Based ... · Programming with Python: Math, Music and Art in Web Based Environments GISA 2016 Session 233. Marist School. ... Disney,

Canvas Functions•size(width, height); // Sets the

Size of the Canvas•background(red, green, blue);

// Sets the color of the canvas

Page 35: Programming with Python: Math, Music and Art in Web Based ... · Programming with Python: Math, Music and Art in Web Based Environments GISA 2016 Session 233. Marist School. ... Disney,

2D Shape Functionsline(x1, y1, x2, y2);

rect(x, y, width, height);

ellipse(x, y, width, height);

triangle(x1, y1, x2, y2, x3, y3);

quad(x1, y1, x2, y2, x3, y3, x4, y4);

arc(x, y, width, height, start, stop);

Note: Arc start and stop are in RADIANSUse radians(deg) to convert.

Page 36: Programming with Python: Math, Music and Art in Web Based ... · Programming with Python: Math, Music and Art in Web Based Environments GISA 2016 Session 233. Marist School. ... Disney,

Color and Pen in Processingfill(red, green, blue); // inside of shape

fill(red, green, blue, alpha); // inside of shape

stroke(red, green, blue); // outside of shape

stroke(red, green, blue, alpha); // outside of shape

strokeWeight(10); // Thickness of shape

Page 37: Programming with Python: Math, Music and Art in Web Based ... · Programming with Python: Math, Music and Art in Web Based Environments GISA 2016 Session 233. Marist School. ... Disney,

Color Representations in Graphic Systems• Red, Green , Blue (Standard)

– Values from 0 to 255 (Base 10)• 0 is Dark (no light), 255 is maximum

– Values of 00 to ff (Hexadecimal)• #ff0000 is Red or same as (255, 0, 0)

– Mix Red, Green, and Blue to Create all Colors

• Standard in Computer Graphics (Apple, Android, Java, Maya . . )

Page 38: Programming with Python: Math, Music and Art in Web Based ... · Programming with Python: Math, Music and Art in Web Based Environments GISA 2016 Session 233. Marist School. ... Disney,

Color in Processing: H, S, B

• Hue, Saturation, Brightness• Device independent• Available in Processing

http://www.tomjewett.com/colors/hsb.html

Page 39: Programming with Python: Math, Music and Art in Web Based ... · Programming with Python: Math, Music and Art in Web Based Environments GISA 2016 Session 233. Marist School. ... Disney,

Functions in Java to Create Complex Shapes• Return types:

– void: Does not return a Value. Performs an action– int: Returns an integer value– float: Returns a float value– Any data type or object can be a function return type

• Parameters: Values passed into the function• Brackets {}

– Define regions of the Function

Page 40: Programming with Python: Math, Music and Art in Web Based ... · Programming with Python: Math, Music and Art in Web Based Environments GISA 2016 Session 233. Marist School. ... Disney,

Example Functions

Sample student created functions

Page 41: Programming with Python: Math, Music and Art in Web Based ... · Programming with Python: Math, Music and Art in Web Based Environments GISA 2016 Session 233. Marist School. ... Disney,

Objects in Processing: Modeling with Java ClassesWhat is a Class?Collection of Variables and Functions that model an object.Blueprint through which we build instances.

Remember:Variables store valuesFunctions store sequences of directionsClasses store models of objects with variables and functions.

Page 42: Programming with Python: Math, Music and Art in Web Based ... · Programming with Python: Math, Music and Art in Web Based Environments GISA 2016 Session 233. Marist School. ... Disney,

Parts of a ClassNameFields

-Variables and Data structures that store the properties of the object

Functions-Code that gives the Class the ability to perform actions-Accessors, Modifiers, Void(Getters, Setters, and Action)

Constructor-Builds the instance and returns it to the program

Page 43: Programming with Python: Math, Music and Art in Web Based ... · Programming with Python: Math, Music and Art in Web Based Environments GISA 2016 Session 233. Marist School. ... Disney,

The Actor ClassImplements the idea of the Sprite from Scratch.Models a 2D Character / Agent in the Animated World.Automates movements, collision, edge interaction, colors, rotation, scaling,

and polygons.Code is provided to students as a library.Actor code is open source to students so they can modify if desired.

http://nebomusic.net/processinglessons/Actor.txt

Page 44: Programming with Python: Math, Music and Art in Web Based ... · Programming with Python: Math, Music and Art in Web Based Environments GISA 2016 Session 233. Marist School. ... Disney,

Actor Class Fields and Functions

Page 45: Programming with Python: Math, Music and Art in Web Based ... · Programming with Python: Math, Music and Art in Web Based Environments GISA 2016 Session 233. Marist School. ... Disney,

For Loops•Repeats section of code while counting up or down with an index

variable•Example

for (int i = 0; i < 10; i++) {System.out.println(i);

}

Page 46: Programming with Python: Math, Music and Art in Web Based ... · Programming with Python: Math, Music and Art in Web Based Environments GISA 2016 Session 233. Marist School. ... Disney,

for (int i = 0; i < 10; i++) {println(i);

}•i++ means "i = i + 1"•int i means "integer i"•for (int i = 0; i < 10; i++) means "For index variable i starting at 0, while i is less than 10, count be 1."

Page 47: Programming with Python: Math, Music and Art in Web Based ... · Programming with Python: Math, Music and Art in Web Based Environments GISA 2016 Session 233. Marist School. ... Disney,

Conditional Statements

•‘if statement’: Checks if a given statement or expression is true and then executes a section of code

if (score > 9) {textScore.setText(“You Win”);

}

Page 48: Programming with Python: Math, Music and Art in Web Based ... · Programming with Python: Math, Music and Art in Web Based Environments GISA 2016 Session 233. Marist School. ... Disney,

User Input and Fields

Page 49: Programming with Python: Math, Music and Art in Web Based ... · Programming with Python: Math, Music and Art in Web Based Environments GISA 2016 Session 233. Marist School. ... Disney,

Overview of Computational Media Curriculum

Media: File Extensions - How computers store dataGraphics:

PhotoshopSketchUp

Programming:Processing: 2D Graphics

StillAnimation

EarSketch: MusicCode.org JavaScript Lessons in App Studio

Page 50: Programming with Python: Math, Music and Art in Web Based ... · Programming with Python: Math, Music and Art in Web Based Environments GISA 2016 Session 233. Marist School. ... Disney,

Sequence of Lessons Computational Media EarSketch

• AB Exercise: http://nebomusic.net/earsketchlessons/A_B_Exercise_EarSketch2.pdf

• makeBeat Exercise: http://nebomusic.net/earsketchlessons/makeBeat_practice_EarSketch_2.pdf

• Functions Exercise: http://nebomusic.net/earsketchlessons/Directions_EarSketch2_Function_Exercise.pdf

• List Exercise: http://nebomusic.net/earsketchlessons/Directions_EarSketch2_Lists.pdf

• Final Project: http://nebomusic.net/earsketchlessons/EarSketch_Final_Project_v2.pdf

Page 51: Programming with Python: Math, Music and Art in Web Based ... · Programming with Python: Math, Music and Art in Web Based Environments GISA 2016 Session 233. Marist School. ... Disney,

Sequence of Lessons Used in Computational Media Processing

• Boat Exercise: http://nebomusic.net/processinglessons/Processing_Exercise_03a_Functions_Written.pdf

• Function Exercise: http://nebomusic.net/processinglessons/Processing_Exercise_01a_Functions.pdf

• Actor Class Project: http://nebomusic.net/processinglessons/Class_Based_Exercise_Requirements_v4.pdf

• Simple Game Template: http://nebomusic.net/processinglessons/SimpleGameProcessing.pdf

• Particle Simulation (Make it Rain): http://nebomusic.net/processinglessons/Arrays_Loops_Act.pdf

• Final Design Process: http://nebomusic.net/processinglessons/Process_Processing_Final_Project.pdf

Page 52: Programming with Python: Math, Music and Art in Web Based ... · Programming with Python: Math, Music and Art in Web Based Environments GISA 2016 Session 233. Marist School. ... Disney,

References

•Key Java Vocabulary: (http://docs.oracle.com/javase/tutorial/java/nutsandbolts/index.html)

•Processing Official Reference:http://processing.org/reference/