MidiLin Project Modification

TL:DR How do I synthesize a MIDI output coming from serial.write to a speaker without a PC?

I am attempting to make my take on the MidiLin (Found here GitHub - HanYangZhao/MidiLin: Violin inspired Midi Controller ) which is an Arduino take on the Wintergatan Modulin, an analog modular synthesizer with a SoftPot potentiometer as the control. Preface, I've done some projects before, but nothing with audio.

I've hit a bit of a snag. I have two options for outputting sound. I either use the MIDI shield's out jack to send the signal to my computer and have it be synthesized there, which is fine, but I want to try and make this portable.

What I would like to do it have the input be synthesized on the board and sent to a speaker. I know about the tone function, but I'm also trying to work out two features that I'm not sure how to implement.

  1. Sawtooth and sine waves.
  2. basic ADSR.

If you look at the code on the github page, at line 588 is the noteOn function, where the pitch and velocity data are sent on Serial.write. I do not know what to do here.

The first step is to figure out how to get the output to the speaker. That is the main thing I am asking for. I have seen the arduino synthesizer (https://www.instructables.com/id/The-Arduino-Synthesizer/ ) but I would need to figure out how to combine the two projects. Thank you.

To connect a speaker to an Arduino you need an active amplifier, one that requires power. attach the input to this to the signal generating output of the Arduino through a series capacitor and don’t forget to connect the ground of the amplifier to the ground of the Arduino.

As most Arduinos do not have a D/A converter you will either have to add an external one or use one of the PWM pins ( the ones marked with a ~ ). The PWM pin you use is determined by the software you use, but a PWM pin only gives you 8 bits resolution.

Different waveforms can be made, a saw tooth is the simplest as it just involves incrementing a byte and sending that to the output. A sin wave is similar but you use a look up table ( an array ) to convert this increment into a sin wave.

ADSR is all together more difficult and involves multiplying the final sample by some figure to control its amplitude. You will normally need a much more powerful processor than a Uno to do this. The Teensy series of processors are a good choice as they have the grunt.

Re: the speaker, apologies for the drawing, but let me see if I understand this. I’m not sure what specifically you mean by amplifier.

This project uses a Mega.

ExampleBoard.png

Well given the diagram shows very little information it is hard to fault.
You should not label the output as TX0 as that labelling is for a serial output not an audio output.

I was thinking of more the combined amplifier speakers you find in active speakers like this

Or an amplifier like this:-

Right okay, I have a speaker, I think I'm okay with getting the adafruit amplifier. The problem is that I don't know what to do with it in the code. The output (pitch, velocity) is being written to serial, so I'm not understanding what to do with that information.

Also I saw something about the volume library GitHub - connornishijima/arduino-synth: Synth is a library for creating custom waveshapes at any volume level using ONLY an Arduino and a speaker! ? Volume2 can fake different waves, but I'm pretty sure this breaks my code somewhere.

The output (pitch, velocity) is being written to serial,

That is a MIDI message you can not directly turn that into sound.

For a Mega the sound comes out of pin 4, that is what you need to connect your amplifier to.

Sorry for the late reply, I think after this I’ll be good for a bit. Port PH4 on the arduino (above tx and rx) if I’m understanding correctly?
Also, I am not quite sure where in the code the sound would be sent to pin 4?

ExampleBoard2.png

I am not quite sure where in the code the sound would be sent to pin 4?

You won’t see it directly referenced to as it is a pin that is controlled by a timer.

Port PH4 on the arduino (above tx and rx) if I'm understanding correctly?

On the Mega one alternative use of this pin is a TX pin, but is is not TX0 but some other number, but the big thing is that you are not using it as a TX pin so you should not label it as such. TX and RX are terms that are reserved for use when talking about asynchronous serial communication.

Understood.
Just one last thing, is the image I posted correct with a line from pin 4 to A+?
Also, the speaker I have is 4 ohm 3 watts, is that an issue for the 2.5 watt part?

is the image I posted correct with a line from pin 4 to A+?

Yes.

Also, the speaker I have is 4 ohm 3 watts, is that an issue for the 2.5 watt part?

The power capability of speaker must always be greater than the amplifier can supply, so that is no problem.

Thank you. I should be able to handle things from here.
I don't fully understand why pin 4 is audio output, but I trust you.

I don't fully understand why pin 4 is audio output

From this page https://oscarliang.com/arduino-timer-and-interrupt-tutorial/:-

On the Arduino Mega we have 6 timers and 15 PWM outputs:

Pins 4 and 13: controlled by Timer0
Pins 11 and 12: controlled by Timer1
Pins 9 and10: controlled by Timer2
Pin 2, 3 and 5: controlled by timer 3
Pin 6, 7 and 8: controlled by timer 4
Pin 46, 45 and 44:: controlled by timer 5

A timer can generate PWM and it is the PWM that is being used as an audio signal.