MIDI file player?

Post here first, or if you can't find a relevant section!
GVisser
Posts: 19
Joined: Thu Jun 11, 2020 5:17 pm
Answers: 1
Location: Hatfield, UK

Re: MIDI file player?

Post by GVisser »

Bakisha wrote:
Sun Jun 28, 2020 1:04 pm
@GVisser

After some experiment on my diy-dev board for audio experiments, here is my conclusions:
- Depending on what device you are connected, distortions are present when you are plugged in some equipment with AUDIO-IN. Most probably because it expect 1Vp-p, and you are feeding it with 3.3Vp-p.
Here is recommended audio setup:

Code: Select all

//  .-------------------------------------.
//  |                                     |
//  | STM32FxxxXXxx                       |
//  .---------------------------------|---.
//  |G                               P|
//  |N                               B|
//  |D                               0-----|R1|------|C2|----------|
//  |                                            |                 --
//  |                                            C                 || P1
//  |                                            1                 ||<----------------| OUDIO OUT
//  |                                            |                 --
//  .--------------------------------------------|-----------------|------------------| GND

//  R1 = 100-500 Ohm
//  C1 = 100-500 nF
//  C2 = 10 uF
//  P1 = 10KOhm potentiometer
Also, distortions may come from a way how author decided to create sound. Base PWM frequency of 31250Hz is, IMHO, just too low for 1-bit DAC.
maybe it's ok for 1 channel, but for four channels (used in sketch), it is noticeably full of "eeeeeks" :-)
Maybe try with higher capacitor in low-pass filter (500nF+), or higher resistor (500 Ohm+).
In my audio project, i used 250KHz base PWM frequency, that's 4uS period, meaning it is 4*72= 288 (bluepill) or 4*84=336 (STM32F401CC) values for width of that pulse. Enough for 3 channels (each channel is 1/3 of maximum of pulse width).
I think author just ported code for STM32, with all the limitation original code have (for arduino uno).

Also, i don't think it doesn't matter is it PWM1 or PWM2 mode. Beside visually in analyzer. Sound is difference in audio signal, it sound the same when you are altering between 99% HIGH - 1% LOW and 100% HIGH - 0% LOW, and 1% HIGH - 99% LOW and 100% LOW - 0% HIGH. It's the difference of 1% that is creating analog AC voltage.
Personally, i'm more into digital electronics then into analog. With digital, i'm dealing only with ones and zeroes :-)

Anyway,

Code: Select all

setOverflow(31250, HERTZ_FORMAT);
will set correct prescaler and overflow for you, no matter on what speed CPU is (and that's why i like this core: same code, no matter is it STM32F103C or STM32F401CC).
Hi again @Bakisha , Thank you for the very detailed reply!

I have done as you suggested and built the low pass filter. This has worked to limit some of the distortions but I think you are right in saying that the base pwm frequency is just too low to get decent quality audio output. I'll keep working at at, although I suspect that it is possible that the low audio quality output could also be from how the midi file is converted with the smf2seq script.

LOL Yes you are absolutely right about the PWM1 and PWM2 issue, with reference to connecting the output to a speaker, as it would make no difference. My end goal however is to use the signal to drive the interrupt of my solid state tesla coil, and in this configuration, a constant high output (PWM1) would mean death for my coil which requires pulses of less than 10% duty cycle :-)

User avatar
Bakisha
Posts: 28
Joined: Fri Dec 20, 2019 6:50 pm
Answers: 1
Contact:

Re: MIDI file player?

Post by Bakisha »

I tried to increase pwm frequency (one timer for interrupt, one for pwm), but it didn't help.
And then i looked at waveforms in oscilloscope, and everything was cleared why there is so much distortions. (i'll attach few pics from oscilloscope)
It is simply how sound is created.
On the other hand, maybe it's well suited for tesla coil...

I did audio project on STM32 (both cores), you can see and hear it at https://www.youtube.com/watch?v=pvWagY7ppYg , but i know very little about midi, to be able to adapt it, to play midi files.
Attachments
midi3.jpg
midi3.jpg (64.17 KiB) Viewed 111 times
midi2.jpg
midi2.jpg (55.28 KiB) Viewed 111 times
midi1.jpg
midi1.jpg (71.49 KiB) Viewed 111 times

ag123
Posts: 412
Joined: Thu Dec 19, 2019 5:30 am
Answers: 1

Re: MIDI file player?

Post by ag123 »

+5 nice !
i did some google searches and stumbled into this
https://github.com/keshikan/CureSynth_Whip
https://www.youtube.com/watch?v=-Fs1h-m ... e=youtu.be
based on stm32f7
my guess is doing a full wave table synth probably won't be easy, the specs, sound fonts etc are rather complex
in addition is the speeds of all that interpolation etc. i'm not too sure if stm32f401 84 mhz is fast enough to do all that computation.
perhaps a stm32f405, or stm32f407 and those running at 168mhz would have no problem weaving music playing notes and chords across 16 polyphonic channels

on linux there is the popular Fluidsynth
http://www.fluidsynth.org/
they have a bundle of soundfont2 wavetable files, one of the GM soundfont is 140MB !
https://github.com/FluidSynth/fluidsynth/wiki/SoundFont
but fluidsynth do make nice music ;)
my guess it may be possible to port fluidsynth onto the stm32f4, but it may be a herculean effort or at least a pretty involved project
among the things we don't really have a real malloc() etc, it'd take some exploration to see if dynamic memory is after all needed for that.
A sd card is most likely needed given the size of the sound font file.

