Simple synthesizer for STM32F401RE/STMstation.

Dependencies:   FastPWM mbed-dsp

This is a basic synthesizer to play music on the STM32F401RE Nucleo/STMstation development board:

/media/uploads/kkado/imgp1229.jpg

Please see the API documentation for further details.

Here's a demo of the synthesizer at work in a music composing program on the STMstation. This one is "Miku" by Anamanaguchi.

STMstation_synth.h

Committer:
kkado
Date:
2017-07-03
Revision:
0:c5ca205c0a80
Child:
1:db0c24aebb8a

File content as of revision 0:c5ca205c0a80:

#ifndef STMstation_synth_h
#define STMstation_synth_h

#include "mbed.h"
#include "arm_math.h"
#include "FastPWM.h"

/** Basic synthesizer library for STM32F401RE Nucleo or STMstation P.1 development boards - may work with
 *  other targets, but not tested yet.
 *
 *  This is a multi-channel synthesizer, with sine, square, triangle, and noise waveform generation. Music is 
 *  stored as arrays in flash, but it is possible to use editable arrays in RAM. Standard number of channels is
 *  5, but additional channels can be added by changing the CHANNELS define.
 *  
 *  Audio signal is output as a PWM signal on PB_0, which MUST be fed into a 1-bit DAC to generate sound!
 *
 *  Suggested setup:
 *  @code
 *      (PB_0)-----(R1)-----(C2) 
 *                  |        |
 *                 (C1)  (SPEAKER)
 *                  |        |
 *                 (GND)    (GND)
 *      
 *      R1 = 150 Ohms, C1 = 47nF, C2 = 1uF, Speaker = 0.25W, 8 Ohms
 *  @endcode 
 */

#define AUDIO_PIN   PB_0
#define CHANNELS    5

//Audio melody struct
struct melody{
    const uint8_t * notes[CHANNELS];
    const uint8_t * durations[CHANNELS]; 
    const uint8_t * AR[CHANNELS]; 
    const uint8_t * vol[CHANNELS];
    uint16_t max[CHANNELS];
    bool ended[CHANNELS], repeat[CHANNELS];
    uint8_t bpm;
};

//Audio master struct
struct master{
    const uint8_t * notes[CHANNELS];
    const uint8_t * durations[CHANNELS]; 
    const uint8_t * AR[CHANNELS]; 
    const uint8_t * vol[CHANNELS];
    uint16_t index[CHANNELS], max[CHANNELS];
    float env[CHANNELS], sineCoef[CHANNELS], vSum, atkSlope[CHANNELS], relSlope[CHANNELS], relOffset[CHANNELS], volCoef[CHANNELS], halfPeriod[CHANNELS], triSlope[CHANNELS], noiseVal[CHANNELS], triVal[CHANNELS];
    uint32_t counter[CHANNELS], envAtkEnd[CHANNELS], envRelStart[CHANNELS];
    uint8_t val, timbre[CHANNELS], freqIndex[CHANNELS], bpm[CHANNELS];
    bool repeat[CHANNELS];
    bool* endptr[CHANNELS];
};

class STMstation_synth{
    public:
        STMstation_synth();
        
        void calc_coefs(int i);
        void calc_vSum2();
        void calc_env();
        int8_t square(float _halfperiod, uint16_t _counter);
        void calc_triangle(uint8_t _channel, float _halfperiod, float _trislope, uint32_t _counter);
        void calc_noise(uint8_t _channel, uint8_t _freq, uint32_t _counter);
        void calc_val2();
        void clear_channel(uint8_t _channel);
        void stop_track(melody &newMelody);
        bool check_track(melody &newMelody);
        void check_end();
        void check_start();
        void note2();
        void play(melody &newMelody, uint8_t refChannel = 0, uint16_t newIndex = 0);
        
        const uint16_t FSAMP;
        const float period;
        struct master Master;
    private:
        Ticker sample;
        FastPWM tone;
};

#endif