[Go to “teaching” in Morimoto Lab]

Game by ball 3

Attention!
Find your favorite sound file in advance and bring it to this lecture.
Also, a microphone and earphones required.

そもそも音とはなんでしょう? 音の基礎や応用的事例については以下に記載しています。 Sound

Now you have implemented gravity with many balls.
Based on that, we use a sound to control gravity.


Import sound library & use your mic

From your processing menu, please select as below.
First step:

  1. “Sketch”

  2. “Import library “

  3. (if you have “Sound” in the list, go to the Second step)

  4. Input “sound” in the search window

  5. Select “Sound” in the result list (with an official mark in the author field)

  6. Push “Install” button

Second step:

  1. “Sketch”

  2. “Import library”

  3. “Sound”

Then you will have a line like the below on the top of your code.

import processing.sound.*;

After first step:
You can see the sample file of “AudioInput” from file > sample.

Alt text

If you run the sample, you will see a ellipse which size is changing by the volume of srroundings & your mouse position on y-axis.
Try to hit on where near by a mic of your pc.

Alt text

Let’s move back to your ball program after you input the below line:

import processing.sound.*;

Copy some below 5 lines (with //copy 1~5) from the sample.

import processing.sound.*;
AudioIn input;//copy 1
Amplitude rms;//copy 2
int scale=1;
void setup() {
size(640,360);
background(255);
//Create an Audio input and grab the 1st channel
input = new AudioIn(this, 0);//copy
// start the Audio Input
input.start();//copy 3
// create a new Amplitude analyzer
rms = new Amplitude(this);//copy 4
// Patch the input to an volume analyzer
rms.input(input);//copy 5
}
void draw() {
background(125,255,125);
// adjust the volume of the audio input
input.amp(map(mouseY, 0, height, 0.0, 1.0));
// rms.analyze() return a value between 0 and 1. To adjust
// the scaling and mapping of an ellipse we scale from 0 to 0.5
scale=int(map(rms.analyze(), 0, 0.5, 1, 350));
noStroke();
fill(255,0,150);
// We draw an ellispe coupled to the audio analysis
ellipse(width/2, height/2, 1*scale, 1*scale);
}

Then put the below lines in the draw(){} in your program.

if(rms.analyze() > 0.2){
print("★");
}
println(rms.analyze());//0~0.5

Amplitude is volume of sound.
rms.analyze() gives the volume of sound at each frame.
If you make larger sound than 0.2, something happens★

Let’s program so that a loud noise reverses gravity (see the below).

Alt text

Alt text

Sometimes clapping your hands doesn’t reverse gravity? :
because it reverse gravity twice (or even numbered time).
The below is an example of the result of “print(“★”)” of one hand clap. 
★★★★★★★★
It means that one hand clap reverses gravity 8 times.
As a result, nothing happens.

これを避けるためには、短い時間の連続判定は2回目以降は考慮しない、などの処理が考えられる。
時間を考慮する方法はいくつかあるが、例えばdraw内で1ずつ増える変数を用意すると、開始から経過したフレーム数とすることができる。
更に、手を叩いたときにその変数を0にリセットすれば、最後に判定されたときからのフレーム数を取得でき、それが適当な値より大きいときに判定が連続していないことがわかる。
興味のある人は実装してみてください。

In the similar way, you can change gravity by mouse and keyboard input:
[mouseClicked() in the official web]
[mousePressed() in the official web]
[mouseReleased() in the official web]
There are also mouseMoved(), mouseDragged(), mouseWheel(), mouseButton.

[Recommended page of keyPressed]
[keyPressed() in the official web]

Ex A. Change gravity by sound and mouse.

Ex B. Change something by keyboard interaction.

The below code is a simple example of keyboard input.

void keyPressed(){
if(key=='a' || key=='A'){
print(0);
}
if(key=='s' || key=='S'){
print(1);
}
}

Make sound using a sound file.

Make sound when you clap your hands.
The good sample codes to play sounds are SimplePlayback and Keyboard.

Alt text

1. Get sound file from free web site.
[Free sound files download]

上記URLは2021/4/6現在、音ファイルのダウンロードが九大ネットワークでは許可されていませんが、本日許可依頼を出しています。
もし使えなさそうなら、下記のProselfよりDLしてください。
(パスワードはコース名2文字)
https://storage.design.kyushu-u.ac.jp/public/bMEwgAqOVgmARMQBhvt4j0WliuDY8iO21v75IZrvuXys
この他、事前に家のネットなどでDLしておいたフリー素材などを持参していただいても構いません。

2. Put these lines.

Below should be at the place of global variables (グローバル変数、大域変数).
場所はsetupより上のファイルの最初のところです。

SoundFile sound;
//AudioDevice device;//seems not always necessary

Below sould be in the setup().

//device = new AudioDevice(this, 48000, 32);
sound = new SoundFile(this, "mariocoin.wav");

Below should be used when something is happened (a loud sound occurs, mouse click, a collision occurs …).

sound.play();

Warning!
If you make sounds with collisions of balls, the number of the balls should be two or three.
Otherwise, it would be very noisy.
Be careful to adjust the volume of your pc.

Alt text

Ex C. Play your favorite sound file by adjusting parameters of play(rate, amplitude).

[play() in the official web]
In the sample code of Keyboard, there are play(rate, amplitude).

Ex D. Make sounds with your ball program.

For example, make sound when you clap your hands.

Ex E ビジュアルに変化のあるサウンドを用いたアプリを作ってみましょう.

キーボードやマウスクリックその他によって音が出たり、見た目が変化するプログラムを作ってみましょう。
色々考えられますが、いくつか参考資料を挙げます↓

物理的なボールのシミュレーションと組み合わせたゲームはたくさんあります。
異例の大ヒット  :すいかゲーム🍉
おすすめ知育ゲーム:SUM!プラネット🌎

ボールの動きを反復的にし(1方向に限定する、重力をなくす、など)、
壁に当たった時や、ある位置を通った時に指定した音が再生されるようにすると、ミニマル音楽生成アプリになりそうです(以下の例は更に非常に複雑ですが)。
例:岩井俊夫さんの「テノリオン」

同じ岩井さんの例で、「音を作るプランクトン」というコンセプトのゲームも参考になるかもしれません。
今までに教えた技術ではこれを再現するのはまだ難しいかもしれませんが…。
例:岩井俊夫さんと任天堂の「エレクトロプラクトン」

以下の例のリズムシさんみたいなものを作ってみてもいいと思います。
是非、録音して音素材を用意するところからやってみた例を見てみたいです(笑)
例:リズムシさんがラップする!!「ラップムシ」

アレンジしてできる範囲でやれることを考えるというのも大事です!