hydrogen 1.2.3
Synth.cpp
Go to the documentation of this file.
1/*
2 * Hydrogen
3 * Copyright(c) 2002-2008 by Alex >Comix< Cominu [comix@users.sourceforge.net]
4 * Copyright(c) 2008-2024 The hydrogen development team [hydrogen-devel@lists.sourceforge.net]
5 *
6 * http://www.hydrogen-music.org
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY, without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see https://www.gnu.org/licenses
20 *
21 */
22
23
24#include <core/Synth/Synth.h>
25#include <core/Basics/Note.h>
26#include <core/Globals.h>
27
28#include <cassert>
29#include <cmath>
30
31namespace H2Core
32{
33
35 : Object()
36{
37
38
39 m_pOut_L = new float[ MAX_BUFFER_SIZE ];
40 m_pOut_R = new float[ MAX_BUFFER_SIZE ];
41
42 m_fTheta = 0.0;
43
44 m_pAudioOutput = nullptr;
45}
46
47
48
50{
51 INFOLOG( "DESTROY" );
52 delete[] m_pOut_L;
53 delete[] m_pOut_R;
54}
55
56
57
58
59void Synth::noteOn( Note* pNote )
60{
61 INFOLOG( "NOTE ON" );
62 assert( pNote );
63
64 m_playingNotesQueue.push_back( pNote );
65}
66
67
68
69
70void Synth::noteOff( Note* pNote )
71{
72 INFOLOG( "NOTE OFF - not implemented yet" );
73 assert( pNote );
74
75 // delete the older note...
76 for ( uint i = 0; i < m_playingNotesQueue.size(); ++i ) {
77 Note *pPlayingNote = m_playingNotesQueue[ i ];
78 if ( pPlayingNote->get_instrument() == pNote->get_instrument() ) {
79 m_playingNotesQueue.erase( m_playingNotesQueue.begin() + i );
80 delete pPlayingNote;
81
82 delete pNote;
83 pNote = nullptr;
84 break;
85 }
86 }
87
88 ERRORLOG( "note not found" );
89}
90
91
92
93// perche' viene passata anche la canzone? E' davvero necessaria?
94void Synth::process( uint32_t nFrames )
95{
96 //INFOLOG( "process" );
97
98 // cleanup of the output buffers
99 memset( m_pOut_L, 0, nFrames * sizeof( float ) );
100 memset( m_pOut_R, 0, nFrames * sizeof( float ) );
101
102
103 for ( const auto& pPlayingNote : m_playingNotesQueue ) {
104 //pPlayingNote->dumpInfo();
105
106 float fAmplitude = pPlayingNote->get_velocity();
107 float fFrequency = TWOPI * 220.0 / 44100.0;
108
109 for ( uint i = 0; i < nFrames; ++i ) {
110 float fVal = sin( m_fTheta ) * fAmplitude;
111 m_pOut_L[ i ] += fVal;
112 m_pOut_R[ i ] += fVal;
113
114 m_fTheta += fFrequency;
115 }
116 }
117}
118
119
121{
122 m_pAudioOutput = pAudioOutput;
123}
124
125
126
127} // namespace H2Core
#define INFOLOG(x)
Definition Object.h:237
#define ERRORLOG(x)
Definition Object.h:239
Base abstract class for audio output classes.
Definition AudioOutput.h:39
A note plays an associated instrument with a velocity left and right pan.
Definition Note.h:102
std::shared_ptr< Instrument > get_instrument()
__instrument accessor
Definition Note.h:500
void process(uint32_t nFrames)
Definition Synth.cpp:94
float * m_pOut_R
Definition Synth.h:47
void noteOff(Note *pNote)
Stop playing a note.
Definition Synth.cpp:70
Synth()
Constructor of the Synth.
Definition Synth.cpp:34
float * m_pOut_L
Definition Synth.h:46
void noteOn(Note *pNote)
Start playing a note.
Definition Synth.cpp:59
std::vector< Note * > m_playingNotesQueue
Definition Synth.h:73
void setAudioOutput(AudioOutput *pAudioOutput)
Definition Synth.cpp:120
float m_fTheta
Definition Synth.h:75
AudioOutput * m_pAudioOutput
Definition Synth.h:76
#define MAX_BUFFER_SIZE
Maximum buffer size.
Definition config.dox:87
#define TWOPI
Definition Globals.h:40