Archive

Posts Tagged ‘arduino’

Hardware monophonic pitch and amplitude detector or ‘double bass to 303 box’

February 13th, 2009

In earlier posts I’ve alluded to the second half of my midiCV box, a hardware pitch and amplitude tracker that would allow me to play my 303 in unison with my bass. Before anyone gets too excited it’s not all there yet, but it’s finally starting to come together and as I need to put it on the back burner for a while I decided now was a good time document it. The dream was and still is to be able to play my double bass, pick out the pitch and amplitude in real time and use my homemade Arduino controlled cv generator to control my 303. Both the double bass and the 303 are inherently slidey beasts and as control voltages have a real advantage over midi in that arena so in my head it seemed like the next logical step after building a CV box. At first I thought all this would be easy and to be honest if I had known how difficult and time consuming this project was going to be I probably would have stopped right there and got on with other more pressing elements of my PhD. More fool me ….

The motivation for doing pitch tracking using hardware rather relying on the ubiquitous laptop MaxMSP combination was twofold. Firstly I really like the idea of freeing myself, the double bass and x0x box from the computer; just plugging the double bass piezo mic into the cv box and going for it. Secondly I tried pitch tracking in Max and I couldn’t get good results for bass. The zerox~ object which counts zero crossings is pretty lousy and my normal go to object for pitch tracking, Tristain Jehan’s port of Miller Puckette’s fiddle~ doesn’t perform well with low frequencies. In fact any FFT based approach is likely to be disappointing for bass due to the fact that to accurately track low frequencies you need large sample sizes which incur enough latency to make the whole thing miserable.

Googling about bass pitch trackers brings up all kinds of academic papers featuring algorithms you haven’t got a cat in hell’s chance of running on an Arduino with latencies low enough or hardware I can’t afford and ouputs MIDI (why play a fretless instrument and use MIDI to drive an analogue synthesizer?). Having read in Sound on Sound magazine about the old roland SPV355 pitch to voltage box and googling around the subject enough to discover the ems pitch to voltage synth I knew there had to be a hardware design that was at least half decent enough to be fun to play.

My first idea was to count zero crossings, or more precisely count the time between them. This lead to a bit of googling which dug up this interesting rf audio demodulator which feeds the the signal through a voltage divider set close to the logical bandgap to trigger the external interrupt on an atmel ATmega8. After building and testing a circuit that fed my double bass’s signal through an overblown op-amp and then used zener diodes to regulate it back down to a 0 to 5 V square wave I realised that zero crossings were not really what I wanted. Using the time between zero crossings to calculate the period only works for sine waves and what I really needed was to measure the time between peaks in the waveform. I’d like to claim this blast of inspiration was my own but in reality it was googling for hours looking for guitar tuner circuits. The straight dope came from this excellent article on electronicdesign.com. This circuit is pretty simple which is why I like it so much, the signal gets filtered a bit then fed to an envelope follower. This then gets potted down to 90% of its original voltage value and compared with that of the signal straight from the filter. Whenever the waveform’s voltage is in its top 10% the compartor goes high and the rest of the time it’s low. After building roughly the same circuit as that on electronicdesign.com I got a nice digital looking pulse with the time between pulses corresponding to the frequency of the incoming signal. This signal swung between +/- 15 V (as this was what was driving the op-amp rails) which I didn’t particularly fancy feeding the external interupt on my Arduino but it only took a diode and potential divider to format this signal to 0 to 5 V range. I adapted the circuit a bit so that the envelope followers output gets buffered, smoothed and fed into one of the Arduino’s analogue inputs, that takes care of the amplitude part of the signal tracking.

At this point it was time to start reading up on how to get the Arduino to accurately measure times on a sub millisecond scale. To get enough resolution to detect the difference between 200 and 201 Hz I would need to be able to measure time in 25 microsecond increments. The Arduino comes with millis() a function for how many milliseconds since you turned it on and delayMicroseconds() for putting it to sleep for a brief while but you can’t measure short (sub millisecond) periods of time without creating your own timer using internal interrupts driven from a sub division of the Arduino’s 16MHz clock. Luckily uchobby.com has a resource that borders on the sublime side of brilliant for this kind of stuff so it wasn’t too hard to set up the arduino so that on external interrupts from the circuit board the time since the last interrupt is stored.

From there the rest is just maths and coding. I wrote a simple algorithm for bonk (note onset) detection and some fancy bits to try and filter octave jumps due to shifting harmonic strengths (more on these later). The code has to be fairly efficient given the strain the internal timer code puts on the CPU so I skipped any floating point maths and saved a load of processor time by using a precomputed lookup table stored in program memory for converting a period reading into a pitch control voltage. Doing all this gave me a new respect for the people who coded on 68K processors like the Amiga back in the day and before when efficiency was really valued and sloppy brute force methods didn’t cut it. In short I felt like the man.

