Skip to content

Commit 54694ff

Browse files
committed
Added more test examples
1 parent 853c971 commit 54694ff

File tree

2 files changed

+254
-0
lines changed

2 files changed

+254
-0
lines changed

AudioTests.ipynb

+106
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"id": "8aec73c2",
6+
"metadata": {},
7+
"source": [
8+
"#### Audio tests\n",
9+
"\n",
10+
"Here is some code to play audio and record your voice to make sure speaker and microphone are working. **Make sure you reduced the volume by running `alsamixer` or `amixer set Master 40%` in a terminal on the Voice Kit.**"
11+
]
12+
},
13+
{
14+
"cell_type": "code",
15+
"execution_count": null,
16+
"id": "2d7c97f5",
17+
"metadata": {},
18+
"outputs": [],
19+
"source": [
20+
"import tempfile\n",
21+
"import time\n",
22+
"from aiy.voice.audio import AudioFormat, play_wav, record_file\n",
23+
"\n",
24+
"TEST_SOUND_PATH = '/usr/share/sounds/alsa/Front_Center.wav'\n",
25+
"RECORD_DURATION_SECONDS = 3"
26+
]
27+
},
28+
{
29+
"cell_type": "markdown",
30+
"id": "6b0e34b4",
31+
"metadata": {},
32+
"source": [
33+
"##### Playing a sound\n",
34+
"\n",
35+
"Run the next cell and you should hear the words 'Front center' spoken."
36+
]
37+
},
38+
{
39+
"cell_type": "code",
40+
"execution_count": null,
41+
"id": "e39e04e0",
42+
"metadata": {},
43+
"outputs": [],
44+
"source": [
45+
"play_wav(TEST_SOUND_PATH)\n",
46+
"print('Hope you heard the sound :)')"
47+
]
48+
},
49+
{
50+
"cell_type": "markdown",
51+
"id": "6be78c98",
52+
"metadata": {},
53+
"source": [
54+
"##### Recording\n",
55+
"\n",
56+
"Now we record sound for three seconds and play it back. Run the next cell and then press enter when ready to speak or sing :)"
57+
]
58+
},
59+
{
60+
"cell_type": "code",
61+
"execution_count": null,
62+
"id": "2a9c7525",
63+
"metadata": {},
64+
"outputs": [],
65+
"source": [
66+
"with tempfile.NamedTemporaryFile() as f:\n",
67+
" input('Press enter when you are ready and say \"Testing, 1 2 3\"...')\n",
68+
" print('Recording for %d seconds...' % RECORD_DURATION_SECONDS)\n",
69+
"\n",
70+
" record_file(AudioFormat.CD, filename=f.name, filetype='wav',\n",
71+
" wait=lambda: time.sleep(RECORD_DURATION_SECONDS))\n",
72+
" print('Playing back recorded audio...')\n",
73+
" play_wav(f.name)"
74+
]
75+
},
76+
{
77+
"cell_type": "code",
78+
"execution_count": null,
79+
"id": "39e755e3",
80+
"metadata": {},
81+
"outputs": [],
82+
"source": []
83+
}
84+
],
85+
"metadata": {
86+
"kernelspec": {
87+
"display_name": "Python 3 (ipykernel)",
88+
"language": "python",
89+
"name": "python3"
90+
},
91+
"language_info": {
92+
"codemirror_mode": {
93+
"name": "ipython",
94+
"version": 3
95+
},
96+
"file_extension": ".py",
97+
"mimetype": "text/x-python",
98+
"name": "python",
99+
"nbconvert_exporter": "python",
100+
"pygments_lexer": "ipython3",
101+
"version": "3.7.3"
102+
}
103+
},
104+
"nbformat": 4,
105+
"nbformat_minor": 5
106+
}

ButtonTests.ipynb

