In [1]:
from ipywidgets import interact, interactive, fixed, interact_manual
import ipywidgets as widgets
from gtts import gTTS
import matplotlib
import numpy
from traitlets.config.manager import BaseJSONConfigManager
path = "/Users/Kristen/anaconda3/envs/py36/etc/jupyter/nbconfig"
cm = BaseJSONConfigManager(config_dir=path)
cm.update("livereveal", {"autolaunch": True,
"theme": "sky",
}
)
#Supress default INFO logging
# The UT Dallas Art Science Lab Training module
print ("Sonification Training Module v0.5.2")
print("\nDeveloped by the ArtSciLab at UT Dallas")
print("\n")
print("Full or Empty Testing Module" )
print("\n")
print("\nBasic Instructions for each cell :")
print('1. Press Shift + Enter to initiate display')
print("2. Press Space Bar to proceed to the next cell")
print("\n")
print("Volume controls are located in the top row of the keyboard if you need to adjust the volume at any time")
In [1]:
import random
import time
from IPython.display import Image, display, clear_output
from ipywidgets import Button, HBox, VBox,Layout
from ipywidgets import widgets
from ipywidgets import interact, interactive, fixed, interact_manual
from gtts import gTTS
import os
import numpy
import ctcsound
import platform
import os
speechflag = 0
if (platform.system()=='Windows'):
speechflag = 2
if (platform.system()!='Windows'):
speechflag = 1
print("Press Shift + Enter to activate cell")
print ("\nListen to the Sonification, then set the Slider to what value you think it represents.")
print("Pay close attention to the timbre and acoustic resonance.")
print("You can listen to the sonification as many times as you need to.")
print("\nWhen you are sure of your answer, click Submit Response once and wait for the next trial to load.")
print("You will undergo 5 trials.")
count=0
accuracy =0
pan = 0
user_input=0
cs = ctcsound.Csound()
index = 0
csd = '''
<CsoundSynthesizer>
<CsOptions>
-o dac
</CsOptions>
<CsInstruments>
;
sr = 44100
ksmps = 32
nchnls = 2
0dbfs = 1
giSine ftgen 0, 0, 2^10, 10, 1
instr 1 ;master instrument
inumparts = p4 ;number of partials
ibasfreq = 200 ;base frequency
ipart = 1 ;count variable for loop
;loop for inumparts over the ipart variable
;and trigger inumpartss instanes of the subinstrument
loop:
ifreq = ibasfreq * ipart
iamp = 1/ipart/inumparts
event_i "i", 10, 0, p3, ifreq, iamp
loop_le ipart, 1, inumparts, loop
endin
instr 10 ;subinstrument for playing one partial
ifreq = p4 ;frequency of this partial
iamp = p5 ;amplitude of this partial
aenv transeg 0, .01, 0, iamp, p3-0.1, -10, 0
apart poscil aenv, ifreq, giSine
outs apart, apart
endin
</CsInstruments>
<CsScore>
f 0 14400
f 1 0 1024 10 1
</CsScore>
</CsoundSynthesizer>
'''
try:
pt
except NameError:
var_exists = False
else:
pt.stop()
pt.join()
time.sleep(2)
cs.compileCsdText(csd)
cs.start()
pt = ctcsound.CsoundPerformanceThread(cs.csound())
pt.play()
def f(percentage):
global user_input
user_input = percentage
def redraw():
global index
global accuracy
sonibutton = widgets.Button(description = 'Listen to Sonification')
answerbutton = widgets.Button(description='Submit Response')
choices = random.sample(range(100), 4)
choices = list(map(str, choices))
correct = random.choice(choices)
index = int(correct)
#display(Image(correct))
#display(correct)
time.sleep(0.5)
#display(button)
#button.on_click(on_button_clicked)
#buttons = [widgets.Button(description = choice) for choice in choices]
#sonibutton = [widgets.Button(description = 'Listen to Sonification')]
interact (f, percentage=(0,100,1))
#answerbutton = [widgets.Button(description='Submit Input')]
#container = widgets.HBox(children=buttons)
left_box = VBox([(sonibutton)])
right_box = VBox([(answerbutton)])
#HBox([left_box, right_box])
container = widgets.HBox([left_box,right_box])
print("Trial " + str(count+1))
display(container)
def ans_button_clicked(b):
global count
global accuracy
count = count + 1
tts = gTTS(text='Input Submitted', lang='en')
tts.save("answer.mp3")
if (speechflag==1):
os.system("afplay answer.mp3")
if (speechflag==2):
os.system("cmdmp3 answer.mp3")
text=list()
text.append(index)
text.append(user_input)
text.append(index-user_input)
accuracy = accuracy + abs(index -user_input)
with open('drop_responses.csv','a') as file:
file.write('\n')
for line in text:
file.write(str(line))
file.write(',')
time.sleep(2)
container.close()
clear_output()
if count <5:
redraw()
if count == 5:
msg = widgets.Button(description = 'Thank you for finishing this module',layout=Layout(width='50%', height='80px'))
display(msg)
print("Your accuracy of response is " + str(100-(accuracy/5)) + "%")
pt.stop()
pt.join()
file.close()
def son_button_clicked(b):
#tts = gTTS(text='Playing Sonification', lang='en')
#tts.save("answer.mp3")
#os.system("afplay answer.mp3")
in_min = 0
in_max = 100
out_min=0
out_max = 20
global index
freq = (index - in_min) * (out_max - out_min) / (in_max - in_min) + out_min
#print(freq)
pt.scoreEvent(False, 'i', (1, 0, 3,freq))
time.sleep(4.5)
answerbutton.on_click(ans_button_clicked)
sonibutton.on_click(son_button_clicked)
redraw()
Now that you have been evaluated on this sound representation, it is time to move on to the next set where you will learn a new sonification.
Please close this tab and open the next module from the list of notebooks:
5. Panned Pulse Training Module