add week 9

This commit is contained in:
Tomoya Matsuura(MacBookPro) 2023-06-13 18:48:18 +09:00
parent ea1dce3f3b
commit a06880b413
50 changed files with 388 additions and 2 deletions

View File

@ -0,0 +1,44 @@
#include <MozziGuts.h>
#include <mozzi_midi.h>
#include <Oscil.h>
#include <ADSR.h>
#include <tables/sin8192_int8.h>
Oscil<8192, AUDIO_RATE> aOscil(SIN8192_DATA); //元となる音色
ADSR<AUDIO_RATE, AUDIO_RATE> envelope; //エンベロープをかけるためのクラス
unsigned int Dur, Atk, Dec, Sus, Rel; //ADSRの長さを入れておく変数
int button_prev = LOW;
void setup() {
startMozzi(1024);
Atk = 10;
Dec = 10;
Sus = 50;
Rel = 100;
envelope.setTimes(Atk, Dec, Sus, Rel);
envelope.setADLevels(255, 128);
}
void updateControl() {
auto button_state = digitalRead(2);
if (button_prev == LOW && button_state == HIGH) {
auto f = mtof((int)random(65, 100));
aOscil.setFreq(f);
envelope.noteOn();
}
if (button_prev == HIGH && button_state == LOW) {
envelope.noteOff();
}
button_prev = button_state;
}
int updateAudio() {
envelope.update();
return envelope.next() * aOscil.next() / 255;
}
void loop() {
audioHook();
}

View File

@ -0,0 +1,73 @@
#include <MozziGuts.h>
#include <mozzi_midi.h>
#include <Oscil.h>
#include <EventDelay.h>
#include <ADSR.h>
#include <tables/sin8192_int8.h>
Oscil <8192, AUDIO_RATE> aOscil(SIN8192_DATA);//元となる音色
Oscil <8192, AUDIO_RATE> aOscil2(SIN8192_DATA);//元となる音色
ADSR <AUDIO_RATE, AUDIO_RATE> envelope_A;//エンベロープをかけるためのクラス
ADSR <AUDIO_RATE, AUDIO_RATE> envelope_B;//エンベロープをかけるためのクラス
unsigned int Dur, Atk, Dec, Sus, Rel;//ADSRの長さを入れておく変数
//piano phaseのパターン
unsigned int pattern[] = {64, 66, 71, 73, 74, 66, 64, 73, 71, 66, 74, 73};
//二つのタイマーを用意
int phase_A = 0;
int phase_B = 0;
EventDelay timer_B;
EventDelay timer_A;
void setup() {
startMozzi(1024);
Atk = 10;
Dec = 10;
Sus = 50;
Rel = 100;
envelope_A.setTimes(Atk, Dec, Sus, Rel);
envelope_A.setADLevels(255, 128);
envelope_B.setTimes(Atk, Dec, Sus, Rel);
envelope_B.setADLevels(255, 128);
timer_A.set(150);
timer_B.set(151);
timer_B.start();
timer_A.start();
}
void updateControl()
{
if (timer_A.ready())
{
int note = pattern[phase_A];
phase_A = (phase_A + 1) % 12;
aOscil.setFreq(mtof(note));
envelope_A.noteOn();
timer_A.start();
}
if (timer_B.ready())
{
int note = pattern[phase_B];
phase_B = (phase_B + 1) % 12;
aOscil2.setFreq(mtof(note));
envelope_B.noteOn();
timer_B.start();
}
}
int updateAudio()
{
envelope_A.update();
envelope_B.update();
int A = (envelope_A.next() * aOscil.next()) >> 8;
int B = (envelope_B.next() * aOscil2.next()) >> 8;
return (A + B) / 2;
}
void loop() {
audioHook();
}

View File

@ -0,0 +1,22 @@
#include <MozziGuts.h>
#include <Oscil.h>
#include <tables/sin2048_int8.h>
Oscil <SIN2048_NUM_CELLS, AUDIO_RATE> aSin(SIN2048_DATA);
void setup(){
startMozzi();
aSin.setFreq(440);
}
void updateControl(){
}
int updateAudio(){
return aSin.next();
}
void loop(){
audioHook();
}

View File

@ -0,0 +1,17 @@
const int button_pin = 2;
const int sound_pin = 9;
void setup() {
pinMode(button_pin, INPUT);
//tone()を使うときは3,11以外
pinMode(sound_pin, OUTPUT);
}
void loop() {
if (digitalRead(2) == HIGH) {
tone(sound_pin, 1000);
} else {
noTone(sound_pin);
}
delay(20);
}

