搜索 Search
《交互式程序设计(第二版)》/Programming Interactivity 2nd (second) Edition
/
出版于/Published in 2014
译著/translated book
文献作者/Author:
文献译者/Translator:
文献校对/Proofreader:
文献收集/Collector
文献顾问/Consultant:
关键词/Keywords:
读物/Reader:
Functions: --Defining a Function: “When someone gives you some money, add the amount given to you to the amount that you already have and report that number back to me.” There are a few parts there that can be broken out: * An amount of money that you are given * Adding the amount given to you to the amount that you already have * Reporting that number Then you create a function that takes money, adds it to myBank, and returns that value: int moneyReceived(int money){ myBank += money; return myBank; } --Passing Parameters to a Function: Once the function is defined, it’s time to call it. To call the function, you need to pass the correct kinds of parameters to it, which requires knowing what parameters the function requires. In the moneyReceived()function example, the required parameter is a single integer. moneyReceived(4);
在Supercollider中,如果想循环一个进程,可以使用"do";如果想自动完成一系列进程,可以使用"Task"。 下面是一个简单的例子: ( // 定义一个声音,该例子中为一个正弦波,并含有有两个变量,频率(freq)和振幅(amp)。 SynthDef("sine", { arg freq, amp = 0.2; var osc; osc = SinOsc.ar(freq, 0, amp).dup; Out.ar(0, osc); }).send(s); // 下面为自动进程,该例子中自动运行上面的声音,并且从100Hz,200Hz,300Hz中随机抽取频率,间隔3秒,重复3次。 t = Task({ a=Synth("sine"); 3.do({ a=Synth("sine"); a.set(\freq, [100,200,300].choose.postln); 3.wait; a.free; 3.wait;} ) }); ) t.start; t.pause; t.resume; t.reset; t.stop;
在SuperCollider中,如果想切换声卡驱动以及声卡输出通道,可以使用以下代码: //以Fireface UFX音频接口为例,现在选择的驱动为MME,输出通道为3和4。 s.options.device_("MME : Analog (3+4) (RME Fireface UFX)"); 如果想要ASIO多通道,可以尝试一下代码: s.options.device_("ASIO : ASIO Fireface") ( {Out.ar( [0,1,2,3,4,5], PinkNoise.ar(0.1))}.play ) // for 5.1 system in Room 310 ( {Out.ar( [0,1], PinkNoise.ar(0.1))}.play; {Out.ar( [2], PinkNoise.ar(0.2))}.play; {Out.ar( [3], SinOsc.ar(55, 0.5))}.play; {Out.ar( [4,5], PinkNoise.ar(0.1))}.play; )
Arrays: An arraycontains one or more variables in a list. int[] numbers = new int[3]; (in Processing) int: The type that the array contains []: Signifies that this is an array numbers: The name of the array variable [3]: How many variables the array will contain int[3] numbers = {1,2,3}; (in C++) int: The type that the array contains []: Signifies that this is an array [3]: How many variables the array will contain numbers: The name of the array variable Operators: Operatorsare the symbols that a compiler uses to perform commands and calculations in your program. Operators let you set variables like the =operator, compare variables like the ==operator, add variables like the +operator, and so on. There are three major types of operators. The first operators are the mathematical operators that perform mathematical operations. The second are assignment operators that change values. The third are comparison operators, which determine whether two variables are equal, different, greater than, or less than another variable. Control Statements: You’ll often want to control how the logic of your program flows. If one thing is true, then you’ll want to do something different if it’s false. You might also want to do something a certain number of times or until something becomes false. There are two kinds of control statements that you can use to create this kind of logic flow in your application: 1, conditional logic statements, which check whether something is trueor false. 2, loop statements, which do something a set number of times or until something becomes false. --if/then: int myWeight = 72; if(myWeight > 100){ print(" you're getting heavy! "); } else { print(" you're still doing ok "); } --for Loop: The forstatement lets us do things over and over again, for a specified number of repetitions. Loops are frequently used to loop through arrays and examine, use, or alter each element within them. This is going to be particularly useful when dealing with the pixels in images or frames of video, as well as sounds, data from the Internet, and many other kinds of information that needs to be sorted through: for(i=0; i<10; i++){ print("i is" + i); } in=0: Initialize the counter i<10: Continue until this statement is false i++: What to do on each pass through the loop --continue --break
-Variables: x = 440 "There’s something called x, and it is equal to the number 440." -Simple Types: --int: --float: --char: This type can contain characters, that is, single letters or typographic symbols such as A, d, and $. --bool or boolean: Booleanvalues store two possible values: trueand false. In C++ and Arduino, the trueis actually a 1, and the falseis actually a 0. It’s usually clearer to use trueand false, but you can use 1 and 0 if you like. --string: A stringis a sequence of characters. Here are some strings in Processing: String f = "foo"; String b = "bar"; String fb = f+b; // this will be "foobar" Here are some strings in C++: string f = "foo"; string b = "bar"; string foobar = f+" "+b; // this will be "foo bar" note the space w/in quotes -Arrays -Operators: +, −, *, / Adds, subtracts, multiplies, and divides. % Modulo; returns the remainder of a division. = Assignment; assigns the value on the right to the variable on the left. +=, −=, *=, /= Mathematical assignment; adds, subtracts, multiples, or divides the value on the left by the value on the right and sets the value on the right to that result. ++ Adds 1 to the value to the left. −− Subtracts 1 from the value to the left. == Compares the value on the left with the value on the right. If they are equal, then the expression is true. != Compares the value on the left with the value on the right. If they are not equal, then the expression is true. >, >= Compares the value on the left with the value on the right. If the value on the left is greater than or greater than or equal to the value on the left, the expression is true. <, <= Compares the value on the left with the value on the right. If the value on the left is less than or greater than or equal to the value on the left, the expression is true. && Checks the truth of the expression to the left of the operator and to the right; if both are true, the entire expression is true. || Checks the expression to the left of the operator and to the right of the operator; if either is true, the entire expression is true. -Control Statements: --if/then --for Loop --continue --break -Functions: A functionis a name for a grouping of one or more lines of code and is somewhat like a variable in that it has a type and a name. It’s very much unlike a variable in that it doesn’t just store information; A function is something more like an instruction with something that is given at the beginning of the instruction and something that is expected in return. --Defining a Function --Passing Parameters to a Function
Code: Code is a series of instructions that a computer will execute when the code is run.~ Writing code is typing code instructions into a text file that will later be passed to a compiler of some sort. To write a program can mean writing source code from scratch, or it can mean putting several programs together and creating a way for them to communicate. Files: Code is stored in text files that usually any text editor can open.~ Arduino projects use .ino files and sometimes .c files. Processing projects use .pde files and sometimes .javafiles. openFrameworks projects use .cpp and .h files. Compiler: A compiler is a program that takes a code file (or many code files) and turns it (or them) into a series of instructions that a computer will run as a program. ~ Most modern computers do not directly process any instructions that you write; instead, you ask a compiler to turn your code into machine instructions. The compiler optimizes machine instructions for the computer to run very quickly, but they would be very difficult for a person to write, so the better step is to write code in a more human-friendly way and convert it to machine-friendly instructions. /You can imagine/ /the process of writing code as a series of translations/ /in which you tell the compiler what you want to do with the program/ /that (the program) it will create in a high-level programming language like Processing or C++ (and)/ /the compiler then creates a machine language file that will run that file (the program like Processing or C++)./ Executable: An executable is a file that can be run as an application. It is the result of writing code and compiling it. An application may consist of many executable files, or it may consist of only one.