Skip to content

Commit a543449

Browse files
author
falkTX
committed
Import initial code
0 parents  commit a543449

File tree

532 files changed

+109502
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

532 files changed

+109502
-0
lines changed

Diff for: hylia.cpp

+103
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
/*
2+
* This file is part of Hylia.
3+
*
4+
* Hylia is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation, either version 2 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* Hylia is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with Hylia. If not, see <http://www.gnu.org/licenses/>.
16+
*/
17+
18+
#include "mod-link.h"
19+
20+
#include "AudioEngine.hpp"
21+
#include "ableton/link/HostTimeFilter.hpp"
22+
#include <chrono>
23+
24+
class HyliaTransport {
25+
public:
26+
HyliaTransport(double bpm, double bufferSize, double sampleRate)
27+
: link(bpm),
28+
engine(link),
29+
outputLatency(0),
30+
sampleTime(0)
31+
{
32+
outputLatency = std::chrono::microseconds(llround(1.0e6 * bufferSize / sampleRate));
33+
}
34+
35+
void setEnabled(bool enabled, double bpm)
36+
{
37+
link.enable(enabled);
38+
39+
if (enabled)
40+
{
41+
sampleTime = 0;
42+
engine.setTempo(bpm);
43+
}
44+
}
45+
46+
void setTempo(double tempo)
47+
{
48+
engine.setTempo(tempo);
49+
}
50+
51+
void process(uint32_t frames, LinkTimeInfo* info)
52+
{
53+
const auto hostTime = hostTimeFilter.sampleTimeToHostTime(sampleTime);
54+
const auto bufferBeginAtOutput = hostTime + outputLatency;
55+
56+
engine.timelineCallback(bufferBeginAtOutput, info);
57+
58+
sampleTime += frames;
59+
}
60+
61+
private:
62+
ableton::Link link;
63+
ableton::linkaudio::AudioEngine engine;
64+
65+
ableton::link::HostTimeFilter<ableton::platforms::stl::Clock> hostTimeFilter;
66+
std::chrono::microseconds outputLatency;
67+
uint32_t sampleTime;
68+
};
69+
70+
hylia_t* hylia_create(double bpm, uint32_t buffer_size, uint32_t sample_rate)
71+
{
72+
HyliaTransport* t;
73+
74+
try {
75+
t = new HyliaTransport(bpm, buffer_size, sample_rate);
76+
} catch (...) {
77+
return nullptr;
78+
}
79+
80+
return (hylia_t*)t;
81+
}
82+
83+
void hylia_enable(hylia_t* link, bool on, double bpm)
84+
{
85+
((HyliaTransport*)link)->setEnabled(on, bpm);
86+
}
87+
88+
void hylia_set_tempo(hylia_t* link, double bpm)
89+
{
90+
((HyliaTransport*)link)->setTempo(bpm);
91+
}
92+
93+
void hylia_process(hylia_t* link, uint32_t frames, hylia_time_info_t* info)
94+
{
95+
((HyliaTransport*)link)->process(frames, (LinkTimeInfo*)info);
96+
}
97+
98+
void hylia_cleanup(hylia_t* link)
99+
{
100+
delete (HyliaTransport*)link;
101+
}
102+
103+
#include "AudioEngine.cpp"

Diff for: hylia.h

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* This file is part of Hylia.
3+
*
4+
* Hylia is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation, either version 2 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* Hylia is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with Hylia. If not, see <http://www.gnu.org/licenses/>.
16+
*/
17+
18+
#ifndef MOD_LINK_H_INCLUDED
19+
#define MOD_LINK_H_INCLUDED
20+
21+
#include <stdint.h>
22+
23+
#ifdef __cplusplus
24+
extern "C" {
25+
#else
26+
#include <stdbool.h>
27+
#endif
28+
29+
typedef struct _hylia_t hylia_t;
30+
31+
typedef struct _hylia_time_info_t {
32+
double bpm, beats, phase;
33+
} hylia_time_info_t;
34+
35+
hylia_t* hylia_create(double bpm, uint32_t buffer_size, uint32_t sample_rate);
36+
void hylia_enable(hylia_t* link, bool on, double bpm);
37+
void hylia_process(hylia_t* link, uint32_t frames, hylia_time_info_t* info);
38+
void hylia_set_tempo(hylia_t* link, double tempo);
39+
void hylia_cleanup(hylia_t* link);
40+
41+
#ifdef __cplusplus
42+
}
43+
#endif
44+
45+
#endif // MOD_LINK_H_INCLUDED

Diff for: link/AudioEngine.cpp

