Playing more than one note at once using the bleeper (or another) API?

14 Mar 2010

Ok, this will be hard to explain other than to just program in MyBrainLanguage and put it on here...

PlayNote1(D,5)//do not wait for the end of this note before triggering the next
PlayNote2(G,5)
As you can see from the comment, it would trigger the note, and let it play whilst going onto the next note.

Is this possible? Or is it just physically impossible with the hardware?

I know I could make it trigger connections to another PIC, but then, that turns out rather expensive.

For those wondering of the purpose, I want to make something that plays the introduction of Baba O'Reilly by The Who over and over again until you smash it up :P

 

Thanks again, and apologies for posting so much! Will try not to post so much in future...

14 Mar 2010

I don't know what's possible with the sort of piezo based approach used by bleeper but if you do all your audio processing yourself (i.e. you plug some speakers into PWM out + ground) like the Synth library does (http://mbed.org/projects/cookbook/wiki/Synth) then it's certainly possible hardware wise (iirc it should just be a matter of adding together the PCM data), but mixing isn't actually implemented in the Synth library at the moment (it was on my todo list, but I never got around to it).

14 Mar 2010

Will take a look and experiment with that, Mike. Cheers :)

As a side-note, I was thinking of using BusOut to send out multiple signals, but it's setting the note that I would find difficult.

15 Mar 2010

Ok, so I've found this sweet MIDI API, which would let me have multiple notes going, but I really want to use a piezo. At least for part of it. So, now it comes into two  questions -

Can the MIDI API be used to output to a piezo (my guess is no, but then I know little about electronics, and, for that matter, MIDI)?

If not, how can I trigger more than one note at the same time using an API similar to Beeper which will let me output to a piezo? (See my first post for that question asked a little clearer...)

 

Thanks,

Dom.

15 Mar 2010

To clarify the MyBrainLanguage code, it should be a bit more like this...

 

NoteOn(60)//Start a note with a frequency of 60Hz
NoteOn(70)//Start a note with a frequency of 70hz
NoteOff(60)//Stop the note with the frequency of 60Hz
NoteOff(70)//Stop the note with the frequency of 70Hz
15 Mar 2010

I think I may have to go with the MIDI option, but...

Is there a way to hook this directly to a speaker? I know MIDI is designed to communicate with a computer, but...

18 Mar 2010

For future reference to anyone who is interested in this, I am modifying the Beeper API so that you call the stop function rather than have a set time, and so that several instances can be called, each outputting to a different pin.

I hope this works!

18 Mar 2010 . Edited: 18 Mar 2010

OK, I've modified it so that I think it will work...How do I compile this to an API/Library?

18 Mar 2010

Am I right in thinking that PWM can only write one different thing at once? Even through multiple pins?

18 Mar 2010

Hi Dom,

The PWM hardware channels can each set the pulsewidth independently, but not the period; that is (unfortunately) shared among all channels. See the implementation notes at:

Looking at what you are trying to do in this thread, I think the aim is to make multiple notes at the same time. You might find the best way is actually a very simple sound synth e.g. put out a analog value at a fixed rate (e.g. 8khz), and calucalte each time what it should be based on the composition of your desired frequencies. e.g. make a simple sample based saw/triangle/square wave generator, have a number of them and mix the values to decide what to output.

A quick example (sorry, not tested, so might have not quite thought it through):

#include "mbed.h"

AnalogIn note1(p15);
AnalogIn note2(p16);
AnalogIn note3(p17);

AnalogOut sounder(p18);

int main() {

    Timer t;
    t.start();

    float samplerate = 8000;

    float output = 0.0;
    
    while(1) {

        float freq1 = 220 + 880 * note1;
        float freq2 = 220 + 880 * note2;
        float freq3 = 220 + 880 * note3;

        float delta1 = freq1 / samplerate;
        float delta2 = freq2 / samplerate;
        float delta3 = freq3 / samplerate;
        
        output += delta1 + delta2 + delta3;
        output = fmod(output, 1.0f);
                   
        while(t < (1.0 / samplerate));
        t.reset();
        sounder = output;
    }
}
I.e. three triangle waves controlled by analog ins, updating the analog output every samplerate.

Simon

18 Mar 2010

Thanks.

I'm assuming this is with a speaker, right?