Manipulating Sound CS1316: Representing Structure and Behavior.

Slides:



Advertisements
Similar presentations
Linked Lists.
Advertisements

The ArrayList Class and the enum Keyword
Chapter 8: Arrays.
Georgia Institute of Technology Introduction to Processing Digital Sounds.
Copyright 2010 by Pearson Education Building Java Programs Chapter 7 Lecture 7-2: Arrays as Parameters reading: , 3.3 self-checks: Ch. 7 #5, 8,
Searching Kruse and Ryba Ch and 9.6. Problem: Search We are given a list of records. Each record has an associated key. Give efficient algorithm.
Lecture 8 CS203. Implementation of Data Structures 2 In the last couple of weeks, we have covered various data structures that are implemented in the.
An Array-Based Implementation of the ADT List public class ListArrayBased implements ListInterface { private static final int MAX_LIST = 50; private Object.
CMPT 225 ADT List using Dynamic arrays A dynamic data structure is one that changes size, as needed, as items are inserted or removed The Java ArrayList.
1 CS 177 Week 15 Recitation Slides Review. Announcements Final Exam on Sat. May 8th  PHY 112 from 8-10 AM Complete your online review of your classes.
12-Jul-15 Lists in Java Part of the Collections Framework.
Week 8: Audio Processing 1.  Light and sound are both transmitted in waves 2.
Intro-Sound-part21 Introduction to Processing Digital Sounds part 2 Barb Ericson Georgia Institute of Technology Oct 2009.
More on Trees CS1316: Representing Structure and Behavior.
Introduction to Computing and Programming in Python: A Multimedia Approach Chapter 7: Modifying Samples in a Range.
Problem Solving with Data Structures using Java: A Multimedia Approach Chapter 5: Arrays: A Static Data Structure for Sounds.
A Computer Science Tapestry 1 Recursion (Tapestry 10.1, 10.3) l Recursion is an indispensable technique in a programming language ä Allows many complex.
Stacks - 2 Nour El-Kadri ITI Implementing 2 stacks in one array and we don’t mean two stacks growing in the same direction: but two stacks growing.
Main Index Contents 11 Main Index Contents Week 3 – The Vector Container.
224 3/30/98 CSE 143 Recursion [Sections 6.1, ]
Structuring Music CS1316: Representing Structure and Behavior.
Chapter 2 Array Data Structure Winter Array The Array is the most commonly used Data Storage Structure. It’s built into most Programming languages.
Arrays Chapter 8. What if we need to store test scores for all students in our class. We could store each test score as a unique variable: int score1.
Ics202 Data Structures. class Array { protected Object[] data; protected int base; public Array (int n, int m) { data = new Object[n]; base = m; } public.
The while Loop Syntax while (condition) { statements } As long condition is true, the statements in the while loop execute.
Georgia Institute of Technology Processing Sound Ranges Barb Ericson Georgia Institute of Technology July 2005.
UsingSoundRanges-part21 Processing Sound Ranges part 2 Barb Ericson Georgia Institute of Technology Oct 2009.
Can we talk?. In Hello World we already saw how to do Standard Output. You simply use the command line System.out.println(“text”); There are different.
ArrayList Class An ArrayList is an object that contains a sequence of elements that are ordered by position. An ArrayList is an object that contains a.
1 CSC 201: Computer Programming I Lecture 2 B. S. Afolabi.
Today’s Agenda  Linked Lists  Double ended Linked Lists  Doubly Linked Lists CS2336: Computer Science II.
Dynamic Array. An Array-Based Implementation - Summary Good things:  Fast, random access of elements  Very memory efficient, very little memory is required.
String Handling StringBuffer class character class StringTokenizer class.
CPSC1301 Computer Science 1 Chapter 8 Introduction to Processing Digital Sounds part 3.
Introduction to Data Structures and Algorithms
EE 422C Day 2 Java, Eclipse. Copyright Pearson Education, 2010 Based on slides bu Marty Stepp and Stuart Reges from
Classes and Objects CS177 Rec 10. Announcements Project 4 is posted ◦ Milestone due on Nov. 12. ◦ Final submission due on Nov. 19. Exam 2 on Nov. 4 ◦
Chapter 8: Modifying Samples in a Range. Chapter Objectives.
Finally! Making the Villagers CS1316: Representing Structure and Behavior.
Searching and Sorting Searching: Sequential, Binary Sorting: Selection, Insertion, Shell.
LINKED LIST’S EXAMPLES Salim Malakouti. Linked List? 523 Pointer Node ValuePointer.
Dynamic Programming & Memoization. When to use? Problem has a recursive formulation Solutions are “ordered” –Earlier vs. later recursions.
CSE 8A Lecture 14 Reading for next class: None (INTERM EXAM #3) Today’s topics: –More Sounds! PSA 6 due tonight PSA 7 (sounds) due next Monday (11/19)
CS 46B: Introduction to Data Structures July 23 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak.
Intro-Sound-Mod10-part31 Introduction to Processing Digital Sounds part 3 while loop, tracing, for loop, parameters Barb Ericson Georgia Institute of Technology.
Chapter 9 Introduction to Arrays Fundamentals of Java.
Stacks as an Abstract Data Type CS1316: Representing Structure and Behavior.
LINKED LISTS.
Problem Solving with Data Structures using Java: A Multimedia Approach Chapter 9: Lists and Trees for Structuring Sounds.
Methods Matthew Harrison. Overview ● There are five main aspects of methods... ● 1) Modifiers – public, private ● 2) Method Name ● 3) Parameters ● 4)
An Array-Based Implementation of the ADT List
CS1316: Representing Structure and Behavior
Processing Sound Ranges part 3
Introduction to Processing Digital Sounds part 2
Building Java Programs
CS1316: Representing Structure and Behavior
Processing Sound Ranges part 1
Search,Sort,Recursion.
Building Java Programs
Introduction to Processing Digital Sounds part 3
Building Java Programs
class PrintOnetoTen { public static void main(String args[]) {
Processing Sound Ranges part 2
Processing Sound Ranges
Creative Commons Attribution Non-Commercial Share Alike License
Building Java Programs
Processing Sound Ranges part 3
CSE 143 Lecture 2 ArrayIntList, binary search
Selection Sort Insertion Sort if time
Presentation transcript:

Manipulating Sound CS1316: Representing Structure and Behavior

Story Programming Sampled Sounds in Java Manipulating sounds in-place Returning a new sound Manipulating an array Inserting a sound Deleting a sound segment

Loading and playing sounds Welcome to DrJava. > Sound s = new Sound(“C:/cs1316/MediaSources/thisisat est.wav") > s.increaseVolume(2.0); > s.play();

increaseVolume method /** * Increase the volume of a sound **/ public void increaseVolume(double factor){ SoundSample [] samples = this.getSamples(); SoundSample current = null; for (int i=0; i < samples.length; i++) { current = samples[i]; current.setValue((int) (factor * current.getValue())); }

increaseVolume: How it works SoundSample is the name of the class for samples. There’s something named Sample already in Java, so it would get confusing. getSamples(), getValue(), and setValue() work just the same as in CS1315 Python. They (respectively) return an array of all the samples, get the value of a given sample object, and set the value of a given sample object.

Methods that return sounds > Sound s = new Sound("D:/cs1316/MediaSources/ thisisatest.wav") > s.play() > s.reverse() Sound number of samples: Why do you think we’re seeing this?

Reverse returns a Sound! /** * Method to reverse a sound. **/ public Sound reverse() { Sound target = new Sound(getLength()); int sampleValue; for (int srcIndex=0,trgIndex=getLength()-1; srcIndex < getLength(); srcIndex++,trgIndex--) { sampleValue = this.getSampleValueAt(srcIndex); target.setSampleValueAt(trgIndex,sampleValue); }; return target; }

Methods that cascade nicely in Sound public Sound reverse() public Sound append(Sound appendSound) public Sound mix(Sound mixIn, double ratio) public Sound scale(double factor)

Little sounds in MediaSources -h: Half second -q: Quarter second -1 or -2: 1 or 2 seconds -tenth: 1/10 second -twentieth: 1/20 second

Making collages public class MySoundCollage { public static void main(String [] args){ Sound snap = new Sound(FileChooser.getMediaPath("snap-tenth.wav")); Sound drum = new Sound(FileChooser.getMediaPath("drumroll-1.wav")); Sound clink = new Sound(FileChooser.getMediaPath("clink-tenth.wav")); Sound clap = new Sound(FileChooser.getMediaPath("clap-q.wav")); Sound drumRev = drum.reverse().scale(0.5); Sound soundA = snap.append(clink).append(clink).append(clap).append(d rumRev); Sound soundB = clink.append(clap).append(clap).append(drum).append(s nap).append(snap); Sound collage = soundA.append(soundB).append(soundB).append(soun dA).append(soundA).append(soundB); collage.play(); }

How do we insert and delete sound? Welcome to DrJava. > Sound test = new Sound("D:/cs1316/MediaSources/thisisatest.wav"); > test.getLength() > Sound clink = new Sound("D:/cs1316/MediaSources/clink- tenth.wav"); > clink.getLength() 2184 > test.insertAfter(clink,40000) > test.play()

Handling the error cases > Sound test2 = new Sound("D:/cs1316/MediaSources/thisisat est.wav"); > test.insertAfter(test2,40000) > test.play()

First, making room start this.getLength() inSound.getLength() start start+inSound.getLength() 1294 …

Second, copying in inSound.getLength() start start+inSound.getLength() 1294 …

insertAfter method /** * insert the input Sound after the nth sample (input integer). * Modifies the given sound insound Sound to insert start index where to start inserting the new sound **/ public void insertAfter(Sound inSound, int start){ SoundSample current=null; // Find how long insound is int amtToCopy = inSound.getLength(); int endOfThis = this.getLength()-1; if (start + amtToCopy > endOfThis) {// If too long, copy only as much as will fit amtToCopy = endOfThis-start-1;} else { // If short enough, need to clear out room. // Copy from endOfThis-amtToCopy;, moving backwards // (toward front of list) to start, // moving UP (toward back) to endOfThis // KEY INSIGHT: How much gets lost off the end of the // array? Same size as what we're inserting -- amtToCopy for (int source=endOfThis-amtToCopy; source >= start ; source--) { // current is the TARGET -- where we're copying to current = this.getSample(source+amtToCopy); current.setValue(this.getSampleValueAt(source)); } // NOW, copy in inSound up to amtToCopy for (int target=start,source=0; source < amtToCopy; target++, source++) { current = this.getSample(target); current.setValue(inSound.getSampleValueAt(source)); }

Setting up the variables SoundSample current=null; // Find how long insound is int amtToCopy = inSound.getLength(); int endOfThis = this.getLength()-1;

Checking for room if (start + amtToCopy > endOfThis) {// If too long, copy only as much as will fit amtToCopy = endOfThis-start-1;} else { // If short enough, need to clear out room.

Now, copy down else { // If short enough, need to clear out room. // Copy from endOfThis-amtToCopy;, moving backwards // (toward front of list) to start, // moving UP (toward back) to endOfThis // KEY INSIGHT: How much gets lost off the end of the // array? Same size as what we're inserting -- amtToCopy for (int source=endOfThis-amtToCopy; source >= start ; source--) { // current is the TARGET -- where we're copying to current = this.getSample(source+amtToCopy); current.setValue(this.getSampleValueAt(source)); }

Finally, copy in the new sound //** Second, copy in inSound up to amtToCopy for (int target=start,source=0; source < amtToCopy; target++, source++) { current = this.getSample(target); current.setValue( inSound.getSampleValueAt(source)); }

How do we delete? > Sound test = new Sound("D:/cs1316/MediaSources/thisisat est.wav"); > test.getLength() > test.delete(2000,30000) > test.play() // We hear “This test”

First, copy from end to getLength, back to start this.getLength()endstart This distance is start-end

Then, clear out the end this.getLength()endstart … This distance is start-end. And we’ll go from the length, backwards.

Deleting method /** * Delete from start to end in this sound start where to start deletion end where to stop deletion **/ public void delete(int start, int end){ int value = 0; // Basically, we simply copy from "end" to getLength back to start for (int source=end, target=start; source < this.getLength(); source++, target++) {value = this.getSampleValueAt(source); this.setSampleValueAt(target,value);} // Then clear out the rest. Gap is end-start length int gap = end-start; for (int i=1; i <= gap ; i++) { this.setSampleValueAt(this.getLength()-i,0);} }

First, copy up—over the start to end gap // Basically, we simply copy from "end" to getLength back to start for (int source=end, target=start; source < this.getLength(); source++, target++) {value = this.getSampleValueAt(source); this.setSampleValueAt(target,value);}

Then, clear out the gap at the end // Then clear out the rest. Gap is end-start length int gap = end-start; for (int i=1; i <= gap ; i++) { this.setSampleValueAt( this.getLength()-i, 0);}

Arrays: Strengths and weaknesses Strengths: Easy to understand Very efficient “Static”—it’s always the same length (shape?) Weaknesses: Any change in the middle is hard to do Expensive in complexity and processing “Static”—it’s always the same length (shape?)