-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
217 lines (190 loc) · 5.27 KB
/
Copy pathserver.js
File metadata and controls
217 lines (190 loc) · 5.27 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
const environment = process.env.NODE_ENV ?? 'development'
const autobahn = require('autobahn')
const Fastify = require('fastify')
const { ak } = require('./waapi.js')
const { wwiseRequest } = require('./wwise.api.js')
const envToLogger = {
development: {
transport: {
target: 'pino-pretty',
options: {
translateTime: 'HH:MM:ss Z',
ignore: 'pid,hostname',
},
},
},
production: true,
test: false,
}
const fastify = require('fastify')({
logger: envToLogger[environment] ?? true, // defaults to true if no entry matches in the map
})
const PORT = 3000
/** @typedef {import('autobahn').Connection} Connection */
/** @typedef {import('autobahn').Session} Session */
/** @type {Connection} */
let connection = null
// Middleware to register session in the Fastify instance
fastify.decorate('wwiseSession', null)
// CORS configuration
fastify.register(require('@fastify/cors'), {})
// Establish a connection to Wwise WAAPI
const connectToWwise = (url) => {
connection = new autobahn.Connection({
url: `${url}/waapi`,
realm: 'realm1',
protocols: ['wamp.2.json'],
})
connection.onopen = (session) => {
fastify.log.debug('Connected to Wwise WAAPI')
fastify.wwiseSession = session
session.call(ak.wwise.core.getInfo, [], {}).then(
function (res) {
fastify.log.debug(
`Hello ${res.kwargs.displayName} ${res.kwargs.version.displayName}!`
)
},
function (error) {
fastify.log.error(`Error getInfo: `, error)
}
)
}
connection.onclose = (reason) => {
fastify.log.error('Connection to Wwise closed:', reason)
fastify.wwiseSession = null
}
connection.open()
}
// API Endpoint: Fetch Wwise objects
fastify.get('/event', async (request, reply) => {
/** @type {Session} */
const session = fastify.wwiseSession
if (!session) {
reply.status(500).send({ error: 'Not connected to Wwise' })
return
}
try {
const result = await wwiseRequest(
ak.wwise.core.object.get,
{
// waql: "$ from type sound",
// from: { ofType: ["Sound"] }, // Fetch Sound object
from: { ofType: ['Event'] },
},
session
)
reply.send(result.return)
} catch (error) {
reply.status(500).send({ error: error.message })
}
})
fastify.get('/gameobjs', async (request, reply) => {
const session = fastify.wwiseSession
if (!session) {
reply.status(500).send({ error: 'Not connected to Wwise' })
return
}
try {
// https://www.audiokinetic.com/en/library/edge/?source=SDK&id=ak_wwise_core_profiler_getgameobjects_example_query_game_objects_registered_at_or_before_1_minute.html
const result = await wwiseRequest(
ak.wwise.core.profiler.getGameObjects,
{
time: 'capture',
},
session
)
reply.send(result.return)
} catch (error) {
reply.status(500).send({ error: error.message })
}
})
fastify.get('/play', async (request, reply) => {
const session = fastify.wwiseSession
if (!session) {
reply.status(500).send({ error: 'Not connected to Wwise' })
return
}
try {
// https://www.audiokinetic.com/en/library/edge/?source=SDK&id=ak_soundengine_registergameobj.html
await wwiseRequest(
ak.soundengine.registerGameObj,
{
gameObject: 1122334,
name: 'MyGameObjectName',
},
session
)
await wwiseRequest(
ak.soundengine.setListeners,
{
emitter: 1122334,
listeners: [18446744073709552000n],
},
session
)
// https://www.audiokinetic.com/en/library/edge/?source=SDK&id=ak_soundengine_postevent_example_posting_an_event_by_name.html
await wwiseRequest(
ak.soundengine.postEvent,
{
event: 'Istanbul_Amb_Test',
gameObject: 1122334,
},
session
)
reply.send(result.return)
} catch (error) {
reply.status(500).send({ error: error.message })
}
})
fastify.get('/playtrasnp', async (request, reply) => {
const session = fastify.wwiseSession
if (!session) {
reply.status(500).send({ error: 'Not connected to Wwise' })
return
}
try {
//https://www.audiokinetic.com/en/library/edge/?source=SDK&id=ak_wwise_ui_commands_execute.html
// https://www.audiokinetic.com/en/library/edge/?source=SDK&id=globalcommandsids.html
const result = await wwiseRequest(
ak.wwise.ui.commands.execute,
{
command: 'TransportPlayStop',
},
session
)
reply.send(result.return)
} catch (error) {
reply.status(500).send({ error: error.message })
}
})
fastify.get('/stop', async (request, reply) => {
const session = fastify.wwiseSession
if (!session) {
reply.status(500).send({ error: 'Not connected to Wwise' })
return
}
try {
const result = await wwiseRequest(
ak.soundengine.stopAll,
{
gameObject: 1122334,
},
session
)
reply.send(result.return)
} catch (error) {
reply.status(500).send({ error: error.message })
}
})
// Start the Fastify server and connect to Wwise
const startServer = async () => {
try {
await fastify.listen({ port: PORT })
fastify.log.info(`Server running at http://localhost:${PORT}`)
connectToWwise('ws://localhost:8080')
} catch (err) {
fastify.log.error(err)
process.exit(1)
}
}
startServer()