-
Notifications
You must be signed in to change notification settings - Fork 1
/
SoundInput.cpp
executable file
·112 lines (79 loc) · 1.97 KB
/
SoundInput.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
/*
* SoundInput.cpp
* openframeworksQT
*
* Created by mads hobye on 09/08/09.
* Copyright 2009 __MyCompanyName__. All rights reserved.
*
*/
#include "SoundInput.h"
void SoundInput::init(myData * _data, QVBoxLayout * boxLayout)
{
addBaseGui(boxLayout);
data = _data;
nameid = "SoundInput";
bufferCounter = 0;
drawCounter = 0;
smoothVolume = 0;
fastVolume = 0;
beat = false;
QGroupBox * sliderArea = new QGroupBox("Threshold");
QGridLayout *sliderAreaLayout = new QGridLayout;
sliderArea->setLayout(sliderAreaLayout);
boxLayout->addWidget(sliderArea);
sliderLower = AddQSlider("Lover",1,1,500,sliderAreaLayout,1,true);
sliderHigher = AddQSlider("Higher", 1, 1, 500, sliderAreaLayout, 2, true);
}
void SoundInput::update()
{
float tmpval = 0;
if (data->newSound)
{
for (int i = 0; i < 256; i++)
{
tmpval = data->soundLeft[i] * 100;
if ( tmpval < 0)
{
tmpval = tmpval * -1;
}
smoothVolume = smoothVolume * 0.9999f + tmpval * 0.0001f;
fastVolume = fastVolume * 0.7f + tmpval * 0.3f;
}
data->newSound = false;
}
if(fastVolume > smoothVolume && !beat && smoothVolume > sliderHigher->value())
{
beat = true;
}
else if (fastVolume < smoothVolume && smoothVolume < sliderLower->value())
{
beat = false;
}
if (fastVolume > smoothVolume )
{
smoothVolume = fastVolume;
}
}
void SoundInput::draw()
{
// draw the left:
ofSetColor(0x333333);
ofRect(100,100,256,200);
ofSetColor(0xFFFFFF);
for (int i = 0; i < 256; i++){
ofLine(100+i,200,100+i,200+data->soundLeft[i]*100.0f);
}
// draw the right:
ofSetColor(0x333333);
ofRect(600,100,256,200);
ofSetColor(0xFFFFFF);
for (int i = 0; i < 256; i++){
ofLine(600+i,200,600+i,200+data->soundRight[i]*100.0f);
}
ofSetColor(0x333333);
ofBackground(255 * beat, 0, 0);
drawCounter++;
char reportString[255];
sprintf(reportString, "fastvolume: %f\n smooth volume: %f\n", fastVolume,smoothVolume);
ofDrawBitmapString(reportString,80,380);
}