Skip to content

Commit 2ab413f

Browse files
authored
Merge pull request #1 from defold/opus
Added opus support
2 parents 7ba4395 + a6300ec commit 2ab413f

File tree

10 files changed

+137
-13
lines changed

10 files changed

+137
-13
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
liveupdate.mounts
22
build
33
.internal
4+
/.editor_settings

README.md

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,19 @@ This example shows how to stream sounds from either the archive (the base game b
55
For supporting loading from the web, start a web server where the files uner the [web](./web) folder is available.
66

77
E.g. starting a local server:
8+
Note: For full features, the serer should support:
9+
* Ranged requests (i.e. returning 206)
10+
* ETag (i.e. returning 304)
811

9-
$ (cd sounds && python -m RangeHTTPServer)
12+
macOS - install:
1013

11-
You can of course also change the url in main.script, to somethin of your choosing (defaults to http://localhost:8000)
14+
$ brew install http-server
15+
16+
macOS - run:
17+
18+
$ (cd sounds && http-server -p 8000)
19+
20+
You can of course also change the url in main.script, to something of your choosing (defaults to http://localhost:8000)
1221

1322

1423
![](./hero.png)

game.project

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ scale_mode = stretch
3636

3737
[sound]
3838
stream_enabled = 1
39+
stream_cache_size = 131072
3940
stream_preload_size = 16384
4041

4142
[liveupdate]

main/main.collection

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,12 @@ embedded_instances {
4646
" data: \"collection: \\\"/main/soundWeb.collection\\\"\\n"
4747
"\"\n"
4848
"}\n"
49+
"embedded_components {\n"
50+
" id: \"proxyOpus\"\n"
51+
" type: \"collectionproxy\"\n"
52+
" data: \"collection: \\\"/main/soundOpus.collection\\\"\\n"
53+
"\"\n"
54+
"}\n"
4955
""
5056
}
5157
embedded_instances {

main/main.gui

Lines changed: 62 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ nodes {
6969
nodes {
7070
position {
7171
x: 396.0
72-
y: 600.0
72+
y: 621.0
7373
}
7474
scale {
7575
x: 0.7
@@ -94,6 +94,7 @@ nodes {
9494
nodes {
9595
position {
9696
x: 48.0
97+
y: 20.0
9798
}
9899
type: TYPE_TEMPLATE
99100
id: "origin_web"
@@ -124,7 +125,7 @@ nodes {
124125
nodes {
125126
position {
126127
x: 48.0
127-
y: -96.0
128+
y: -66.0
128129
}
129130
type: TYPE_TEMPLATE
130131
id: "origin_archive"
@@ -155,7 +156,7 @@ nodes {
155156
nodes {
156157
position {
157158
x: 777.0
158-
y: 600.0
159+
y: 621.0
159160
}
160161
scale {
161162
x: 0.7
@@ -180,6 +181,7 @@ nodes {
180181
nodes {
181182
position {
182183
x: 48.0
184+
y: 20.0
183185
}
184186
type: TYPE_TEMPLATE
185187
id: "type_ogg"
@@ -210,7 +212,7 @@ nodes {
210212
nodes {
211213
position {
212214
x: 48.0
213-
y: -96.0
215+
y: -66.0
214216
}
215217
type: TYPE_TEMPLATE
216218
id: "type_wav"
@@ -238,6 +240,37 @@ nodes {
238240
overridden_fields: 8
239241
template_node_child: true
240242
}
243+
nodes {
244+
position {
245+
x: 48.0
246+
y: -152.0
247+
}
248+
type: TYPE_TEMPLATE
249+
id: "type_opus"
250+
parent: "text_type"
251+
inherit_alpha: true
252+
template: "/dirtylarry/radio_label.gui"
253+
}
254+
nodes {
255+
type: TYPE_BOX
256+
id: "type_opus/larryradio"
257+
parent: "type_opus"
258+
template_node_child: true
259+
}
260+
nodes {
261+
color {
262+
x: 0.2
263+
y: 0.102
264+
z: 0.502
265+
}
266+
type: TYPE_TEXT
267+
text: "Opus"
268+
id: "type_opus/larrylabel"
269+
parent: "type_opus"
270+
overridden_fields: 5
271+
overridden_fields: 8
272+
template_node_child: true
273+
}
241274
nodes {
242275
position {
243276
x: 463.0
@@ -286,5 +319,30 @@ nodes {
286319
overridden_fields: 8
287320
template_node_child: true
288321
}
322+
nodes {
323+
position {
324+
x: 1262.0
325+
y: 63.0
326+
}
327+
scale {
328+
x: 0.7
329+
y: 0.7
330+
}
331+
size {
332+
x: 200.0
333+
y: 100.0
334+
}
335+
color {
336+
x: 0.2
337+
y: 0.102
338+
z: 0.502
339+
}
340+
type: TYPE_TEXT
341+
text: "Origin:"
342+
font: "larryfont"
343+
id: "info"
344+
pivot: PIVOT_E
345+
inherit_alpha: true
346+
}
289347
material: "/builtins/materials/gui.material"
290348
adjust_reference: ADJUST_REFERENCE_PARENT

main/main.gui_script

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,18 @@ local dirtylarry = require "dirtylarry/dirtylarry"
33
function init(self)
44
self.selected_origin = 'Archive'
55
self.selected_type = 'Ogg'
6+
7+
local n = gui.get_node("info")
8+
local text = ''
9+
10+
local info = sys.get_sys_info()
11+
if info.system_name == 'HTML5' then
12+
pprint(sys.get_sys_info())
13+
text = html5.run('var simd = WebAssembly.validate(new Uint8Array([0, 97, 115, 109, 1, 0, 0, 0, 1, 4, 1, 96, 0, 0, 3, 2, 1, 0, 10, 9, 1, 7, 0, 65, 0, 253, 4, 26, 11])); await simd();')
14+
print("MAWE: result:", text)
15+
end
16+
17+
gui.set_text(n, string.format("Info: '%s'", text))
618
end
719

820
function final(self)
@@ -32,6 +44,7 @@ function on_input(self, action_id, action)
3244

3345
self.selected_type = dirtylarry:radio("type_ogg", action_id, action, "Ogg", self.selected_type)
3446
self.selected_type = dirtylarry:radio("type_wav", action_id, action, "Wav", self.selected_type)
47+
self.selected_type = dirtylarry:radio("type_opus", action_id, action, "Opus", self.selected_type)
3548

3649
dirtylarry:button("bn_play", action_id, action, function ()
3750
msg.post("main:/controller#script", "play", { origin = self.selected_origin, type = self.selected_type })

main/main.script

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22
-- TODO: Find an online server with good content!
33

44
local URL = "http://localhost:8000"
5+
--local URL = "http://127.0.0.1:8000"
56
local web_sounds = {}
67
web_sounds["Ogg"] = "mary-had-a-little-lamb-loop.ogg"
78
web_sounds["Wav"] = "mary-had-a-little-lamb-loop.wav"
9+
web_sounds["Opus"] = "mary-had-a-little-lamb-loop.opus"
810

911
local function load(self, source, type)
1012
self.source = source
@@ -28,6 +30,16 @@ function init(self)
2830
local source = sys.get_config_string('test.source', 'Archive')
2931
local sound_type = sys.get_config_string('test.sound_type', 'Wav')
3032

33+
if not (source == 'Archive' or source == 'Web') then
34+
print("Invalid source type:", source)
35+
source = 'Archive'
36+
end
37+
38+
if not (sound_type == 'Ogg' or sound_type == 'Wav' or sound_type == 'Opus') then
39+
print("Invalid sound type:", sound_type)
40+
sound_type = 'Ogg'
41+
end
42+
3143
self.loaded = false
3244
load(self, source, sound_type)
3345

main/sound.script

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,27 +14,31 @@ local function parse_range(s)
1414
end
1515

1616
local function http_result(self, _id, response, extra)
17-
if response.status == 200 or response.status == 206 then
17+
if response.status == 200 or response.status == 206 or response.status == 304 then
1818
--pprint(response.headers)
19-
--pprint(response)
19+
pprint(response)
2020
print("Playing web sound:", response.url)
2121
local relative_path = self.filename
22-
local range = response.headers['content-range'] -- content-range = "bytes 0-16383/103277"
23-
local rstart, rend, filesize = parse_range(range)
22+
-- local range = response.headers['content-range'] -- content-range = "bytes 0-16383/103277"
23+
-- local rstart, rend, filesize = parse_range(range)
2424
-- Create the Defold resource, "partial" will enable the streaming mode
2525
print("Creating resource", relative_path)
26-
local hash = resource.create_sound_data(relative_path, { data = response.response, filesize = filesize, partial = true })
26+
local hash = resource.create_sound_data(relative_path, { data = response.response, filesize = response.document_size, partial = true })
2727
-- send "play_sound" to the component
2828
play_sound(self, hash)
2929
end
3030
end
3131

3232
local function load_web_sound(base_url, relative_path)
3333
local url = base_url .. "/" .. relative_path
34+
35+
local range_start = 0
36+
local range_end = 16384-1
3437
local headers = {}
35-
headers['Range'] = string.format("bytes=%d-%d", 0, 16384-1)
38+
headers['Range'] = string.format("bytes=%d-%d", range_start, range_end)
3639

37-
http.request(url, "GET", http_result, headers, nil, { ignore_cache = true })
40+
local request = http.request(url, "GET", http_result, headers, nil)
41+
print("REQUEST ID", request, "url:", url)
3842
end
3943

4044
-- ***************************************************************************
@@ -50,6 +54,8 @@ function on_message(self, message_id, message)
5054
if message_id == hash("start") then
5155
self.source = message.source
5256
self.sound_type = message.sound_type
57+
print("MAWE: self.source", self.source)
58+
print("MAWE: self.sound_type", self.sound_type)
5359
if message.source == "Web" then
5460
self.url = message.url
5561
self.filename = message.filename

main/soundOpus.collection

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
name: "sounds"
2+
scale_along_z: 0
3+
embedded_instances {
4+
id: "sounds"
5+
data: "components {\n"
6+
" id: \"test\"\n"
7+
" component: \"/main/sound.script\"\n"
8+
"}\n"
9+
"embedded_components {\n"
10+
" id: \"music\"\n"
11+
" type: \"sound\"\n"
12+
" data: \"sound: \\\"/sounds/mary-had-a-little-lamb-loop.opus\\\"\\n"
13+
"looping: 1\\n"
14+
"group: \\\"music\\\"\\n"
15+
"\"\n"
16+
"}\n"
17+
""
18+
}
133 KB
Binary file not shown.

0 commit comments

Comments
 (0)