View File

@ -0,0 +1,32 @@
const int button_pin = 2;
const int sound_pin = 9;
bool is_playing = false;
int button_prev= LOW;
float freq = 440;
float midiToFreq(int midi){
return 440.*pow(2.,(midi-69.)/12.);
}
void setup() {
pinMode(button_pin,INPUT);
pinMode(sound_pin, OUTPUT);
}
void loop() {
auto button_state = digitalRead(button_pin);
if(button_prev == LOW && button_state==HIGH){
is_playing = !is_playing;
freq = midiToFreq(random(65,100));
}
if(is_playing){
tone(sound_pin,freq);
}else{
noTone(sound_pin);
}
delay(20);
button_prev = button_state;
}

View File

@ -0,0 +1,152 @@
---
title: Week 7
date: 2023-05-26
weight: 7
---
# 2023 Tokyo University of the Arts Art media Center「Code and Design」
## Slides (in Japanese)
{{< button href="2023-code-and-design-7.pdf">}}SlidesPDF{{< /button >}}
{{< button href="./slides">}}SlidesHTML{{< /button >}}
## Python Scripts for Mouse Automation
https://github.com/tomoyanonymous/py-serial-automouse
{{< button href="https://github.com/tomoyanonymous/py-serial-automouse/archive/refs/heads/main.zip">}} DL Zip {{< /button >}}
## Arduino Example Codes
### ADCTouch_monitor.ino
{{< button href="./ADCTouch_monitor/ADCTouch_monitor.ino">}}DL{{< /button >}}
```cpp
#include <ADCTouch.h>
void setup() {
pinMode(A0, INPUT);
pinMode(LED_BUILTIN, OUTPUT);
Serial.begin(9600);
}
void loop() {
int val = ADCTouch.read(A0, 100);
Serial.println(val);
delay(40);
}
```
### ADCTouch_minimal.ino
{{< button href="./ADCTouch_minimal/ADCTouch_minimal.ino">}}DL{{< /button >}}
```cpp
#include <ADCTouch.h>
//setup()とloop()で共通して使う値はグローバル変数(関数の外側)で定義しておく
int ref = 0;
void setup() {
pinMode(A0, INPUT);
pinMode(LED_BUILTIN, OUTPUT);
Serial.begin(9600);
//繋げた導体の状態で初期値が変わるので、それを保存しておく必要がある
ref = ADCTouch.read(A0, 100); //(使用するピン番号,平均を取るサンプル数)
}
void loop() {
int val = ADCTouch.read(A0, 100) - ref;
if (val > 40) {
digitalWrite(LED_BUILTIN, HIGH);
} else {
digitalWrite(LED_BUILTIN, LOW);
}
Serial.println(val);
delay(40);
}
```
### ADCTouch_Mouse
{{< button href="./ADCTouch_Mouse/ADCTouch_Mouse.ino">}}DL{{< /button >}}
```cpp
#include <ADCTouch.h>
const int upButton = A0;
const int downButton = A1;
const int leftButton = A2;
const int rightButton = A3;
const int mouseButton = A4;
int ref_up = 0;
int ref_down = 0;
int ref_left = 0;
int ref_right = 0;
int ref_mouse = 0;
int range = 10; // マウスカーソルの移動幅
void setup() {
pinMode(upButton, INPUT);
pinMode(downButton, INPUT);
pinMode(leftButton, INPUT);
pinMode(rightButton, INPUT);
pinMode(mouseButton, INPUT);
ref_up = ADCTouch.read(upButton, 100);
ref_down = ADCTouch.read(downButton, 100);
ref_left = ADCTouch.read(leftButton, 100);
ref_right = ADCTouch.read(rightButton, 100);
ref_mouse = ADCTouch.read(mouseButton, 100);
Serial.begin(9600);
}
//マウス移動に関する処理をひとまとめに
void moveMouse(int x, int y, int mouse_state) {
Serial.print(x);
Serial.print(" ");
Serial.print(y);
Serial.print(" ");
Serial.println(mouse_state);
}
void loop() {
// ボタンの検出
int v1 = ADCTouch.read(upButton, 100) - ref_up;
int v2 = ADCTouch.read(downButton, 100) - ref_down;
int v3 = ADCTouch.read(leftButton, 100) - ref_left;
int v4 = ADCTouch.read(rightButton, 100) - ref_right;
int v5 = ADCTouch.read(mouseButton, 100) - ref_mouse;
//40以上ならクリックしたとみなし、1を、40以下なら0を
// A ? B : C;はif (A) { return B;} else {return C;} の略記(三項演算子)
int upState = v1 > 40 ? 1 : 0;
int downState = v2 > 40 ? 1 : 0;
int rightState = v3 > 40 ? 1 : 0;
int leftState = v4 > 40 ? 1 : 0;
int clickState = v5 > 40 ? 1 : 0;
// 移動距離を算出(左右同時押しも考慮)
int xDistance = (leftState - rightState) * range;
int yDistance = (upState - downState) * range;
//移動もクリックもしてないならシリアル送信は冗長なので、しない
// &&  AND、!はNOT
if (!(xDistance == 0 && yDistance == 0 && clickState == 0)) {
moveMouse(xDistance, yDistance, clickState);
}
delay(5);
}
```
### DrunkMouse(Add Randomness)
{{< button href="./DrunkMouse/DrunkMouse.ino">}}DL{{< /button >}}
### ADCTouch_Mouse_HID.ino (Only works for Leonardo and Pro Micro)
{{< button href="./ADCTouch_Mouse_HID/ADCTouch_Mouse_HID.ino">}}DL{{< /button >}}
### ADCTouch_Mouse_scroll.ino(works only with `move_relative_scroll.command`)
{{< button href="./ADCTouch_Mouse_scroll/ADCTouch_Mouse_scroll.ino">}}DL{{< /button >}}

