-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsyntezer.cpp
116 lines (96 loc) · 2.66 KB
/
syntezer.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#include <QTimer>
#include <QSettings>
#include <QFuture>
#include <stdio.h>
#include <stdlib.h>
#include "syntezer.h"
#include "alsadriver.h"
#include "puseaudiodriver.h"
Syntezer::Syntezer()
: QObject()
{
QSettings settings("./settings/settings.ini", QSettings::IniFormat);
if( settings.value("use_driver").toString() == "alsa" )
{
alsa = new AlsaDriver();
alsa->open((char*)settings.value("alsa_device").toString().toStdString().c_str(),false);
pulse = 0;
}else if(settings.value("use_driver").toString() == "pulse")
{
pulse = new PuseAudioDriver();
//pulse->open();
alsa = 0;
}
//connect(this, SIGNAL(sigDisableNote(char)),&alsa->mixer_thread, SLOT(disable_note(char)));
bKeyPressed = false;
bEnabled = false;
QTimer* timer = new QTimer();
connect(timer,SIGNAL(timeout()), this, SLOT(on_Timer()));
timer->start(1000);
}
void Syntezer::on_Timer()
{
}
void Syntezer::out_pcm(short* buffer, int len)
{
if(!bEnabled) return;
if(alsa)
{
alsa->drop_pcm_frames();
alsa->out_pcm(buffer, (unsigned long)len);
}
}
void Syntezer::out_buffer(Buffer* buf)
{
if(!bEnabled) return;
if(alsa) alsa->out_buffer(buf);
if(pulse) pulse->out_buffer(buf);
}
void Syntezer::drop_pcm_frames()
{
if(!bEnabled) return;
if(alsa) alsa->drop_pcm_frames();
}
void Syntezer::on_key_press(int key_code)
{
if(key_code > 128) return;
if(!bEnabled) return;
fprintf(stderr,"keycode=%d", key_code);
if(key2noteBuffer.find(key_code) == key2noteBuffer.end())
{
Buffer* buffer = play_note(key2note[key_code], 10, 1);
buffer->bWrited = false;
key_time[key_code].start();
bKeyPressed = true;
key2noteBuffer[key_code] = buffer;
}
}
void msleep(int ms){
static QMutex mutex;
static QMutexLocker locker(&mutex);
mutex.tryLock(ms);
}
void Syntezer::on_key_release(int key_code)
{
bKeyPressed = false;
if(!bEnabled) return;
//emit sigDisableNote(key2note[key_code]);
Buffer* buf=0;
bDoOutBuffer = true;
bAsynch = false;
if(key2noteBuffer.find(key_code)!=key2noteBuffer.end())
{
buf = key2noteBuffer[key_code];
buf->bWrited = true;
//QThread::currentThread()->msleep(50);
//fprintf(stderr,"disable note");
msleep(50);
double time_release = release_note(buf, key2note[key_code], (double)key_time[key_code].elapsed()/1000.) ;
buf->timeEnd = QTime::currentTime().addSecs(time_release);
key2noteBuffer.erase(key2noteBuffer.find(key_code));
}
}
bool Syntezer::isChannelEnabled(int n_channel)
{
return (enabledChannels >> n_channel)&1;
}