Escolar Documentos
Profissional Documentos
Cultura Documentos
Neil MacMillan
For G. Tzanetakis, CSC 475
University of Victoria
nrqm@uvic.ca
Abstract
This document describes a project involving a
2. Clap Detection
modification to the existing robot drummer, allowing it to The robot must be able to gather input before it can
respond in real time to audio stimulus (hand clapping) respond to claps. I looked for an off-the-shelf clap sensor,
without needing external equipment. The stimulus but could not find one that was cheap and produced
detection, timing considerations, clap tracking and appropriate output. I decided to build a clap sensor from
prediction, and results are all discussed. scratch. This involved designing a circuit for the sensor
and laying it out as a circuit board, purchasing parts,
1. Introduction etching and assembling the circuit board, installing the
To satisfy the requirements for a past course at UVic, I and components, and writing the firmware.
two other engineering students, Matthew Loisel and Daniel Without the clap sensor, there would be two choices for
Partridge, designed and built a robot drummer that could clap detection. If the robot drummer did clap detection,
strike a drum in response to Musical Instrument Digital then the signal processing component would add a
Interface (MIDI) input. [1] That project was mostly significant processing and memory load to the drummer’s
successful, but once it was completed there were still many firmware. The sensor offloads this work onto a
ways that the robot could be made more useful or coprocessor that transforms the analog input into a digital
interesting. One problem with the robot is that it is limited output that is very easy for the drummer to read. Another
to MIDI input. It can accept commands from any MIDI way to do clap detection would be with a computer.
source, but it requires such a source—for example a Clearly this would undermine one of the points of this
synthesizer or computer—to be present. In other words, project, which is to make the robot drummer more
the robot is not a standalone machine. independent. In addition to that, my experiments show
The robot can be improved by adding capability to that sending MIDI commands over USB incurs a
drum in direct response to live audio input such as hand transmission delay between 10 and 30 ms. The main
clapping, instead of requiring some complex external piece problem is the delay variance, which is impossible to
of equipment to translate analog signals to MIDI predict or to correct.
commands. It would be even more interesting if the robot 2.1 Clap Sensor Circuit Board
could, when a regular rhythm is being clapped, drum along
with the rhythm—not in response to the claps, but by Mic
anticipating each clap and striking the drum at the precise TRS
moment of the clap instead of shortly after it. 2.2 kΩ Jack
This document will describe the three core tasks that I
undertook for this project:
- Designed and built a sensor for detecting a hand ATtiny84
10 kΩ
clap and outputting a digital signal. This turned
out to be the main task.
- Measured the delays between a stimulus to the + ISP
robot drummer and the drum strike. 5V 10 μF
- Modified the robot’s firmware to accept the clap
sensor’s digital signal, and to predict when the
next clap in a simple beat will occur.
LED
1.5 kΩ
Permission to make digital or hard copies of all or part of this work for
100 Ω
personal or classroom use is granted without fee provided that copies Output
are not made or distributed for profit or commercial advantage and that
copies bear this notice and the full citation on the first page.
© 2009 University of Victoria Figure 1. Clap sensor circuit diagram.
The clap sensor is a simple circuit to interface an solved this problem by drilling extra via holes next to the
electret microphone with a microcontroller. [2] Figure 1 through holes, connecting the top copper to metal posts in
shows the circuit I used. The symbol at the top represents the vias, and then connecting the component leads to the
a tip-ring-sleeve (TRS) jack, which is what the microphone posts on the bottom of the board, after the components
plugs into. The sleeve is ground, the ring is V+, and the tip were installed. This problem could be solved by
is the signal line. The V+ line is connected to power purchasing the boards from a professional manufacturer.
through a resistor, which is what causes the output voltage The manufacturer would be able to tin inside the through
to vary when the electret is stimulated. In the middle of holes, which would make it very easy to solder all of the
Figure 1, the six-pin connector is for the in-system components from the bottom of the board.
programmer (ISP) plug, which is how code is loaded onto The small holes in Figure 2, on the right side of the top
the microcontroller. The ISP plug includes a 10 kΩ pull- layer, correspond from top to bottom to the capacitor, the
up resistor connected to the ISP’s RESET pin. The block LED, and the three resistors (1.5 kΩ, 100 Ω, and 2.2 kΩ).
to the right represents the ATtiny84 microcontroller that I The small holes on the microcontroller interface are vias to
chose to use (the logical connections to the ATtiny84 in connect the bottom layer traces to the top layer. The three
the diagram above to not match the physical layout of the large holes on the outer edge of the top layer correspond
chip). To the left is the DC power input, which can range from top to bottom to V+, ground, and the output. The
in voltage from 2.7 V to 5.5 V. The robot drummer large holes in the middle of the top and bottom layers are
outputs 5 V. Beside the power source is a 10 μF capacitor, for the six ISP header pins.
which protects the circuitry from transients in the power Figure 3 shows the finished clap sensor. As of this
source. The LED is simply a visual output. The LED’s writing, there is still a bug in the microphone jack that
current-limiting resistor value depends on the LED; the causes the mic signal pin to stay at around 0.3 V.
LED that I used needed a 1.5 kΩ resistor to provide its One problem with the circuit board layout is the pull-up
rated current at 3.3 V. It should be 2.5 kΩ for 5 V, but resistor that drives the ISP RESET pin high. In Figure 3
going above the rated current is safe as long as the LED one can see that I added the resistor after creating the
does not spend much time turned on. At the bottom of circuit board. I have not found a way to integrate a full-
Figure 1 is the digital output that the robot drummer reads, sized resistor naturally into the board. A surface-mount
protected by a small current-limiting resistance. resistor, which is much smaller than the ½ Watt axial
resistor that I used, would fit easily next to the ISP port but
it would make the sensor more difficult to assemble.
2
The drummer’s mechanical delay depends on two
I found that the fastest and most memory-efficient option for factors: the strike velocity and the strike displacement. For
integer square root is by far a nearest-neighbour binary search my purposes, the strike velocity is always set to the
on a constant lookup table stored in Flash ROM.
maximum of 0x7F (the strike velocity value corresponds to for calculating the histogram; further investigation is
the velocity argument in a MIDI ―note on‖ command). required to determine how the available memory limits the
The displacement is the physical distance that the tip of the histogram’s effectiveness.
drum stick travels to go from its rest position to the drum Incorporating beat strength complicates the problem
head. A larger displacement causes a larger mechanical even more. For example, two sections of a beat pattern
delay. that have the same period but different strengths should be
It’s difficult to predict the displacement accurately, considered different, and the histogram approach cannot
because it depends on where the user positions the handle that elegantly. Another way to look at the data is to
drummer and the drum. It might be possible to put a interleave strength measurements with times. For
distance sensor on the drum stick’s tip, but that would not example, weak and strong alternating beats at a frequency
be durable or extensible. I chose to make a reasonable of 2 Hz would look like this:
guess at the displacement and came up with 25 mm, which
corresponds to a delay of around 40 ms. 127 500 110 500 127 500 110 …
4. Clap Tracking In the above sequence, the strong (127) and weak (110)
The final part of the project was to program the robot beats are interleaved with 500 ms beat periods. This does
drummer to strike in time with a clapped rhythm. The not lend itself to a beat histogram, but a more general
scenario that I finished in time for this report was to have numerical pattern matching approach would be able to
the drummer respond to a simple, regular rhythm. The predict, for example, a 500 ms gap preceded by a strong
drummer calculates the time between the first four claps, beat and succeeded by a weak beat.
stores the three times, and averages them to produce a final
beat period. It adds the beat period to the time of the last 5. Conclusion
clap to predict the time of the next clap. Then it subtracts Augmenting the robot drummer with the ability to track
40 ms to compensate for the drummer’s mechanical delay. claps predictively makes the system more interesting and
That way, the drum stick will strike the drum within a few useful. A helpful component is a clap-sensing coprocessor
milliseconds of the next predicted strike time (as long as that extracts beat information from hand claps. By
the drum stick is positioned 25 mm above the drum head). analysing a simple beat and predicting its future, the robot
The drummer could handle more complex beat patterns can now strike the drum in time with the beat while
with a more intelligent algorithm. One simple option is to compensating for mechanical latency. I did not get a
allow the user to pre-configure the length of the pattern. chance to investigate this problem fully, and further work
This is an extension to my implementation. The drummer can be done in the areas of clap detection and beat
would count N time differences between claps, and then tracking.
start to output at the recorded times when it reaches clap
N+1. Perhaps it could average three patterns of length N 6. References
before striking the drum.3 [1] N. MacMillan, M. Loisel, D. Partridge. ―Final Design Report
If the clap sensor microphone is positioned so that it for a Robot Drummer,‖ [Web site] August, 2009, Available:
can ignore the drum strike and detect only claps, then the http://www.scribd.com/doc/19161969
robot could continually average the clap times it detects. [2] T. Engdahl. ―Powering Microphones,‖ [Web site] 2000,
This would allow the user to change the pattern Available:
dynamically, but it would be confusing to the user. During http://www.epanorama.net/circuits/microphone_powering.ht
my tests, I found it difficult to carry on my own clapping ml
beat when the drummer was playing something slightly [3] ExpressPCB. ―Free PCB layout software – Low cost circuit
boards – Top quality PCB manufacturing,‖ [Web site] 2009,
different. Available: http://expresspcb.com/
Ideally, a beat tracking or beat histogram algorithm [4] B.H. Repp. ―The sound of two hands clapping: An
would be used. I did not have time to investigate the exploratory study,‖ J. Acoust. Soc. Am. Volume 81, Issue 4,
options deeply. I think especially the beat histogram could pp. 1100-1109, April 1987.
be a feasible approach, because it requires less [5] J.W. Crenshaw. ―Integer Square Roots,‖ [Web site] February
computation than artificial intelligence mechanisms. 1998, Available: http://www.embedded.com/98/9802fe2.htm
There is around 1 KB of writable memory available on the
robot’s microcontroller, which places a significant limit on
the effectiveness of any beat tracking algorithms. The
histogram method would need to strike a balance between
histogram size and the number of samples being tracked
3
The algorithm I implemented is a special case of this approach,
with N=1.