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

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

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

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

Q&A

解決済

1回答

4030閲覧

pythonで音を出したいが音が出ない

Yuth

総合スコア9

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

0グッド

0クリップ

投稿2019/09/29 10:29

編集2019/09/29 10:30

#コード

import platform
import random
from time import sleep # sleep() 呼び出し用

if platform.system() == 'Windows' :
import winsound
else :
import os

freq = (260, 294, 330, 349, 392, 440, 494, 523)
note = ('ド','レ','ミ','ファ','ソ','ラ','シ','ド')

def playnote(n) :
if platform.system() == 'Windows' :
winsound.Beep(freq[n], 1000)
else :
os.system('play -n synth 1 sin %s > /dev/null 2>&1' % (freq[n]))

def play(i, n) :
print('基準となるドを鳴らします。')
playnote(0)

sleep(0.5) # 0.5秒待つ c = random.randint(1, 7) playnote(c) x = input('なった音は?(ド,レ,ミ...)==> ') if (note[c] == x): print('正解') n = n + 1 else : print('間違い:正解は', note[c]) return n

point = 0
random.seed()

for i in range(10): # 10回繰り返す
point = play(i, point)

print('正解率は : ' + str(point * 10.0) + '%')

#実行結果
このコードを実行した結果プログラム自体は作動するのですが音が出ません。
VScodeのターミナルで実行しています。
Macbook なのですが環境に問題があるのかなにが問題なのかがわかりません。

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

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

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

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

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

mushroominger

2019/09/29 10:43

コードはコードの挿入内に書くとインデント間違いなどの指摘がもらえるかもしれません インデントし直して僕のMacでも動作させてみましたが音は出ませんでしたね
Yuth

2019/09/29 10:47

やはり音は出ないんですね、、、。試していただきありがとうございます。
guest

回答1

0

ベストアンサー

https://teratail.com/questions/214355
先の質問で回答されているsoxをインストールしたら音が出ましたよ

homebrewのインストールをしてから先の質問のインストールをしてください
https://brew.sh/index_ja

投稿2019/09/29 11:15

mushroominger

総合スコア133

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

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

Yuth

2019/09/29 11:28

インストールした結果以下の文章が出てきました。 ==> This script will install: /usr/local/bin/brew /usr/local/share/doc/homebrew /usr/local/share/man/man1/brew.1 /usr/local/share/zsh/site-functions/_brew /usr/local/etc/bash_completion.d/brew /usr/local/Homebrew ==> The following existing directories will be made group writable: /usr/local/bin ==> The following existing directories will have their owner set to apple: /usr/local/bin ==> The following existing directories will have their group set to admin: /usr/local/bin ==> The following new directories will be created: /usr/local/etc /usr/local/include /usr/local/lib /usr/local/sbin /usr/local/share /usr/local/var /usr/local/opt /usr/local/share/zsh /usr/local/share/zsh/site-functions /usr/local/var/homebrew /usr/local/var/homebrew/linked /usr/local/Cellar /usr/local/Caskroom /usr/local/Homebrew /usr/local/Frameworks Press RETURN to continue or any other key to abort appurunoMacBook-Pro:chapter04 apple$ python3 beep0.py sh: play: command not found またインストール後にプログラムを呼び出したところplay command not found と出てしまいました。。。
Yuth

2019/09/29 11:29

この後にSOXのインストールが必要であるということでしょうか。。
mushroominger

2019/09/29 11:31

homebrew だけではダメですよ その後にhomebrewを使用してsoxをインストールしてください
Yuth

2019/09/29 11:38

このhomebrew 実行後になにをすればsoxをインストールできますか?
mushroominger

2019/09/29 11:41

$ brew install sox でsoxがインストールできませんかね?
Yuth

2019/09/29 11:43

==> This script will install: /usr/local/bin/brew /usr/local/share/doc/homebrew /usr/local/share/man/man1/brew.1 /usr/local/share/zsh/site-functions/_brew /usr/local/etc/bash_completion.d/brew /usr/local/Homebrew ==> The following existing directories will be made group writable: /usr/local/bin ==> The following existing directories will have their owner set to apple: /usr/local/bin ==> The following existing directories will have their group set to admin: /usr/local/bin ==> The following new directories will be created: /usr/local/etc /usr/local/include /usr/local/lib /usr/local/sbin /usr/local/share /usr/local/var /usr/local/opt /usr/local/share/zsh /usr/local/share/zsh/site-functions /usr/local/var/homebrew /usr/local/var/homebrew/linked /usr/local/Cellar /usr/local/Caskroom /usr/local/Homebrew /usr/local/Frameworks Press RETURN to continue or any other key to abort appurunoMacBook-Pro:chapter04 apple$ brew install sox bash: brew: command not found  こうなってしまいます????
Yuth

2019/09/29 11:48

returnを押すとpasswordを求められるのですが、、、
Yuth

2019/09/29 11:50

Press RETURN to continue or any other key to abort ==> /usr/bin/sudo /bin/chmod u+rwx /usr/local/bin Password:
mushroominger

2019/09/29 11:51

英語さっぱりなのでお手上げ状態です パスワード入れてみてください
mushroominger

2019/09/29 11:54 編集

危険ですねお手上げです本当に…
Yuth

2019/09/29 11:57

passwordの後に文字を入力しようとしても文字が表示されません。。。 1回間違えるともう一度やり直すようにされ、3回エンターでスキップすると最初に戻ります。
Yuth

2019/09/29 12:00

対応有難うございました。????
Yuth

2019/09/29 12:05

installできました!
mushroominger

2019/09/29 12:05

おめでとうございますぅー
Yuth

2019/09/29 12:08

音も出ました!!本当に有難うございます!!!!
mushroominger

2019/09/29 12:09

ますますおめでとうございます よかったです
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.49%

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

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

質問する

関連した質問