質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Processing

Processingは、オープンソースプロジェクトによるCGのためのプログラミング言語です。Javaをベースにしており、グラフィック機能に特化しています。イメージの生成やアニメーションなど、視覚的なフィードバックを簡単に得ることが可能です。

Q&A

解決済

2回答

2063閲覧

processingのmp3の再生方法について

bump_of_pool

総合スコア7

Processing

Processingは、オープンソースプロジェクトによるCGのためのプログラミング言語です。Javaをベースにしており、グラフィック機能に特化しています。イメージの生成やアニメーションなど、視覚的なフィードバックを簡単に得ることが可能です。

0グッド

0クリップ

投稿2020/01/07 15:55

以前質問をさせていただいてprocessingでの鍵盤の作り方を教えていただきました。そこにmp3ファイルの再生をしようと考えています。再生だけはできたのですが、何かキーを押して再生や停止をすることは可能でしょうか?また、それと同時にレコードのようなものを画面に映し出し、mp3ファイルが再生されると回転し、停止すると回転が止まるようなプログラムは作れるのでしょうか。わかる方がいらっしゃいましたら教えていただきたいです。

import ddf.minim.spi.;
import ddf.minim.signals.
;
import ddf.minim.;
import ddf.minim.analysis.
;
import ddf.minim.ugens.;
import ddf.minim.effects.
;

Minim minim;
AudioOutput out;
AudioPlayer player;
HashMap<String, Keyboard> pressedKeys = new HashMap<String, Keyboard>();

void setup() {
size(600, 400);
minim = new Minim(this);
player = minim.loadFile("y2mate.com - flamingo_OjdMFqnSybw_320kbps.mp3");
player.play();
out = minim.getLineOut();
}

void draw() {
}

void keyPressed() {
String pitchName = getPitchName();
if (pitchName == null) return;

Keyboard k = pressedKeys.get(pitchName);
if (k == null) {
k = new Keyboard(pitchName);
pressedKeys.put(pitchName, k);
}

k.noteOn();
}

void keyReleased() {
String pitchName = getPitchName();
if (pitchName == null) return;

pressedKeys.get(pitchName).noteOff();
}

String getPitchName() {
switch (key) {
case '1': return "G3";
case '2': return "A3";
case '3': return "A#3";
case '4': return "D4";
case '5': return "A#3";
case '6': return "C4";
case '7': return "A3";
case '8': return "A#3";
case '9': return "A3";
case '0': return "G3";
case 'q': return "G3";
case 'w': return "A3";
case 'e': return "A#3";
case 'r': return "F3";
case 't': return "D3";
case 'y': return "D4";
case 'u': return "A#3";
case 'i': return "C4";
case 'o': return "A3";
case 'p': return "A#3";
case '@': return "A3";
case '[': return "G3";

case 'a': return "C3"; case 's': return "D3"; case 'd': return "E3"; case 'f': return "F3"; case 'g': return "G3"; case 'h': return "A3"; case 'j': return "B3"; case 'k': return "C4"; case 'l': return "D4"; case ';': return "E4"; case ':': return "F4"; case ']': return "G4"; case 'z': return "C#3"; case 'x': return "D#3"; case 'v': return "F#3"; case 'b': return "G#3"; case 'n': return "A#3"; case ',': return "C#4"; case '.': return "D#4";

}

return null;
}

class Keyboard {
Oscil osc;
boolean pressed;

Keyboard(String pitchName) {
osc = new Oscil(Frequency.ofPitch(pitchName), 0.1, Waves.SAW);
}

void noteOn() {
if (pressed) return;

osc.patch(out); pressed = true;

}

void noteOff() {
osc.unpatch(out);
pressed = false;
}
}

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答2

0

mp3の再生はsoundライブラリ
キーを押したらxxはkeyPressedシステム変数keyPressed関数
画を回すにはtranslate()とrotate() (もう面倒だからリファレンスページから自分でたどってみましょう。)

投稿2020/01/08 14:36

thkana

総合スコア7639

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

0

ベストアンサー

何かキーを押して再生や停止をすること

一時停止 player.pause();
再開 player.play(); or player.loop();
keyPressed()で好きなキーに割り当てればいいでしょう。

レコードのようなものを画面に映し出し

レコードっぽい画像を表示するのが手っ取り早いでしょう。
画像の表示・回転は参考ページがたくさんあります。

画像を回転するには | 自己啓発。人生について考えるの「画像中央を中心に360°回転させる例:」等がちょうどよさそうです。

mp3ファイルが再生されると回転し、停止すると回転が止まる

player.isPlaying()で今の再生状態が取れます。
draw内にあるであろう回転角度の変更部分を、再生中だけ増やすようにすればいいでしょう。


追記 参考コード
ENTERキーでトグル動作 本題でないところ全カット

Processing

