rsc3/doc-schelp/Help-3.12.2/Tutorials/Getting-Started/02-First-Steps.html

112 lines
12 KiB
HTML
Raw Permalink Normal View History

2022-08-24 13:53:18 +00:00
<!doctype html><html lang='en'><head><title>02. First Steps | SuperCollider 3.12.2 Help</title>
<link rel='stylesheet' href='./../../scdoc.css' type='text/css' />
<link rel='stylesheet' href='./../../codemirror.css' type='text/css' />
<link rel='stylesheet' href='./../../editor.css' type='text/css' />
<link rel='stylesheet' href='./../../frontend.css' type='text/css' />
<link rel='stylesheet' href='./../../custom.css' type='text/css' />
<meta name='viewport' content='width=device-width, initial-scale=1'>
<meta http-equiv='Content-Type' content='text/html; charset=UTF-8' />
<script src='./../../lib/jquery.min.js'></script>
<script src='./../../lib/codemirror-5.39.2.min.js' type='text/javascript'></script>
<script src='./../../lib/codemirror-addon-simple-5.39.2.min.js' type='text/javascript'></script>
<script>
var helpRoot = './../..';
var scdoc_title = '02. First Steps';
var scdoc_sc_version = '3.12.2';
</script>
<script src='./../../scdoc.js' type='text/javascript'></script>
<script src='./../../docmap.js' type='text/javascript'></script>
<script src='qrc:///qtwebchannel/qwebchannel.js' type='text/javascript'></script>
</head>
<body onload='fixTOC()'>
<div id='toc'>
<div id='toctitle'>02. First Steps:</div>
<span class='toc_search'>Filter: <input id='toc_search'></span><ul class='toc'><li class='toc1'><a href='#Hello%20World,%20I'm%20SuperCollider'>Hello World, I'm SuperCollider</a></li>
<ul class='toc'></ul><li class='toc1'><a href='#The%20World%20According%20to%20SuperCollider'>The World According to SuperCollider</a></li>
<ul class='toc'></ul><li class='toc1'><a href='#Suggested%20Exercise'>Suggested Exercise</a></li>
<ul class='toc'></ul></ul></div><div id='menubar'></div>
<div class='contents'>
<div class='header'>
<div id='label'>
<span id='folder'>Tutorials/Getting-Started</span>
| <span id='categories'><a href='./../../Browse.html#Tutorials'>Tutorials</a>&#8201;&gt;&#8201;<a href='./../../Browse.html#Tutorials>Getting-Started'>Getting-Started</a></span>
</div><h1>02. First Steps</h1>
<div id='summary'>Getting Started With SuperCollider</div>
</div>
<div class='subheader'>
<div id='related'>See also: <a href="./../../Tutorials/Getting-Started/00-Getting-Started-With-SC.html">00. Getting Started With SC</a></div>
</div>
<div class='note'><span class='notelabel'>NOTE:</span> This document is written so that it can be used on all supported systems, though having first of all macOS in mind using the application SuperCollider.app (SCapp). Some features, e.g. menu commands, are platform specific, but the principles extend to all platforms. See the helpfile <a href="./../../Reference/KeyboardShortcuts.html">Keyboard Shortcuts</a> for key commands when using other tools such as SC enhanced editors, e.g. on Linux platforms.</div><h2><a class='anchor' name='Hello%20World,%20I'm%20SuperCollider'>Hello World, I'm SuperCollider</a></h2>
<p>It is traditional when learning a new programming language to start with a simple program called 'Hello World'. This just makes the program print the text 'Hello World!' to well, wherever it prints text. In SC that's a place called the post window. The post window is the one that opened up when you first started SC, and a bunch of stuff was printed there which looks something like this:<textarea class='editor'>init_OSC
compiling class library..
NumPrimitives = 587
compiling dir: '/Applications/SC3/SCClassLibrary'
pass 1 done
Method Table Size 3764776 bytes
Number of Method Selectors 3184
Number of Classes 1814
Number of Symbols 7595
Byte Code Size 180973
compiled 296 files in 1.34 seconds
compile done
RESULT = 256
Class tree inited in 0.14 seconds</textarea>
<p>Don't worry too much about what all that means just now, just keep in mind that this is where SC will send you information. It's also where we'll get the result of our Hello World program, which you can see below:<textarea class='editor'>"Hello World!".postln;</textarea>
<p>To execute it, simply click to place the cursor somewhere on the same line as the code and then press Shift-Enter (Shift-Return on macOS). Try this now.
<p>If all went well, you should see this in the post window.<textarea class='editor'>Hello World!
-&gt; Hello World!</textarea>
<p>Alternatively you can also first select the code you wish to execute (by clicking and dragging over the text until it is highlighted) and then press Ctrl-Enter (Cmd-Return on macOS). Try this now.
<p>Now let's take a closer look at the code. The first bit, <code>"Hello World!"</code>, is a kind of <em>Object</em>, called a String. An object is basically just a way of representing something in the computer, for instance a bit of text, or an oscillator, that allows you to control it and send messages to it. More about that later, but for now just understand that a String is a way of representing a bit of text.
<p>The second bit, <code>.postln;</code>, says 'print me (or a meaningful description of me) to the post window.' Remember postln, it's your friend. You can apply it to almost anything in SC and get something meaningful back. This can be very handy when tracking down bugs in your code.
<p>Why did it print twice? Well, when you execute code in SC, it always posts the result of the last bit of code (the last statement). So it first prints because we explicitly told it to print, and then it prints the result of this operation, which happens to be the same in this case.
<p>So in this case we didn't really need the <code>postln</code> bit. But in the following example we would. Select both lines of text by clicking and dragging over them, and then execute, i.e. press Ctrl-Enter (Cmd-Return on macOS).<textarea class='editor'>"Hello there, I'm SuperCollider!".postln;
"Hello World!".postln;</textarea>
<p>The first line, 'Hello there, I'm SuperCollider!' would not have printed if we didn't have the explicit postln.
<p>In general, when you are meant to execute several lines of code at the same time they will be surrounded by parentheses, as in the example below. You can have your cursor anywhere in this region (or on the line of the parentheses on macOS), then double-click and press Ctrl-Enter or Shift-Enter (Cmd-Return or Shift-Return on macOS) - this selects the whole region and executes it. Try it out on the example below.<textarea class='editor'>(
"Call me,".postln;
"Ishmael.".postln;
)</textarea>
<p>Double clicking inside a pair of enclosing parentheses may not select the entire region on all systems, notably not on modern Macs. On some it may only select the double clicked word. Thus it may be good, to make it a habit to stick to a particular technique. E.g. to first always check you have selected all of the intended code, regardless of the selection technique, before pressing either Ctrl-Enter or Shift-Enter (Cmd-Return or Shift-Enter on macOS). Then stick to the technique that works best for you. Experiment with this to learn how your system behaves.
<p>When code is not surrounded by parentheses it is generally intended to be executed one line at a time. You can have your cursor anywhere in a line of code and press Ctrl-Enter or Shift-Enter (Cmd-Return or Shift-Return on macOS) - this selects the whole line and executes it.
<p>Note that each of the lines within the block of code ends with a semi-colon. This is very important when executing multiple lines of code. Try what happens when you execute following variant of the almost identical code.<textarea class='editor'>(
"Call me?".postln
"Ishmael.".postln;
)</textarea>
<p>Executing the code above results in a 'Parse Error'. With an error of this kind, the dot in the error message shows you where SC ran into trouble. Here it happens just after <code>"Ishmael."</code>.<textarea class='editor'>ERROR: syntax error, unexpected STRING, expecting DOTDOT or ':' or ',' or ')'
in interpreted text
line 3 char 10:
"Ishmael.".postln;
^^^^^^^^^
)</textarea>
<p>Usually the problem actually occurs a little before that, so that's where you should look. In this case, it's the lack of a semi-colon at the end of the previous line. Note that each line of code ends normally with a semi-colon. This is how you separate lines of code in SC. Since we didn't have a semi-colon between the two lines we have gotten an error.
<p>Note also, having an extra semi-colon at the very end of the last piece of code does not hurt and is tolerated by SC for reasons of convenience.
<p>A couple of more notes about the post window. It's very useful to be able to see it, but sometimes it can get hidden behind other windows. You can bring it to the front at any time by pressing Cmd-\.
<p>Other times, the post window becomes full of text and hard to read. You can clear it at any time by pressing Ctrl-Shift-P (Cmd-Shift-P on macOS).<h2><a class='anchor' name='The%20World%20According%20to%20SuperCollider'>The World According to SuperCollider</a></h2>
<p>SuperCollider is actually three programs:<ul>
<li>The text editor you are looking at (also referred to as the IDE or Integrated Development Environment),<li>the language (sclang or the "client" app),<li>and the server, which does the actual synthesis and calculation of audio.</ul>
<p>The sclang part is a sophisticated programming language with nice features for building GUIs (Graphical User Interfaces); and the server part is a lean, mean, efficient UNIX command line application (meaning it runs without any GUI representation).
<p>They communicate by a protocol called OSC (Open Sound Control), over either UDP (User Datagram Protocol) or TCP (Transmission Control Protocol), which are network protocols also used on the internet. Because the client and server communicate this way, more advanced projects might run them on separate computers for performance reasons. In fact, it's even possible that they could be running in different parts of the world! However, just because these two applications communicate using common internet protocols does not mean they must be connected to the internet or on different computers. Most of the time they will be running on the same computer, and the "networking" aspect of things will be relatively transparent for you. Especially while you're still getting started.
<p>You can only communicate with the server using OSC messages over the network, but luckily the language app has lots of powerful objects which represent things on the server and allow you to control them easily and elegantly. Understanding how exactly that works is crucial to effectively working in SC, so we'll be talking about that in some depth.
<p>But first let's have a little fun, and make some sound!
<p>For more information see:
<p><a href="./../../Guides/How-to-Use-the-Interpreter.html">How to Use the Interpreter</a>, <a href="./../../Reference/Literals.html">Literals</a>, <a href="./../../Classes/String.html">String</a>, <a href="./../../Guides/ClientVsServer.html">Client vs Server</a>, <a href="./../../Reference/Server-Architecture.html">Server Architecture</a><h2><a class='anchor' name='Suggested%20Exercise'>Suggested Exercise</a></h2>
<p>Open a new tab or window by pressing Ctrl-N (Cmd-N on macOS) or choose 'New' from the File menu. Save the document by giving it a name like 'My first SC code.scd'. Note, you should always use extension '.scd' for files containing SC code. Copy some of above code examples and paste them into the new document using Ctrl-C and Ctrl-V (Cmd-C and Cmd-V on macOS) or use the copy and paste Edit menu items.
<p>SC will let you edit the help files and documentation, so it's always a good idea to copy text over before changing it to avoid accidentally saving altered help files!
<p>Experiment with altering the text between the quotes to print different things to the post window. Do this with both blocks of text wrapped in parentheses, and single lines.
<p>____________________
<p>This document is part of the tutorial <strong>Getting Started With SuperCollider</strong>.
<p>Click here to go on to the next section: <a href="./../../Tutorials/Getting-Started/03-Start-Your-Engines.html">03. Start Your Engines</a>
<p>Click here to return to the table of Contents: <a href="./../../Tutorials/Getting-Started/00-Getting-Started-With-SC.html">00. Getting Started With SC</a><div class='doclink'>helpfile source: <a href='file:///Applications/SuperCollider.app/Contents/Resources/HelpSource/Tutorials/Getting-Started/02-First-Steps.schelp'>/Applications/SuperCollider.app/Contents/Resources/HelpSource/Tutorials/Getting-Started/02-First-Steps.schelp</a><br>link::Tutorials/Getting-Started/02-First-Steps::<br></div></div><script src='./../../editor.js' type='text/javascript'></script>
</body></html>