Mozzi Polyphonic Synthesizer

3,507 views
Skip to first unread message

Bernardo Alves

unread,
Apr 23, 2014, 9:26:00 AM4/23/14
to mozzi...@googlegroups.com
Hello,

I wrote the attached code (inspired on other code from examples and forum posts) to output sound from a Midi keyboard. The code works well and allows playing 4 notes simultaneously. Additionally, connecting a pushbutton to Arduino, we can alternate between 4 different types of sound waves: sine, saw, triangle and square.

The problem is, I'd like to be able to play more than 4 notes simultaneously, but when I use more than 4oscillators I get zipper noise, and eventually, a total gibberish when I play more than 4 keys. Although most chords have 3 or 4 notes, there are some situations where it would be nice to be able to play 6 or 8 notes at the same time.

I read other posts about polyphony in Mozzi and I got the impression that it should be possible to have 6 oscillators playing at the same time. Does anyone have any suggestions on how the attached code could be improved? Could this problem originate from the fact I'm using HIFI mode? I really like working with the Mozzi library. I've seen other sound projects and IMHO, this one is the best, so I'd really like to implement my synthesizer project with this library.

Cheers,
Bernardo
MIDI_Input.ino

Mr Sensorium

unread,
Apr 24, 2014, 8:18:47 AM4/24/14
to mozzi...@googlegroups.com
Hi Bernado,
I modified the sketch to play 6 voices without breaking up.  For me, it has a regular tick (about 1 Hz) which I think is related to the midi communication - I seem to be getting this consistently lately with any midi sketches, I might go back to testing the usb-midi code which didn't have the issue...or maybe it was testing with midi out from the computer instead of keyboards...  Do you get the midi-ticking at your end?  I also hear it on note-on and note-off messages.

The main change in the sketch which I think made biggest difference is to use control-rate envelopes, which requires the most recent Mozzi (version 2014-04-20-15:06).  Most of the other changes I tried probably end up being cosmetic, however it does save some processing by avoiding the if-else statement in updateAudio(), instead instantiating only the number of oscillators required for the voices and swapping wavetables into the oscillators in updateControl().

The wavetable swapping requires that all tables have the same size, so I made a square wave to match the others you have, attached here so you can drop it into your Mozzi/tables/ folder.  I'll include it in the next release.

Anyway, see how it goes..
Tim
MIDI_Input_6poly.ino
square_no_alias_2048_int8.h

Bernardo Alves

unread,
Apr 25, 2014, 6:26:18 AM4/25/14
to mozzi...@googlegroups.com

Hello Tim,

I tested the sketch you posted, using the latest Mozzi version and it works J Nevertheless in sometimes hear the regular tick you mention and I now get midi tick when a note comes on. I’m playing from a Clavinova piano without usb. Since the control rate is lower, I also feel the program struggling to keep up when I play faster.

 

Overall, I would say I had a slightly better sound quality running my original sketch on the 2013-09-12 Mozzi version, but limited to 4 voices. On the other hand, with this version I can play 6 voices… I guess it’s all about tradeoffs. I’ll keep experimenting to see if I can find the right balance. I have even thought of connecting my two Arduino Nano in series to play 4 voices each, but I’m not sure about how to set up the resistor circuit for HIFI in this double board scenario. I’ll post further results here if I try this.

 

Thank you for your quick and helpful answer and your inputs on the code. My native language is VB and I just have a basic knowledge of C and C++, so it’s always nice to get some tips and best practice examples. I take the opportunity to congratulate you for creating, maintaining and most importantly, supporting this awesome project. Mozzi rocks!

 

Cheers,

Bernardo

Alec Waters

unread,
May 1, 2014, 2:45:20 PM5/1/14
to mozzi...@googlegroups.com
Hi Tim,