1import ddf.minim.*; 2 3Minim minim; 4AudioPlayer player; 5PImage img; 6float deg; // 回転角度 7float delta = 33.3 * 360.0 / 60; // 1秒間に回る角度 8 9void setup() { 10 size(600, 400); 11 minim = new Minim(this); 12 player = minim.loadFile("おんがく.mp3"); 13 img = loadImage("れこーど.png"); 14} 15 16void draw() { 17 rotateImage((width - img.width) / 2, (height - img.height) / 2, img, deg); 18 19 if (player.isPlaying()) { 20 deg += delta / frameRate; 21 if (deg > 360) deg -= 360; 22 } 23} 24 25void keyPressed() { 26 println("keyPressed"); 27 28 if (key == ENTER || key == RETURN) { 29 if (player.isPlaying()) { 30 player.pause(); 31 } else { 32 player.loop(); 33 } 34 } 35 36 // 省略 37} 38 39//http://mslabo.sakura.ne.jp/WordPress/make/processing%E3%80%80%E9%80%86%E5%BC%95%E3%81%8D%E3%83%AA%E3%83%95%E3%82%A1%E3%83%AC%E3%83%B3%E3%82%B9/%E7%94%BB%E5%83%8F%E3%82%92%E5%9B%9E%E8%BB%A2%E3%81%99%E3%82%8B%E3%81%AB%E3%81%AF/ 40void rotateImage(int x, int y, PImage img, float deg) { 41 pushMatrix(); 42 translate(x + img.width / 2, y + img.height / 2); 43 rotate(radians(deg)); 44 imageMode(CENTER); 45 image(img, 0, 0); 46 imageMode(CORNER); 47 popMatrix(); 48}

投稿2020/01/08 09:09

編集2023/12/26 17:09
TN8001

総合スコア9321

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

bump_of_pool

2020/01/08 12:27

回答いただきありがとうございました。こちらの方法で試してみましたが、私が理解していないのもあり、うまくできませんでした。お手数ですが、具体的なプログラムを教えていただけないでしょうか?また、Enterキーを押して実行するプログラムは可能ですか?
bump_of_pool

2020/01/08 13:17

import ddf.minim.spi.*; import ddf.minim.signals.*; import ddf.minim.*; import ddf.minim.analysis.*; import ddf.minim.ugens.*; import ddf.minim.effects.*; Minim minim; AudioOutput out; AudioPlayer player; PImage img; float deg; float delta = 33.3 * 360.0 / 60; HashMap<String, Keyboard> pressedKeys = new HashMap<String, Keyboard>(); void setup() { size(600, 400); minim = new Minim(this); player = minim.loadFile("y2mate.com - flamingo_OjdMFqnSybw_320kbps.mp3"); img = loadImage("record.png"); out = minim.getLineOut(); } void draw() { rotateImage((width - img.width) / 2, (height - img.height) / 2, img, deg); if (player.isPlaying()) { deg += delta / frameRate; if (deg > 360) deg -= 360; } } void keyPressed() { println("keyPressed"); if (key == ENTER || key == RETURN) { if (player.isPlaying()) { player.pause(); } else { player.loop(); } } String pitchName = getPitchName(); if (pitchName == null) return; Keyboard k = pressedKeys.get(pitchName); if (k == null) { k = new Keyboard(pitchName); pressedKeys.put(pitchName, k); } k.noteOn(); } void keyReleased() { String pitchName = getPitchName(); if (pitchName == null) return; pressedKeys.get(pitchName).noteOff(); } String getPitchName() { switch (key) { case '1': return "G3"; case '2': return "A3"; case '3': return "A#3"; case '4': return "D4"; case '5': return "A#3"; case '6': return "C4"; case '7': return "A3"; case '8': return "A#3"; case '9': return "A3"; case '0': return "G3"; case 'q': return "G3"; case 'w': return "A3"; case 'e': return "A#3"; case 'r': return "F3"; case 't': return "D3"; case 'y': return "D4"; case 'u': return "A#3"; case 'i': return "C4"; case 'o': return "A3"; case 'p': return "A#3"; case '@': return "A3"; case '[': return "G3"; case 'a': return "C3"; case 's': return "D3"; case 'd': return "E3"; case 'f': return "F3"; case 'g': return "G3"; case 'h': return "A3"; case 'j': return "B3"; case 'k': return "C4"; case 'l': return "D4"; case ';': return "E4"; case ':': return "F4"; case ']': return "G4"; case 'z': return "C#3"; case 'x': return "D#3"; case 'v': return "F#3"; case 'b': return "G#3"; case 'n': return "A#3"; case ',': return "C#4"; case '.': return "D#4"; } return null; } class Keyboard { Oscil osc; boolean pressed; Keyboard(String pitchName) { osc = new Oscil(Frequency.ofPitch(pitchName), 0.1, Waves.SAW); } void noteOn() { if (pressed) return; osc.patch(out); pressed = true; } void noteOff() { osc.unpatch(out); pressed = false; } } void rotateImage(int x, int y, PImage img, float deg) { pushMatrix(); translate(x + img.width / 2, y + img.height / 2); rotate(radians(deg)); imageMode(CENTER); image(img, 0, 0); imageMode(CORNER); popMatrix(); } これで試してみましたが、 rotateImage((width - img.width) / 2, (height - img.height) / 2, img, deg); のところでエラーが起き、応答なしになってしまいました。
TN8001

2020/01/08 13:37

こちらではこのコードをそのまま実行できましたけどなんでしょうね?? 何かエラー文は出ていますか? いくつか画像を変えて試していただけますか?
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問