By now I had a box that was wonderful at telling me the frequency and amplitude of sine waves from a test signal generator but how would it fare with signals from my bass? The answer is pretty well if it wasn’t for those damn harmonics. Basically it seems that the ratio between the strengths of the fundamental and first harmonic changes enough on different notes on different strings to produce results that are accurate to pitch class but can vary in octave. This is most obvious on the E and A strings where after being played the reported period suddenly doubles as the first harmonic dies away leaving the fundamental to dominate. On the plus side it plays in tune and is responsive to slides and hammer ons which is one real short fall of commercial units (or so I read in the forums).

I had anticipated harmonics were going to be a bitch so I had built into the input a two stage low pass filter comprising of one pole low pass filter with its filter cutoff at 50 Hz and 4 pole harmonic killer kicking in at 200 Hz. I think this shallow role off on the lower notes is the reason they are more prone to octave jumping while the D and G strings seem better behaved. Either way having read a bit more on how the ems worked I figure my next design is going to have to feature an adaptive low pass filter that tracks with the fundamental. The easiest way I can figure to do this is to use a bank of band pass filters tuned at least half an octave apart each feeding an envelope follower going into the Arduino which can then decide where the lowest frequency energy lies and tune a VCF using a CV from the DAC. If it wasn’t the fact I have a shed load of other work on I would get stuck straight into it but as it stands I’m going to leave it a month or two before picking this back up. As such I will leave you with a circuit diagram, a link to the arduino sketch and a silly video showing the fun results of singing into the device.

Pitch and amplitude tracker mark one and my first circuit diagram in PCB Artist

Pitch and amplitude tracker mark one and my first circuit diagram in PCB Artist

For anyone whose interested in the midi-to-CV side of the box I would/will post a circuit diagram if/when I can be bothered, but to be honest with you it’s as simple as wiring inputs on the chip straight up to the digital outputs on the Arduino, supplying it power and wiring the outputs to jacks, I didn’t even bother buffering it given the low current draw from my x0x’s CV inputs. The only extra circuitry I used was to bypass the power supply with capacitors as the datasheet advised.

Hardware , , , ,

Pitch tracker for double bass in development

January 21st, 2009

Here’s a sneaky peek at what’s going on in the Rob cave this week (it’s not all lolcats here).

Screenshot of my development computer working on pitch tracker software

It's all javascript and frantic patching, note the number 40, suspicously close to the frequency of bottom E ....

The pitch tracker half of my midiCV box is getting some heavy development time as I try and squeeze it out in time to write a paper on it by the end of the month. At the moment I’m just playing it recordings of my double bass as I can no longer be arsed to get up, pick up the bass, and play it every time I want new data. Good job I fitted that line input then! One thing I’ve learnt is that javascript in Max is an excellent place to roadtest ideas before going to the bother of coding them in C and uploading them on the Arduino’s firmware. Expect a more detailed blog post and videos soon.

Hardware , , , , ,

Homebrew midi-cv box

January 12th, 2009
midiDAC and double bass pitch and amplitude tracker.

Homebrew midiDAC with integrated double bass pitch and amplitude tracker.

After having built a x0xb0x this summer which I fitted with Brian Castro’s x0xio back panel mod I decided a good project to make would be to make a midi-cv converter box. Although the x0x comes with it’s own midi in, the firmware is a bit tempremental and I was getting a lot of skipped notes and no control over slides when programming midi sequences for the x0x in Ableton. After having played for more time than I care to admit to with the x0x I’ve come to the conclusion that although the internal sequencer rocks, especially when using the SokkOS firmware (funky pattern randomise not to mention the other extra features) the midi response is a bit naff.

This formed half the motivation for building a midi-cv box, the other came from my practice as a double bass player. The double bass being fretless lends itself to slides just like the 303, with my new pitch CV in could I take the pitch and amplitude of double bass and use it as a control signal for my x0x ? More on this side of the project later. The brief I set myself for the midi-cv side was simply that it should supply at least 5 cv outs with a resolution high enough to allow for some interesting tuning options later on. I also wanted proper midi in although I knew while I was programming the firmware I’d probably just send the midi byte code over USB .

midiDAC with integrated double bass pitch and amplitude tracker.

midiDAC close up, the lid contains the inputs and outputs for the pitch tracker

Having looked around the Arduino forums I found a lot of people using the PWM outputs on the Arduino and just running them through a low pass RC filter to get a smooth variable DC voltage out. This has the benefit of being quick to make but the downside of non-linearity across the domain of the duty cycly and the range of voltage out and the small portamento this approach will neccessarily come with. Having decided against using the PWM out and having recently been introduced to the analog devices catalogue by an a electronics friend I decided to get hold of one of their AD5668 DAC chips and dive into the world of surface mount chips and SPI. I’m not claiming to be the first to hook up a DAC chip to the Arduino as there’s a fair amount of evidence people have taken the same approach when building their own cv boxes but the approach worked and I’m pretty pleased with the results.