Is there anything special I'd need to do to make this work with one of those Rock Band keytars? I've added a HandleNoteOff() function (otherwise the notes don't turn off!), but I don't get anything approaching polyphony. I don't really know the proper term to describe the sound I get, but when more than one key is pressed I definitely don't get two-notes-played-at-once!

Many thanks,
alec


--
You received this message because you are subscribed to the Google Groups "Mozzi-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mozzi-users...@googlegroups.com.
To post to this group, send email to mozzi...@googlegroups.com.
To view this discussion on the web, visit https://groups.google.com/d/msgid/mozzi-users/ced09d4a-1f3e-4b2b-bf6a-88c8bee68864%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Tim Barrass

unread,
May 2, 2014, 1:01:53 AM5/2/14
to mozzi...@googlegroups.com
Hi Alec,

for Mozzi with keytar, look at fakebitpolytechnic: http://www.fakebitpolytechnic.com/cheapsynth-concept-safety-info/

If it's sounding wrong when you play more than one note, there's a good chance that it's a variable or calculation overflowing which can be fixed by making sure to cast to int or long before a multiply in updateAudio(), so something like:

   int out = (oscil.next() * envelope.next())>>8;

would become:

  int out = ((int) oscil.next() * envelope.next())>>8;

and if you were adding a few oscils together, you might even need to cast to long to make room:

  int out = (int) ((long) oscil1.next() * envelope1.next() +
                            (int) oscil2.next() * envelope2.next() +
                            (int) oscil3.next() * envelope3.next() +
                            ....etc, etc, +
                            (int) oscil10.next() * envelope10.next()) >> shift-amount;

Hope that helps, post your code if not..
Tim


Alec Waters

unread,
May 2, 2014, 11:51:53 AM5/2/14
to mozzi...@googlegroups.com
Hi Tim,

I'll take a look, but the code I'm using is yours from earlier in this thread. The only change I've made is to add the NoteOff handler, and it still sounded wonky before I did it.

Are you relying on any kind of output circuit? I've got it going directly from pin 9 to headphones.

Many thanks,
alec


Tim Barrass

unread,
May 3, 2014, 2:42:32 AM5/3/14
to mozzi...@googlegroups.com
Alex,

the sketch in this thread is modified from the one posted in the opening question where Bernado indicates it's for HIFI mode.

For STANDARD_PLUS (8, nearly 9 bit output), change the >>6 in the last line to >>10.

My slightly wonky way of working it out is by seeing how many bits the audio calculation takes up...
- the gains in the sketch have a maximum of 255, 8 bits (from ATTACK_LEVEL and DECAY_LEVEL)
- each line of the calculation has a maximum of 255*255, 16 bits (8 bit gain * 8 bit oscil)
- 255*255 times 6 lines = 390150, an 18 bit number on my calculator in programming mode

So, shift right by 10 to reduce 18 bits to an 8 bit number, >>10

Looks like I accidentally reduced it to 12 bits instead of the optiumum 14 bits for HIFI in the sketch!  Which would only make it too soft...

Anyway, that should fix the distortion (but there's still zipper noise from the envelopes working at control rate).


Tim



Odd Ben

unread,
May 8, 2014, 5:45:09 AM5/8/14
to mozzi...@googlegroups.com
Hi to all,

In first I want you say thank you Tim. The above explanations helped me to better understand calculations in updateAudio. And I would like to contribute by sharing a sketch, if it can be useful for users Mozzi I would be very happy. This code needs improvement, especially for note priority but, it works.

By default it has 2 voices of polyphony. However, modifying a few lines of code it can be adapted for 3-6 voices.  Explanations are in the sketch. If there is an error - or more -, please correct me.

++
Ben, from France

PS: Sorry for my english, I need to improve it, I know. :)
polysynth_2_to_6_voices.ino

BenzOu

unread,
May 8, 2014, 5:48:21 AM5/8/14
to mozzi...@googlegroups.com
I forgot to mention, this code works ONLY for Mozzi in HIFI mode!

Jonathon Breitner

unread,
Jun 14, 2016, 12:48:26 PM6/14/16
to Mozzi-users
Thank you vary much. unfortunately though.

Arduino: 1.6.7 (Windows 7), Board: "Arduino/Genuino Uno"

C:\Users\Jonathon\Downloads\polysynth_2_to_6_voices\polysynth_2_to_6_voices.ino: In function 'void setup()':

polysynth_2_to_6_voices:28: error: 'MIDI' was not declared in this scope

   MIDI.begin(MIDI_CHANNEL_OMNI);

   ^

C:\Users\Jonathon\Downloads\polysynth_2_to_6_voices\polysynth_2_to_6_voices.ino: In function 'void updateControl()':

polysynth_2_to_6_voices:68: error: 'MIDI' was not declared in this scope

   MIDI.read();

   ^

exit status 1
'MIDI' was not declared in this scope

Gunnar Frei

unread,
Dec 12, 2016, 9:02:42 AM12/12/16
to Mozzi-users
Hi Jonathon,

You have to write the following line between the includes and defines:
MIDI_CREATE_DEFAULT_INSTANCE();


this is for straightforward compatibility with sketches written with v4.1 and older. See: https://github.com/FortySevenEffects/arduino_midi_library/releases

I attached my version which also includes the HandleNoteOff function so it works with my Midi Keyboard.


MIDI_Input_6poly_newMidiLib_HandleNoteOff.ino
Reply all
Reply to author
Forward
0 new messages