Mozzi  version v2.0
sound synthesis library for Arduino
Portamento.h
1 /*
2  * Portamento.h
3  *
4  * This file is part of Mozzi.
5  *
6  * Copyright 2012-2024 Tim Barrass and the Mozzi Team
7  *
8  * Mozzi is licensed under the GNU Lesser General Public Licence (LGPL) Version 2.1 or later.
9  *
10  */
11 
12 #ifndef PORTAMENTO_H_
13 #define PORTAMENTO_H_
14 
15 #include "mozzi_midi.h"
16 #include "mozzi_fixmath.h"
17 #include "Line.h"
18 
21 template <unsigned int CONTROL_UPDATE_RATE>
22 class
23  Portamento {
24 
25 public:
26 
30  MICROS_PER_CONTROL_STEP(1000000/CONTROL_UPDATE_RATE)
31  {
32  }
33 
37  inline
38  void setTime(unsigned int milliseconds){
39  //control_steps_per_portamento = ((long)milliseconds*1000)/MICROS_PER_CONTROL_STEP; // more accurate but slower
40  control_steps_per_portamento = convertMsecToControlSteps(milliseconds);
41  }
42 
46  inline
47  void start(uint8_t note) {
48  target_freq = Q16n16_mtof(Q8n0_to_Q16n16(note));
49  aPortamentoLine.set(target_freq, control_steps_per_portamento);
50  countdown = control_steps_per_portamento;
51  portamento_on=true;
52  }
53 
57  inline
58  void start(Q16n16 note) {
59  target_freq = Q16n16_mtof(note);
60  aPortamentoLine.set(target_freq, control_steps_per_portamento);
61  countdown = control_steps_per_portamento;
62  portamento_on=true;
63  }
64 
65 
71  inline
73  if (portamento_on==true){
74  if(--countdown < 0) {
75  // stay level when portamento has finished
76  aPortamentoLine.set(target_freq, target_freq, control_steps_per_portamento);
77  portamento_on=false;
78  }
79  }
80  return aPortamentoLine.next();
81  }
82 
83  private:
84 
85  int countdown;
86  int control_steps_per_portamento;
87  Q16n16 target_freq;
88  bool portamento_on;
89  const unsigned int MICROS_PER_CONTROL_STEP;
90  Line <Q16n16> aPortamentoLine;
91 
92 
93  // copied from ADSR.h
94  inline
95  static const unsigned int convertMsecToControlSteps(unsigned int msec){
96  return (uint16_t) (((uint32_t)msec*CONTROL_UPDATE_RATE)>>10); // approximate /1000 with shift
97  }
98 
99 };
100 
106 #endif /* PORTAMENTO_H_ */
A simple portamento (pitch slide from one note to the next) effect, useful for note-based application...
Definition: Portamento.h:23
void start(uint8_t note)
Call this at note-on, it initialises the portamento.
Definition: Portamento.h:47
Q16n16 next()
Use this in updateControl() to provide a frequency to the oscillator it's controlling.
Definition: Portamento.h:72
Portamento()
Constructor.
Definition: Portamento.h:29
void setTime(unsigned int milliseconds)
Set how long it will take to slide from note to note, in milliseconds.
Definition: Portamento.h:38
void start(Q16n16 note)
Call this at note-on, it initialises the portamento.
Definition: Portamento.h:58
Q16n16 Q8n0_to_Q16n16(Q8n0 a)
Convert Q8n0 uint8_t to Q16n16 fix.
uint32_t Q16n16
unsigned fractional number using 16 integer bits and 16 fractional bits, represents 0 to 65535....
Definition: mozzi_fixmath.h:52
Q16n16 Q16n16_mtof(Q16n16 midival_fractional)
Converts midi note number to frequency with speed and accuracy.
Definition: mozzi_midi.h:119