The AD5668 and an adaptor to make soldering wires on to a 5mm chip slightly easier, came to about twenty quid from Farnell. The rest of the box materialised over a period of about two weeks as I made nightly visits to my long suffering local Maplin (I swear they hate me in there, there’s only so many times you can roll in at ten to eight and ask for an opto-isolator and a bag load of assorted components).

Under the hood of the midiDAC

Under the hood of the midiDAC

Although optoisolators aren’t technically neccessary for midi input I’m a bit of a stickler for standards and seeming as I’d never used one before I figured I might as well. This Arduino forum post came in handy for the midi in part of the circuitry and the midi byte parsing, I particularly enjoyed the mspaint circuit diagram. As you can see the box is packed pretty tight and the eight outputs which are on switched mini-jacks and quarter inch jacks barely fitted in the enclosure. The pitch and amplitude tracker I built for my double bass is on a circuit board blu-tacked to the inside of the lid so you can’t see it in this photo but I’ll cover that in another post.

The most time consuming part was reading the AD5668 datasheet forwards, backwards and in random access until I’d squeezed all the functionality I wanted out of it. It uses a 32 bit address space with 4 bit padding at either end, 8 bits for commands and 16 for resolution on the output which swings between 0 and 5v. There’s a fair bit of left and right shifting in the arduino source code as well as some bit masking which is kind of funky if you’re into powers of two.

There already exists a fair wealth of material (e.g. here, here and here) on how to get the Arduino to deal with midi byte code so after having successfuly hooked up the Arduino to the DAC chip (SPI really isn’t that hard given the Arduino’s built in shiftOut method, reading this helped) I just started hacking apart kuk’s midi parsing code for my own purposes. I’ve put the source code here so you can get a head start on making your own if you’re interested in using the AD5668 or just seeing a project that makes broader use of the midi specification. There’s a fair amount of code dedicated to getting pitch and amplitude data from the double bass which you’ll have to scroll past.

Having documented the insides of my midiDAC I’ll leave you with two videos showing its implementation with my x0xb0x. I cooked up a little random scale generator in Max to play on 16th notes and synched the whole thing up to my drum machine via Ableton and Rewire (Max 5’s new transport still doesn’t accept MTC as a clock source, hence Ableton and Rewire joining the party). Enjoy.

Hardware , , , , ,

Introducing the ir2midi box

January 12th, 2009

Another part of my forthcoming interactive tv installation is the hardware infrared to midi box I built for interfacing old tv remotes with max and jitter. This was a pretty simple project based around the now ubiquitous Arduino microcontroller and a rather handy i.r. receiver chip I picked up for next to nothing from Sparkfun.

The IR remote control to midi tool box.

The IR remote control to midi tool box.

The box plugs in to my computer using USB and a max patch takes care of churning out the midi to whatever application I fancy. As well as using it for my installation I use it for controlling Ableton while standing a.f.k practicing bass. I included a hardware reset switch on the top which I hardly ever use since the latest Arduino’s support software reset when loading firmware and seem much happier with Max’s port object.

I stuck on two LEDs to give some user feedback, green for power and red for code received. I initially tried to write my own firmware for decoding the signals but after googling around a bit I came across this guy’s code which worked much better than my own attempts and saved me about three weeks work. Basically the box just chucks out a unique hex code for any button on any remote that’s sufficiently close to the RC5 Philips standard which seems to be most of the ones in my house. Max takes care of assigning each code/button to a MIDI message, like note, program change or continous controller but I might in the future go back and shift this functionality from Max into the hardware. When I’m using it with Ableton I just route the midi back from Max to Live using midiYoke.

Under the hood of the ir2midi.

Under the hood of the ir2midi.

Looking inside you can see there’s not much going on here, just the Vishay TSOP85 receiver and the circuitry for the LEDs and reset switch (this is on the flip side of the vero board but it’s just some pull down resistors). The nice thing about the receiver was it came with the neccessary circuitry integrated so all I had to do was wire it up to the power and one of the digital inputs on the Arduino. I’ve recently become obsessed with socketing things so I can pull them out and swap/replace them as I see fit so I used a few of these yummy modular connectors from Maplin. That’s where I got hold of the frosted blue enclosure which lets through the i.r. signal unimpeded as well as looking quite shhhexy (as much as a blue see through box can).

Introducing the Vishay TSOP85

Introducing the Vishay TSOP85

That pretty much covers it. I’m pretty pleased with it as it is, the whole thing only cost about £30 and took not too long to build. In the future I’ll post up the Max patch, circuit diagram and a video showing it’s integration with Ableton. Changing scene with the channel change and controlling volume, stop, start, record, etc by remote is really quite handy for me as I play double bass and constantly putting it down just to stop/start is a pain in the arse.

Hardware , , , , ,