Skip to content

Commit

Permalink
Merge branch 'use-thread-interruption' into ecs/scripting
Browse files Browse the repository at this point in the history
# Conflicts:
#	redhorizon-engine/source/nz/net/ultraq/redhorizon/engine/audio/AudioSystem.groovy
#	redhorizon-engine/source/nz/net/ultraq/redhorizon/engine/graphics/GraphicsSystem.groovy
  • Loading branch information
ultraq committed Feb 2, 2024
2 parents d463961 + 3bd019d commit fb904b5
Show file tree
Hide file tree
Showing 22 changed files with 228 additions and 456 deletions.
17 changes: 0 additions & 17 deletions redhorizon-async/build.gradle

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

3 changes: 0 additions & 3 deletions redhorizon-classic/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ description = 'Code that bridges the classic C&C file formats for use with the R
year = '2007'

dependencies {
implementation project(':redhorizon-async')
implementation project(':redhorizon-engine')
implementation project(':redhorizon-events')
implementation project(':redhorizon-filetypes')
Expand All @@ -35,7 +34,6 @@ dependencies {
shadowJar {
archiveClassifier.set('')
dependencies {
include(project(':redhorizon-async'))
include(project(':redhorizon-engine'))
include(project(':redhorizon-events'))
include(project(':redhorizon-filetypes'))
Expand All @@ -49,7 +47,6 @@ groovydoc {
here, and serve as working examples for other programmers.
''')
// Can't link to these until they get published on maven central themselves
// link("https://javadoc.io/doc/nz.net.ultraq.redhorizon/redhorizon-async/${version}/", 'nz.net.ultraq.redhorizon.async.')
// link("https://javadoc.io/doc/nz.net.ultraq.redhorizon/redhorizon-events/${version}/", 'nz.net.ultraq.redhorizon.events.')
// link("https://javadoc.io/doc/nz.net.ultraq.redhorizon/redhorizon-filetypes/${version}/", 'nz.net.ultraq.redhorizon.filetypes.')
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@

package nz.net.ultraq.redhorizon.classic.filetypes

import nz.net.ultraq.redhorizon.async.ControlledLoop
import nz.net.ultraq.redhorizon.classic.codecs.IMAADPCM16bit
import nz.net.ultraq.redhorizon.classic.codecs.WSADPCM8bit
import nz.net.ultraq.redhorizon.events.EventTarget
import nz.net.ultraq.redhorizon.filetypes.FileExtensions
import nz.net.ultraq.redhorizon.filetypes.SoundFile
import nz.net.ultraq.redhorizon.filetypes.Streaming
import nz.net.ultraq.redhorizon.filetypes.StreamingDecoder
import nz.net.ultraq.redhorizon.filetypes.StreamingSampleEvent
import nz.net.ultraq.redhorizon.filetypes.Worker
import nz.net.ultraq.redhorizon.filetypes.io.NativeDataInputStream

import org.slf4j.Logger
Expand Down Expand Up @@ -100,7 +100,7 @@ class AudFile implements SoundFile, Streaming {

return ByteBuffer.fromBuffers(
Executors.newSingleThreadExecutor().executeAndShutdown { executorService ->
def worker = streamingDataWorker
def worker = streamingDecoder
def samples = []
worker.on(StreamingSampleEvent) { event ->
samples << event.sample
Expand All @@ -114,15 +114,13 @@ class AudFile implements SoundFile, Streaming {
}

/**
* Returns a worker that can be run to start streaming sound data. The worker
* will emit {@link StreamingSampleEvent}s for new samples available.
*
* @return Worker for streaming sound data.
* Returns a decoder that can be run to start streaming sound data. The
* decoder will emit {@link StreamingSampleEvent}s for new samples available.
*/
@Override
Worker getStreamingDataWorker() {
StreamingDecoder getStreamingDecoder() {

return new AudFileWorker()
return new StreamingDecoder(new AudFileDecoder())
}

@Override
Expand All @@ -145,12 +143,9 @@ class AudFile implements SoundFile, Streaming {
}

/**
* A worker for decoding AUD file sound data.
* Decode AUD file sound data and emit as {@link StreamingSampleEvent}s.
*/
class AudFileWorker extends Worker {

@Delegate
private ControlledLoop workLoop
class AudFileDecoder implements Runnable, EventTarget {

@Override
void run() {
Expand All @@ -162,7 +157,7 @@ class AudFile implements SoundFile, Streaming {

// Decompress the aud file data by chunks
def headerSize = input.bytesRead
workLoop = new ControlledLoop({ input.bytesRead < headerSize + compressedSize }, { ->
while (input.bytesRead < headerSize + compressedSize && !Thread.interrupted()) {
def sample = average('Decoding sample', 1f, logger) { ->

// Chunk header
Expand All @@ -177,8 +172,8 @@ class AudFile implements SoundFile, Streaming {
)
}
trigger(new StreamingSampleEvent(sample))
})
workLoop.run()
Thread.sleep(20)
}

logger.debug('Decoding complete')
}
Expand Down
Loading

0 comments on commit fb904b5

Please sign in to comment.