ofApp.h 2.61 KB
Newer Older
Paul Bethge's avatar
init  
Paul Bethge committed
1
/*
2
 * Language Identifier
Paul Bethge's avatar
init  
Paul Bethge committed
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
 *
 * Copyright (c) 2021 ZKM | Hertz-Lab
 * Paul Bethge <bethge@zkm.de>
 * Dan Wilcox <dan.wilcox@zkm.de>
 *
 * BSD Simplified License.
 * For information on usage and redistribution, and for a DISCLAIMER OF ALL
 * WARRANTIES, see the file, "LICENSE.txt," in this distribution.
 *
 * This code has been developed at ZKM | Hertz-Lab as part of „The Intelligent 
 * Museum“ generously funded by the German Federal Cultural Foundation.
 */

#pragma once

#include "ofMain.h"
#include "ofxTensorFlow2.h"
20
#include "ofxOsc.h"
Paul Bethge's avatar
init  
Paul Bethge committed
21

22
23
#include "AudioClassifier.h"
#include "Labels.h"
Paul Bethge's avatar
init  
Paul Bethge committed
24
25
26
27
28
29
30

class ofApp : public ofBaseApp {

	public:

		void setup();
		void update();
31
		void exit();
Paul Bethge's avatar
init  
Paul Bethge committed
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
		void draw();

		void audioIn(ofSoundBuffer & input);

		void keyPressed(int key);
		void keyReleased(int key);
		void mouseMoved(int x, int y);
		void mouseDragged(int x, int y, int button);
		void mousePressed(int x, int y, int button);
		void mouseReleased(int x, int y, int button);
		void mouseEntered(int x, int y);
		void mouseExited(int x, int y);
		void windowResized(int w, int h);
		void dragEvent(ofDragInfo dragInfo);
		void gotMessage(ofMessage msg);

		// audio 
		ofSoundStream soundStream;

		// for ease of use:
		// we want to keep the buffersize a multiple of the downsampling factor
		// downsamplingFactor = micSamplingRate / neuralNetworkInputSamplingRate 
		std::size_t downsamplingFactor;
		std::size_t bufferSize;
		std::size_t samplingRate;
		
		// since volume detection has some latency we keep a history of buffers
		AudioBufferFifo previousBuffers;
		std::size_t numPreviousBuffers;
		// sampleBuffers acts as a buffer for recording (could be fused)
		AudioBufferFifo sampleBuffers;
		std::size_t numBuffers;
		
		// volume
		float curVol = 0.0;
		float smoothedVol = 0.0;
		float scaledVol = 0.0;
		float volThreshold = 25;

		// display
		std::vector<float> volHistory;
		std::string displayLabel = " ";
		float minConfidence = 0.75;

		// neural network	
		AudioClassifier model;
		cppflow::tensor output;
		std::size_t inputSeconds;
		std::size_t inputSamplingRate;
		std::size_t inputSize;

		// neural network control logic
		std::size_t recordingCounter = 0;
		bool trigger = false;
		bool enable = true;
		bool recording = true;
Dan Wilcox's avatar
Dan Wilcox committed
88
89
		bool blink = true; // recording blink state
		float blinkTimestamp = 0; // blink timestamp
90

91

Dan Wilcox's avatar
Dan Wilcox committed
92
		// osc
93
94
95
96
97
98
99
100
101
102
		struct OscClient {
			std::string address;
			int port;
		};
		std::vector<OscClient> hosts = {
			// add multiple send host address/port pairs if needed
			{"localhost", 9999},
			//{"239.200.200.200", 5000} // multicast
		};
		std::vector<ofxOscSender*> senders;
Dan Wilcox's avatar
Dan Wilcox committed
103
		bool recordingStarted = false;
Paul Bethge's avatar
init  
Paul Bethge committed
104
};