{"id":1281,"date":"2018-02-24T23:26:30","date_gmt":"2018-02-24T22:26:30","guid":{"rendered":"http:\/\/yarogniew.net\/arduino\/?page_id=1281"},"modified":"2018-02-25T23:17:37","modified_gmt":"2018-02-25T22:17:37","slug":"processing-sound-library","status":"publish","type":"page","link":"https:\/\/arduino.net.pl\/index.php\/processing\/processing-sound-library\/","title":{"rendered":"Processing &#8211; sound"},"content":{"rendered":"<p>\u22a5 \u0179r\u00f3d\u0142o: <a href=\"https:\/\/processing.org\/tutorials\/sound\/\" target=\"_blank\" rel=\"noopener\">Sound, R. Luke DuBois and Wilm Thoben<\/a><\/p>\n<hr \/>\n<pre class=\"brush: plain; light: false; title: Kod:; toolbar: true; notranslate\" title=\"Kod:\">\r\n\/**\r\n * Processing Sound Library, Example 1\r\n * \r\n * Five sine waves are layered to construct a cluster of frequencies. \r\n * This method is called additive synthesis. Use the mouse position \r\n * inside the display window to detune the cluster.\r\n *\/\r\n\r\nimport processing.sound.*;\r\n\r\nSinOsc&#x5B;] sineWaves; \/\/ Array of sines\r\nfloat&#x5B;] sineFreq; \/\/ Array of frequencies\r\nint numSines = 5; \/\/ Number of oscillators to use\r\n\r\nvoid setup() {  \r\n  size(640, 360);\r\n  background(255);\r\n\r\n  sineWaves = new SinOsc&#x5B;numSines]; \/\/ Initialize the oscillators\r\n  sineFreq = new float&#x5B;numSines]; \/\/ Initialize array for Frequencies\r\n\r\n  for (int i = 0; i &amp;lt; numSines; i++) {\r\n    \/\/ Calculate the amplitude for each oscillator\r\n    float sineVolume = (1.0 \/ numSines) \/ (i + 1);\r\n    \/\/ Create the oscillators\r\n    sineWaves&#x5B;i] = new SinOsc(this);\r\n    \/\/ Start Oscillators\r\n    sineWaves&#x5B;i].play();\r\n    \/\/ Set the amplitudes for all oscillators\r\n    sineWaves&#x5B;i].amp(sineVolume);\r\n  }\r\n}\r\n\r\nvoid draw() {\r\n  \/\/Map mouseY from 0 to 1\r\n  float yoffset = map(mouseY, 0, height, 0, 1);\r\n  \/\/Map mouseY logarithmically to 150 - 1150 to create a base frequency range\r\n  float frequency = pow(1000, yoffset) + 150;\r\n  \/\/Use mouseX mapped from -0.5 to 0.5 as a detune argument\r\n  float detune = map(mouseX, 0, width, -0.5, 0.5);\r\n\r\n  for (int i = 0; i &amp;lt; numSines; i++) { \r\n    sineFreq&#x5B;i] = frequency * (i + 1 * detune);\r\n    \/\/ Set the frequencies for all oscillators\r\n    sineWaves&#x5B;i].freq(sineFreq&#x5B;i]);\r\n  }\r\n}\r\n \r\n<\/pre>\n<hr \/>\n<p>Ten skecz odgrywa losowo pi\u0119\u0107 r\u00f3\u017cnych sampli, kt\u00f3re wcze\u015bniej musimy za\u0142adowa\u0107 jako pliki o nazwach <em>1.aiff, 2.aiff&#8230;, 5.aiff<\/em>. (Wystarczy upu\u015bci\u0107 nad oknem programu). Zamieni\u0142em w szkicu <em>aif<\/em> na <em>aiff<\/em>, bo sample zwykle zapisywane s\u0105 w ten spos\u00f3b.\u00a0Dodatkowo program wy\u015bwietla losowo r\u00f3\u017cnobarwne prostok\u0105ty. Sample maj\u0105 r\u00f3\u017cne oktawy. R\u00f3wnie\u017c losowo przydzielane jak i ilo\u015b\u0107 wsp\u00f3\u0142brzmi\u0105cych sampli.\u00a0Efekt ciekawy.<\/p>\n<pre class=\"brush: plain; light: false; title: Kod:; toolbar: true; notranslate\" title=\"Kod:\"> \r\n\r\n\/**\r\n * Processing Sound Library, Example 3\r\n * \r\n * This example shows how to make a simple sampler and sequencer \r\n * with the Sound library. In this sketch, five different samples are \r\n * loaded and played back at different pitches, in this case five \r\n * different octaves. The sequencer triggers an event every 200-1000 \r\n * milliseconds randomly. Each time a sound is played a colored \r\n * rect with a random color is displayed.\r\n *\/\r\n\r\nimport processing.sound.*;\r\n\r\nSoundFile&#x5B;] files;\r\n\r\n\/\/ Create an array of values which represent the octaves. \r\n\/\/ 1.0 is playback at normal speed, 0.5 is half and therefore \r\n\/\/ one octave down. 2.0 is double so one octave up.\r\nfloat&#x5B;] octave = { \r\n  0.25, 0.5, 1.0, 2.0, 4.0\r\n};\r\n\r\n\/\/ The playSound array is defining how many samples will be \r\n\/\/ played at each trigger event\r\nint&#x5B;] playSound = { \r\n  1, 1, 1, 1, 1\r\n};\r\n\r\n\/\/ The trigger is an integer number in milliseconds so we \r\n\/\/ can schedule new events in the draw loop\r\nint trigger=0;\r\n\r\n\/\/ This array holds the pixel positions of the rectangles \r\n\/\/ that are drawn each event\r\nint&#x5B;] posx = {\r\n  0, 128, 256, 384, 512\r\n};\r\n\r\n\r\nvoid setup() {\r\n  size(640, 360);\r\n  background(255);\r\n\r\n  \/\/ Create an array of 5 empty soundfiles\r\n  files = new SoundFile&#x5B;5];\r\n\r\n  \/\/ Load 5 soundfiles from a folder in a for loop. By naming \r\n  \/\/ the files 1., 2., 3., &#x5B;...], n.aif it is easy to iterate \r\n  \/\/ through the folder and load all files in one line of code.\r\n  for (int i = 0; i &amp;lt; files.length; i++) { files&#x5B;i] = new SoundFile(this, (i+1) + &quot;.aiff&quot;); } } void draw() { \/\/ If the determined trigger moment in time matches up with \/\/ the computer clock events get triggered. if (millis() &amp;gt; trigger) {\r\n\r\n    \/\/ Redraw the background every time to erase old rects\r\n    background(0);\r\n\r\n    \/\/ By iterating through the playSound array we check for \r\n    \/\/ 1 or 0, 1 plays a sound and draws a rect, for 0 nothing happens\r\n\r\n    for (int i = 0; i &amp;lt; files.length; i++) {      \r\n      \/\/ Check which indexes are 1 and 0.\r\n      if (playSound&#x5B;i] == 1) {\r\n        float rate;\r\n        \/\/ Choose a random color and get set to noStroke()\r\n        fill(int(random(0,255)),int(random(0,255)),int(random(0,255)));\r\n        noStroke();\r\n        \/\/ Draw the rect in the positions we defined earlier in posx\r\n        rect(posx&#x5B;i], 50, 128, 260);\r\n        \/\/ Choose a random index of the octave array\r\n        rate = octave&#x5B;int(random(0, 5))];\r\n        \/\/ Play the soundfile from the array with the respective \r\n        \/\/ rate and loop set to false\r\n        files&#x5B;i].play(rate, 1.0);\r\n      }\r\n\r\n      \/\/ Renew the indexes of playSound so that at the next event \r\n      \/\/ the order is different and randomized.\r\n      playSound&#x5B;i] = int(random(0, 4));\r\n    }\r\n\r\n    \/\/ Create a new triggertime in the future, with a random offset \r\n    \/\/ between 200 and 1000 milliseconds\r\n    trigger = millis() + int(random(200, 1000));\r\n  }\r\n}<\/pre>\n<hr \/>\n<pre class=\"brush: plain; light: false; title: Kod:; toolbar: true; notranslate\" title=\"Kod:\">\r\n\/**\r\n * Processing Sound Library, Example 4\r\n * \r\n * Five soundfiles are mapped to the numbers on the keyboard. \r\n * Numbers 1-5 are played back an octave lower and numbers\r\n * 6-0 an octave higher.\r\n *\/\r\n\r\nimport processing.sound.*;\r\n\r\nAudioDevice device;\r\nSoundFile&#x5B;] file;\r\n\r\nint red, green, blue;\r\n\r\nvoid setup() {\r\n  \/\/size(1920, 1080);\r\n  size(640, 320);\r\n  background(255);\r\n\r\n  \/\/ Create an AudioDevice with low buffer size \r\n  \/\/ and create an array containing 5 empty soundfiles\r\n  device = new AudioDevice(this, 44100, 32);\r\n  file = new SoundFile&#x5B;5];\r\n\r\n  \/\/ Load 5 soundfiles from a folder in a for loop. \r\n  for (int i = 0; i &lt; file.length; i++) {\r\n    file&#x5B;i] = new SoundFile(this, (i+1) + &quot;.aiff&quot;);\r\n  }\r\n}\r\n\r\nvoid draw() {\r\n  background(red, green, blue);\r\n}\r\n\r\nvoid keyPressed() {\r\n  \/\/ Set a random background color each time you hit then number keys\r\n  red=int(random(255));\r\n  green=int(random(255));\r\n  blue=int(random(255));\r\n\r\n  \/\/ Assign a sound to each number on your keyboard. 1-5 play at\r\n  \/\/ an octave below the original pitch of the file, 6-0 play at\r\n  \/\/ an octave above.\r\n  switch(key) {\r\n  case '1':\r\n    file&#x5B;0].play(0.5, 1.0);\r\n    break;\r\n  case '2':\r\n    file&#x5B;1].play(0.5, 1.0);\r\n    break;\r\n  case '3':\r\n    file&#x5B;2].play(0.5, 1.0);\r\n    break;\r\n  case '4':\r\n    file&#x5B;3].play(0.5, 1.0);\r\n    break;\r\n  case '5':\r\n    file&#x5B;4].play(0.5, 1.0);\r\n    break;\r\n  case '6':\r\n    file&#x5B;0].play(2.0, 1.0);\r\n    break;\r\n  case '7':\r\n    file&#x5B;1].play(2.0, 1.0);\r\n    break;\r\n  case '8':\r\n    file&#x5B;2].play(2.0, 1.0);\r\n    break;\r\n  case '9':\r\n    file&#x5B;3].play(2.0, 1.0);\r\n    break;\r\n  case '0':\r\n    file&#x5B;4].play(2.0, 1.0);\r\n    break;\r\n  }\r\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u22a5 \u0179r\u00f3d\u0142o: Sound, R. Luke DuBois and Wilm Thoben \/** * Processing Sound Library, Example 1 * * Five sine waves are layered to construct a cluster of frequencies. *&#8230;<\/p>\n","protected":false},"author":3,"featured_media":0,"parent":1256,"menu_order":1,"comment_status":"closed","ping_status":"closed","template":"","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"class_list":["post-1281","page","type-page","status-publish","hentry"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/arduino.net.pl\/index.php\/wp-json\/wp\/v2\/pages\/1281","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/arduino.net.pl\/index.php\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/arduino.net.pl\/index.php\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/arduino.net.pl\/index.php\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/arduino.net.pl\/index.php\/wp-json\/wp\/v2\/comments?post=1281"}],"version-history":[{"count":7,"href":"https:\/\/arduino.net.pl\/index.php\/wp-json\/wp\/v2\/pages\/1281\/revisions"}],"predecessor-version":[{"id":1295,"href":"https:\/\/arduino.net.pl\/index.php\/wp-json\/wp\/v2\/pages\/1281\/revisions\/1295"}],"up":[{"embeddable":true,"href":"https:\/\/arduino.net.pl\/index.php\/wp-json\/wp\/v2\/pages\/1256"}],"wp:attachment":[{"href":"https:\/\/arduino.net.pl\/index.php\/wp-json\/wp\/v2\/media?parent=1281"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}