audio mixer, speaker now plays sound when it's placed
This commit is contained in:
parent
f8612db6ef
commit
8c5ac2bf23
13 changed files with 71 additions and 20 deletions
7
d3r/Cargo.lock
generated
7
d3r/Cargo.lock
generated
|
|
@ -220,6 +220,7 @@ dependencies = [
|
|||
"bitvec",
|
||||
"cpal",
|
||||
"env_logger",
|
||||
"hound",
|
||||
"jni 0.20.0",
|
||||
"log",
|
||||
]
|
||||
|
|
@ -264,6 +265,12 @@ dependencies = [
|
|||
"libc",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "hound"
|
||||
version = "3.5.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "4d13cdbd5dbb29f9c88095bbdc2590c9cba0d0a1269b983fef6b2cdd7e9f4db1"
|
||||
|
||||
[[package]]
|
||||
name = "humantime"
|
||||
version = "2.1.0"
|
||||
|
|
|
|||
|
|
@ -12,5 +12,6 @@ crate-type = ["cdylib"]
|
|||
bitvec = "1.0.1"
|
||||
cpal = "0.14.1"
|
||||
env_logger = "0.9.3"
|
||||
hound = "3.5.0"
|
||||
jni = "0.20.0"
|
||||
log = "0.4.17"
|
||||
|
|
|
|||
|
|
@ -1,8 +1,10 @@
|
|||
use std::cell::RefCell;
|
||||
use std::fmt::Display;
|
||||
use std::fs::File;
|
||||
|
||||
use cpal::traits::{DeviceTrait, HostTrait, StreamTrait};
|
||||
use cpal::{BufferSize, Device, Host, SampleRate, Stream, StreamConfig};
|
||||
use hound::{SampleFormat, WavSpec};
|
||||
use jni::objects::{JClass, JMethodID, JObject};
|
||||
use jni::signature::ReturnType;
|
||||
use jni::sys::jvalue;
|
||||
|
|
@ -157,6 +159,7 @@ pub extern "system" fn Java_net_liquidev_d3r_D3r_openOutputStream(
|
|||
let generator_ref = env.new_global_ref(generator).map_err(|e| e.to_string())?;
|
||||
let jvm = env.get_java_vm().map_err(|e| e.to_string())?;
|
||||
let mut initialized = false;
|
||||
|
||||
let stream = device
|
||||
.build_output_stream(
|
||||
&config.clone(),
|
||||
|
|
|
|||
|
|
@ -1,9 +1,12 @@
|
|||
package net.liquidev.dawd3.sound
|
||||
|
||||
import net.liquidev.d3r.D3r
|
||||
import org.slf4j.LoggerFactory
|
||||
|
||||
/** Common sound utilities. */
|
||||
object Sound {
|
||||
val logger = LoggerFactory.getLogger("dawd³/sound")
|
||||
|
||||
const val sampleRate = 48000
|
||||
private const val bufferSize = 256
|
||||
|
||||
4
src/main/kotlin/net/liquidev/dawd3/audio/MixGenerator.kt
Normal file
4
src/main/kotlin/net/liquidev/dawd3/audio/MixGenerator.kt
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
package net.liquidev.dawd3.sound
|
||||
|
||||
class MixGenerator {
|
||||
}
|
||||
2
src/main/kotlin/net/liquidev/dawd3/audio/unit/volume.kt
Normal file
2
src/main/kotlin/net/liquidev/dawd3/audio/unit/volume.kt
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
package net.liquidev.dawd3.audio.unit
|
||||
|
||||
|
|
@ -1,20 +0,0 @@
|
|||
package net.liquidev.dawd3.block
|
||||
|
||||
import net.minecraft.block.BlockState
|
||||
import net.minecraft.block.entity.BlockEntity
|
||||
import net.minecraft.util.math.BlockPos
|
||||
|
||||
class SpeakerBlockEntity(pos: BlockPos, state: BlockState) : BlockEntity(Blocks.speakerEntity, pos, state) {
|
||||
init {
|
||||
println("Speaker block entity created")
|
||||
|
||||
}
|
||||
|
||||
fun deinit() {
|
||||
println("Speaker block entity deinitialized")
|
||||
}
|
||||
|
||||
private fun finalize() {
|
||||
deinit()
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
package net.liquidev.dawd3.block.entity
|
||||
|
||||
class D3BlockEntity {
|
||||
}
|
||||
|
|
@ -0,0 +1,41 @@
|
|||
package net.liquidev.dawd3.block
|
||||
|
||||
import net.liquidev.dawd3.Mod
|
||||
import net.liquidev.dawd3.audio.Audio
|
||||
import net.liquidev.dawd3.audio.MixGenerator
|
||||
import net.liquidev.dawd3.audio.SineOscGenerator
|
||||
import net.liquidev.dawd3.audio.unit.Amplitude
|
||||
import net.minecraft.block.BlockState
|
||||
import net.minecraft.block.entity.BlockEntity
|
||||
import net.minecraft.util.math.BlockPos
|
||||
|
||||
class SpeakerBlockEntity(pos: BlockPos, state: BlockState) : BlockEntity(Blocks.speakerEntity, pos, state) {
|
||||
private companion object {
|
||||
val logger = Mod.logger<SpeakerBlockEntity>()
|
||||
}
|
||||
|
||||
private val channel: MixGenerator.Channel<SineOscGenerator>?
|
||||
|
||||
init {
|
||||
logger.info("created")
|
||||
val generator = SineOscGenerator(
|
||||
frequency = 440.0f,
|
||||
amplitude = Amplitude(1.0f),
|
||||
)
|
||||
channel = Audio.mixer.createChannel(generator)
|
||||
}
|
||||
|
||||
override fun onSyncedBlockEvent(type: Int, data: Int): Boolean {
|
||||
return super.onSyncedBlockEvent(type, data)
|
||||
}
|
||||
|
||||
fun deinit() {
|
||||
logger.info("stopping channel")
|
||||
channel.stop()
|
||||
}
|
||||
|
||||
protected fun finalize() {
|
||||
logger.warn("speaker block entity had to be deinitialized from finalizer")
|
||||
deinit()
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
package net.liquidev.dawd3.block.entity
|
||||
|
||||
4
src/main/kotlin/net/liquidev/dawd3/common/TaskQueue.kt
Normal file
4
src/main/kotlin/net/liquidev/dawd3/common/TaskQueue.kt
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
package net.liquidev.dawd3.common
|
||||
|
||||
class TaskQueue {
|
||||
}
|
||||
Reference in a new issue