but GM midi is probably the close to professional midi synth sound formats in common use.
more commonly the approach is to run fluidsynth on an RPi, with those ghz processors mmu linux etc, it is easier than do trying to run it embedded.

User avatar
Bakisha
Posts: 28
Joined: Fri Dec 20, 2019 6:50 pm
Answers: 1
Contact:

Re: MIDI file player?

Post by Bakisha »

ag123 wrote:
Mon Jun 29, 2020 7:56 pm
my guess is doing a full wave table synth probably won't be easy, the specs, sound fonts etc are rather complex
in addition is the speeds of all that interpolation etc. i'm not too sure if stm32f401 84 mhz is fast enough to do all that computation.
perhaps a stm32f405, or stm32f407 and those running at 168mhz would have no problem weaving music playing notes and chords across 16 polyphonic channels
During coding emulator for sound chip, I got some insight how sound is created with digital electronics. I might have few ideas how to create, at least basic waveforms (sawtooth, Triangle, noise). All with software counters, and minimal or even without ADSR. As I understand, MIDI is just notes and duration of one of 127 available instruments. If simplified enough, maybe could be possible to squeeze 4 or 8 single channels inside interrupt routine, leaving main program to change/start/stop instrument.
But, it's huge coding challenge, because midi can be read via serial communication (from another midi device), or a midi file (as in internal memory or maybe reading from SD card).

GVisser
Posts: 19
Joined: Thu Jun 11, 2020 5:17 pm
Answers: 1
Location: Hatfield, UK

Re: MIDI file player?

Post by GVisser »

Hi @ag123 and @Bakisha ,

I agree that the F401 is extremely limited both from a computational speed and memory point of view, I was just hoping that it would have been suitable for me as I am only dealing with relatively small midi files and because the tesla coil as an output device can only handle 2 channels at most because of it's heavily limited bandwidth.

I am due to do a test of the work we have done recently with my coil and will report back, but I think the final solution may well be that I use my F4 as the general purpose interrupter and move the midi interrupter over to something like a Raspberry Pi or similar running linux.

Oh and ...WOW ... Keshikan's CureSynth is very impressive! I wish I was that clever :-)

ag123
Posts: 412
Joined: Thu Dec 19, 2019 5:30 am
Answers: 1

Re: MIDI file player?

Post by ag123 »

i've thought about the huge size of the sound fonts e.g. 100 megs, that may possibly be made smaller with the help of fft.
lets just say we use a 1024 bin FFT and 16 bits for each of the real and imaginary parts of that FFT, so it takes 4 bytes for each bin
4K for each instrument 512K for the full set of 128 GM instruments. it would seem pretty feasible to construct a wave table that fits in an mcu that way.
still 512K is 'large' for many stm32fx and an external SPI flash is probably needed to keep that wave table at least.
the other thing that is somewhat harder to guess is how much memory and cpu speeds we'd need to reconstruct the original notes or chords (i think it is called voices for each note in that domain), so perhaps if we consider 32 polyphonic notes (voices) it would be necessary to work that IFFT for the 1024 bins x 2 (im, re) x 32 notes in real time with scaling / translation to account for things like the note, midi velocity etc. the mcu would need to be really fast to do all that calcs in real time.

to oversimply things we can simply consider the midi file, which we'd imagine to be time and midi events (commands) e.g. with the note-on event we'd allocate memory 'slots' for each set of note-on midi events that plays at the same time, thus forming a chord and start computing the output waveform.
in that way, if all these is feasible, we'd have our GM (general midi) 128 instruments midi sequencer ;)

if we bother with such herculean effort, we might as well get a good DAC e.g. PCM5102 or an stm32 with a dac (e.g. stm32f405, stm32f407)
https://www.aliexpress.com/wholesale?ca ... xt=PCM5102
but of course nothing stops doing a delta-sigma dac or a pwm dac, the h/w timers are there after all

it seemed rather feasible to implement such a setup on a stm32f405rg as that has 1M flash (for the wavetable) and modest amount of sram
https://www.st.com/en/microcontrollers- ... 405rg.html
these boards are easy to get it is the micropython boards
https://store.micropython.org/product/PYBv1.1H
adafruit feather f405
https://www.adafruit.com/product/4382
olimex h405
https://www.olimex.com/Products/ARM/ST/STM32-H405/
and the rest from ebay/aliexpress vendors

here is a reall one that seemed to be built around that concept
https://hackaday.com/2017/02/05/buildin ... ble-synth/
http://people.ece.cornell.edu/land/cour ... index.html

Post Reply

Return to “General discussion”