View File

@ -0,0 +1,26 @@
---
title: 第9週
date: 2023-06-09
weight: 9
---
# 2023年 東京藝術大学 芸術情報センター開設科目 「コードとデザイン」 第9回
## スライド
{{< button href="2023-code-and-design-9.pdf">}}スライドPDF{{< /button >}}
{{< button href="slides">}}スライドHTML{{< /button >}}
## Arduinoサンプルコード
{{< preview_code href="examples/tone_minimal/tone_minimal.ino" type= "ino">}}
{{< preview_code href="examples/tone_random_flip/tone_random_flip.ino" type= "ino">}}
{{< preview_code href="examples/mozzi_sine/mozzi_sine.ino" type= "ino">}}
{{< preview_code href="examples/mozzi_envelope/mozzi_envelope.ino" type= "ino">}}
{{< preview_code href="examples/mozzi_reich/mozzi_reich.ino" type= "ino">}}

Binary file not shown.

After

Width:  |  Height:  |  Size: 318 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 160 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 662 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 172 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 348 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 470 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 468 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 436 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 323 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 493 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 418 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 146 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 486 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 627 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 420 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 527 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 501 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 491 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 333 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 191 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 323 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 518 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 438 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 818 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 175 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 725 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 660 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 111 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 362 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 368 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 298 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 380 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 500 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 618 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 409 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 622 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 451 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 372 KiB

View File

@ -0,0 +1,10 @@
---
title: "Code and Design 2023 class 9 Slides"
date: 2023-06-09
bookHidden: true
---
# Slides
{{< slides_jpg >}}

View File

@ -0,0 +1,10 @@
---
title: "コードとデザイン 2023年 第9回 スライド"
date: 2023-06-09
bookHidden: true
---
# スライド
{{< slides_jpg >}}

View File

@ -41,7 +41,7 @@ In addition, by learning how to convert data and materials into each other throu
6. [(5/19) Software and Algorithm - Let's try using Arduino](./6)
7. [(5/26) Considering Inputs (Make Unreasonable Mouse)](./7)
8. [(6/2) Considering Inputs 2 - Combining Processing and Arduino, OOP (PONG for One)](./8)
9. (6/9) Cartesian Robot and Digital Fabrication (Plotter, Laser Cutter)
9. [(6/9) Considering Outputs - Generating Sound with Arduino](./9)
10. (6/16) Cartesian Robot and Digital Fabrication (3D Printer and G-code Hack)
11. (6/23) Studio Work 1. Consultation
12. (6/30) Studio Work 2. Work

View File

@ -42,7 +42,7 @@ bookCollapseSection: true
6. [(5/19) ソフトウェアとアルゴリズムーArduinoを使ってみよう](./6)
7. [(5/26) 入力を考える(不条理なマウスを作る)](./7)
8. [(6/2) 入力を考える2 - ProcessingとArduinoの連携、オブジェクト指向プログラミング一人用PONG](./8)
9. (6/9) Cartesian Robotとデジタルファブリケーションプロッター、レーザーカッター
9. [(6/9) 出力を考える - Arduinoでの音声プログラミング](./9)
10. (6/16) Cartesian Robotとデジタルファブリケーション (3DプリンターとGコードハック)
11. (6/23) 制作①相談
12. (6/30) 制作②作業