+148
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"id": "85294ea7",
6+
"metadata": {},
7+
"source": [
8+
"### Button Tests\n",
9+
"\n",
10+
"Here is some code to test and play with the button on top of your Voice Kit.\n",
11+
"\n",
12+
"##### Lights"
13+
]
14+
},
15+
{
16+
"cell_type": "code",
17+
"execution_count": null,
18+
"id": "6e2301d6",
19+
"metadata": {},
20+
"outputs": [],
21+
"source": [
22+
"from aiy.leds import Leds, Color\n",
23+
"import time"
24+
]
25+
},
26+
{
27+
"cell_type": "markdown",
28+
"id": "88487129",
29+
"metadata": {},
30+
"source": [
31+
"First we turn the button red, wait for one second and then turn it off again."
32+
]
33+
},
34+
{
35+
"cell_type": "code",
36+
"execution_count": null,
37+
"id": "494c7bee",
38+
"metadata": {},
39+
"outputs": [],
40+
"source": [
41+
"with Leds() as buttonLight:\n",
42+
" buttonLight.update(buttonLight.rgb_on(Color.RED))\n",
43+
" time.sleep(1.0)\n",
44+
" buttonLight.update(buttonLight.rgb_off())"
45+
]
46+
},
47+
{
48+
"cell_type": "markdown",
49+
"id": "e66dbf08",
50+
"metadata": {},
51+
"source": [
52+
"This time we make a list of colors and then crcle through it lighting up the button in all specified colors."
53+
]
54+
},
55+
{
56+
"cell_type": "code",
57+
"execution_count": null,
58+
"id": "74f24845",
59+
"metadata": {},
60+
"outputs": [],
61+
"source": [
62+
"colors = [Color.RED, Color.GREEN, Color.BLUE, Color.WHITE]\n",
63+
"\n",
64+
"with Leds() as buttonLight:\n",
65+
" print('Enjoy the light show')\n",
66+
" \n",
67+
" for c in colors:\n",
68+
" buttonLight.update(buttonLight.rgb_on(c))\n",
69+
" time.sleep(0.5)\n",
70+
"\n",
71+
" buttonLight.update(buttonLight.rgb_off())"
72+
]
73+
},
74+
{
75+
"cell_type": "markdown",
76+
"id": "1bf6cc7f",
77+
"metadata": {},
78+
"source": [
79+
"##### Pressing the button\n",
80+
"\n",
81+
"We can detect of the button is pressed or released and then execute a callback function. Here we define two of those callback functions `lightOn()` and `lightOff()`. Then we enter an infinite loop and if the button is pressed or released we run the callback function. Run the next two cells and try."
82+
]
83+
},
84+
{
85+
"cell_type": "code",
86+
"execution_count": null,
87+
"id": "425474a8",
88+
"metadata": {},
89+
"outputs": [],
90+
"source": [
91+
"from aiy.pins import BUTTON_GPIO_PIN\n",
92+
"from gpiozero import Button"
93+
]
94+
},
95+
{
96+
"cell_type": "code",
97+
"execution_count": null,
98+
"id": "11153ec6",
99+
"metadata": {},
100+
"outputs": [],
101+
"source": [
102+
"def lightOn():\n",
103+
" buttonLight.update(buttonLight.rgb_on(Color.RED))\n",
104+
" \n",
105+
"def lightOff():\n",
106+
" buttonLight.update(buttonLight.rgb_off())\n",
107+
" \n",
108+
"with Leds() as buttonLight, Button(BUTTON_GPIO_PIN) as button:\n",
109+
" try:\n",
110+
" print('Press the button. I know you are dying to :)')\n",
111+
" while True:\n",
112+
" button.when_pressed=lightOn\n",
113+
" button.when_released=lightOff\n",
114+
" except KeyboardInterrupt:\n",
115+
" print('Bye')"
116+
]
117+
},
118+
{
119+
"cell_type": "code",
120+
"execution_count": null,
121+
"id": "e646d1a9",
122+
"metadata": {},
123+
"outputs": [],
124+
"source": []
125+
}
126+
],
127+
"metadata": {
128+
"kernelspec": {
129+
"display_name": "Python 3 (ipykernel)",
130+
"language": "python",
131+
"name": "python3"
132+
},
133+
"language_info": {
134+
"codemirror_mode": {
135+
"name": "ipython",
136+
"version": 3
137+
},
138+
"file_extension": ".py",
139+
"mimetype": "text/x-python",
140+
"name": "python",
141+
"nbconvert_exporter": "python",
142+
"pygments_lexer": "ipython3",
143+
"version": "3.7.3"
144+
}
145+
},
146+
"nbformat": 4,
147+
"nbformat_minor": 5
148+
}

0 commit comments

Comments
 (0)