+107
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
/* Copyright 2016, Ableton AG, Berlin. All rights reserved.
2+
*
3+
* This program is free software: you can redistribute it and/or modify
4+
* it under the terms of the GNU General Public License as published by
5+
* the Free Software Foundation, either version 2 of the License, or
6+
* (at your option) any later version.
7+
*
8+
* This program is distributed in the hope that it will be useful,
9+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
10+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11+
* GNU General Public License for more details.
12+
*
13+
* You should have received a copy of the GNU General Public License
14+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
15+
*
16+
* If you would like to incorporate Link into a proprietary software application,
17+
* please contact <link-devs@ableton.com>.
18+
*/
19+
20+
#include "AudioEngine.hpp"
21+
22+
// Make sure to define this before <cmath> is included for Windows
23+
#define _USE_MATH_DEFINES
24+
#include <cmath>
25+
26+
namespace ableton
27+
{
28+
namespace linkaudio
29+
{
30+
31+
AudioEngine::AudioEngine(Link& link)
32+
: mLink(link)
33+
, mSharedEngineData({0., false, 4.})
34+
{
35+
}
36+
37+
double AudioEngine::beatTime() const
38+
{
39+
const auto timeline = mLink.captureAppTimeline();
40+
return timeline.beatAtTime(mLink.clock().micros(), mSharedEngineData.quantum);
41+
}
42+
43+
void AudioEngine::setTempo(double tempo)
44+
{
45+
std::lock_guard<std::mutex> lock(mEngineDataGuard);
46+
mSharedEngineData.requestedTempo = tempo;
47+
}
48+
49+
double AudioEngine::quantum() const
50+
{
51+
return mSharedEngineData.quantum;
52+
}
53+
54+
void AudioEngine::setQuantum(double quantum)
55+
{
56+
std::lock_guard<std::mutex> lock(mEngineDataGuard);
57+
mSharedEngineData.quantum = quantum;
58+
mSharedEngineData.resetBeatTime = true;
59+
}
60+
61+
AudioEngine::EngineData AudioEngine::pullEngineData()
62+
{
63+
auto engineData = EngineData{};
64+
if (mEngineDataGuard.try_lock())
65+
{
66+
engineData.requestedTempo = mSharedEngineData.requestedTempo;
67+
mSharedEngineData.requestedTempo = 0;
68+
engineData.resetBeatTime = mSharedEngineData.resetBeatTime;
69+
engineData.quantum = mSharedEngineData.quantum;
70+
mSharedEngineData.resetBeatTime = false;
71+
72+
mEngineDataGuard.unlock();
73+
}
74+
75+
return engineData;
76+
}
77+
78+
void AudioEngine::timelineCallback(const std::chrono::microseconds hostTime, LinkTimeInfo* const info)
79+
{
80+
const auto engineData = pullEngineData();
81+
82+
auto timeline = mLink.captureAudioTimeline();
83+
84+
if (engineData.resetBeatTime)
85+
{
86+
// Reset the timeline so that beat 0 lands at the beginning of
87+
// this buffer and clear the flag.
88+
timeline.requestBeatAtTime(0, hostTime, engineData.quantum);
89+
}
90+
91+
if (engineData.requestedTempo > 0)
92+
{
93+
// Set the newly requested tempo from the beginning of this buffer
94+
timeline.setTempo(engineData.requestedTempo, hostTime);
95+
}
96+
97+
// Timeline modifications are complete, commit the results
98+
mLink.commitAudioTimeline(timeline);
99+
100+
// Save timeline info
101+
info->bpm = timeline.tempo();
102+
info->beats = timeline.beatAtTime(hostTime, engineData.quantum);
103+
info->phase = timeline.phaseAtTime(hostTime, engineData.quantum);
104+
}
105+
106+
} // namespace linkaudio
107+
} // namespace ableton

Diff for: link/AudioEngine.hpp

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/* Copyright 2016, Ableton AG, Berlin. All rights reserved.
2+
*
3+
* This program is free software: you can redistribute it and/or modify
4+
* it under the terms of the GNU General Public License as published by
5+
* the Free Software Foundation, either version 2 of the License, or
6+
* (at your option) any later version.
7+
*
8+
* This program is distributed in the hope that it will be useful,
9+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
10+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11+
* GNU General Public License for more details.
12+
*
13+
* You should have received a copy of the GNU General Public License
14+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
15+
*
16+
* If you would like to incorporate Link into a proprietary software application,
17+
* please contact <link-devs@ableton.com>.
18+
*/
19+
20+
#pragma once
21+
22+
// Make sure to define this before <cmath> is included for Windows
23+
#define _USE_MATH_DEFINES
24+
#include "ableton/Link.hpp"
25+
#include <mutex>
26+
27+
struct LinkTimeInfo {
28+
double bpm, beats, phase;
29+
};
30+
31+
namespace ableton
32+
{
33+
namespace linkaudio
34+
{
35+
36+
class AudioEngine
37+
{
38+
public:
39+
AudioEngine(Link& link);
40+
double beatTime() const;
41+
void setTempo(double tempo);
42+
double quantum() const;
43+
void setQuantum(double quantum);
44+
45+
void timelineCallback(const std::chrono::microseconds hostTime, LinkTimeInfo* const info);
46+
47+
private:
48+
struct EngineData
49+
{
50+
double requestedTempo;
51+
bool resetBeatTime;
52+
double quantum;
53+
};
54+
55+
EngineData pullEngineData();
56+
57+
Link& mLink;
58+
EngineData mSharedEngineData;
59+
std::mutex mEngineDataGuard;
60+
61+
friend class AudioPlatform;
62+
};
63+
64+
65+
} // namespace linkaudio
66+
} // namespace ableton

0 commit comments

Comments
 (0)