-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathInlet.swift
More file actions
102 lines (76 loc) · 2.49 KB
/
Inlet.swift
File metadata and controls
102 lines (76 loc) · 2.49 KB
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
//
// Inlet.swift
// LabStreamingLayer
//
// Created by Maximilian Kraus on 17.12.19.
// Copyright © 2019 Maximilian Kraus. All rights reserved.
//
import Foundation
public class Inlet {
let base: lsl_inlet
let streamInfo: StreamInfo
private var fullInfo: StreamInfo?
private var _streamInfo: StreamInfo { fullInfo ?? streamInfo }
deinit {
lsl_destroy_inlet(base)
}
public init(streamInfo: StreamInfo) {
self.streamInfo = streamInfo
self.base = lsl_create_inlet(streamInfo.base, 1, LSL_NO_PREFERENCE, 1)
}
}
//MARK: - Properties
public extension Inlet {
var numberOfSamplesAvailable: UInt32 {
lsl_samples_available(base)
}
}
//MARK: - Operations
public extension Inlet {
func fullInfo(timeout: TimeInterval = LSL_FOREVER) throws -> StreamInfo {
var error: Int32 = 0
let info = lsl_get_fullinfo(base, timeout, &error)
if let info = info {
return StreamInfo(base: info)
} else {
throw Error(rawValue: error)!
}
}
func openStream(timeout: TimeInterval = LSL_FOREVER) throws {
var error: Int32 = 0
lsl_open_stream(base, timeout, &error)
if error != 0 {
throw Error(rawValue: error)!
}
}
func pullSample(timeout: TimeInterval = LSL_FOREVER) throws -> [Float32] {
guard streamInfo.channelFormat == .float32 else { throw Error.argument }
var buffer = Array<Float32>(repeating: 0, count: Int(streamInfo.channelCount))
var error: Int32 = 0
lsl_pull_sample_f(base, &buffer, streamInfo.channelCount, timeout, &error)
if error != 0 {
throw Error(rawValue: error)!
}
return buffer
}
func pullSample(timeout: TimeInterval = LSL_FOREVER) throws -> [Double] {
guard streamInfo.channelFormat == .double64 else { throw Error.argument }
var buffer = Array<Double>(repeating: 0, count: Int(streamInfo.channelCount))
var error: Int32 = 0
lsl_pull_sample_d(base, &buffer, streamInfo.channelCount, timeout, &error)
if error != 0 {
throw Error(rawValue: error)!
}
return buffer
}
func pullSample(timeout: TimeInterval = LSL_FOREVER) throws -> [Int32] {
guard streamInfo.channelFormat == .int32 else { throw Error.argument }
var buffer = Array<Int32>(repeating: 0, count: Int(streamInfo.channelCount))
var error: Int32 = 0
lsl_pull_sample_i(base, &buffer, streamInfo.channelCount, timeout, &error)
if error != 0 {
throw Error(rawValue: error)!
}
return buffer
}
}