-
Notifications
You must be signed in to change notification settings - Fork 0
/
sonoheadphone.py
73 lines (64 loc) · 2 KB
/
sonoheadphone.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
from __future__ import division
import math
from easygui import *
import pyaudio as PyAudio # sudo apt-get install python{,3}-pyaudio
from itertools import izip
from time import sleep
import sys
from threading import Thread,Event
import audioop
import struct
from array import array
import pygame
from pygame.locals import *
from pygame.mixer import Sound, get_init, pre_init
class Note(Sound):
def __init__(self, frequency, volume=.1):
self.frequency = frequency
Sound.__init__(self, self.build_samples())
self.set_volume(volume)
def build_samples(self):
period = int(round(get_init()[0] / self.frequency))
samples = array("h", [0] * period)
amplitude = 2 ** (abs(get_init()[1]) - 1) - 1
for time in xrange(period):
if time < period / 2:
samples[time] = amplitude
else:
samples[time] = -amplitude
return samples
chunk = 2048
CHANNELS = 1
RECORD_SECONDS = 0.1
frequency = 23000
p = PyAudio.PyAudio()
stream = p.open(format=p.get_format_from_width(1), # 8bit
channels=1, # mono
rate=96000,
input=True,
output=False,
frames_per_buffer=chunk)
pre_init(96000, -16, 1, 1024)
pygame.init()
best_frequency = 0
amplitude = 0
#msgbox(msg="First, I need to tune.", title="", ok_button="Begin!")
for frequency in range(22700,23500,5):
data=stream.read(6048)
Note(frequency).stop()
Note(frequency).play(-1)
sleep(0.05)
print(audioop.maxpp(data,2))
if(audioop.maxpp(data,2) > amplitude):
amplitude = audioop.maxpp(data,2)
best_frequency = frequency
frequency = best_frequency
Note(frequency,0.8).play(-1)
print(frequency)
msgbox(msg="Now, inject a bubble.", title="", ok_button="Done!")
volume = 0.1
while(True):
Note(frequency,volume).play(-1)
volume = volume+0.05
sleep(0.1)
msgbox('Lit')