-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEnGen.cpp
115 lines (98 loc) · 2.93 KB
/
EnGen.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
//
// EnGen.cpp
// SqueakTester
//
// Created by Ethan Geller on 11/22/14.
// Copyright (c) 2014 Ethan Geller. All rights reserved.
//
#include "EnGen.h"
EnGen::EnGen(){
isCurrentlyPlayingSound = false;
isCurrentlyPlayingAmbience = false;
soundRate = 1.0;
ambRate = 1.0;
}
void EnGen::setCurrentSound(SoundSourceGen s){
soundSize = s.getSize();
currentSound = s.getSound();
}
void EnGen::setCurrentSound(float *snd, int size){
soundSize = size;
currentSound = snd;
}
void EnGen::setCurrentAmbience(BackgroundGen bgnd){
//FIXME: this might be an inheritence issue
ambienceSize = bgnd.getSize();
ambientSound = bgnd.getSound();
isCurrentlyPlayingAmbience = true;
}
void EnGen::setCurrentAmbience(float *snd, int size){
ambienceSize = size;
ambientSound = snd;
isCurrentlyPlayingAmbience = true;
}
void EnGen::setSoundPlayback(float f){
soundRate = f;
}
void EnGen::setAmbPlayback(float f){
ambRate = f;
}
void EnGen::stopPlayingAmbience(){
isCurrentlyPlayingAmbience = false;
}
void EnGen::playCurrentSound(){
soundPlayhead = 0;
isCurrentlyPlayingSound = true;
}
void EnGen::playSound(SoundSourceGen s){
setCurrentSound(s);
playCurrentSound();
}
bool EnGen::synthesize2(float *input, float *output, int numframes){
//playing both
if(isCurrentlyPlayingSound && isCurrentlyPlayingAmbience){
for (int i = 0; i < numframes; i++) {
if (soundPlayhead >= soundSize*2) {
isCurrentlyPlayingSound = false;
}
if (ambiencePlayhead >= ambienceSize)
ambiencePlayhead = 0;
output[i*2] = (currentSound[(int)soundPlayhead*2] + ambientSound[(int)ambiencePlayhead*2]);
output[i*2+1] =currentSound[(int)soundPlayhead*2+1] + ambientSound[(int)ambiencePlayhead*2+1];
soundPlayhead+= soundRate;
ambiencePlayhead+= ambRate;
}
}
//ambience, no sound
else if(isCurrentlyPlayingAmbience){
for (int i = 0; i < numframes; i++) {
if (ambiencePlayhead >= ambienceSize) {
ambiencePlayhead = 0;
}
output[i*2] = ambientSound[(int)ambiencePlayhead*2];
output[i*2+1] =ambientSound[(int)ambiencePlayhead*2+1];
ambiencePlayhead+= ambRate;
}
}
//sound, no ambience
else if(isCurrentlyPlayingSound){
for (int i = 0; i < numframes; i++) {
if (soundPlayhead >= soundSize*2) {
isCurrentlyPlayingSound = false;
}
else{
output[i*2] = currentSound[(int)soundPlayhead*2];
output[i*2+1] =currentSound[(int)soundPlayhead*2+1];
soundPlayhead+= soundRate;
}
}
}
//nothing!
else {
for (int i = 0; i < numframes; i++) {
output[i*2] = 0;
output[i*2+1] = 0;
}
}
return true;
}