added speaker peripheral
This commit is contained in:
@@ -81,6 +81,8 @@ class Peripheral {
|
||||
return getModem(addr);
|
||||
case Printer.TYPE_NAME:
|
||||
return getPrinter(addr);
|
||||
case Speaker.TYPE_NAME:
|
||||
return getSpeaker(addr);
|
||||
case "redstone":
|
||||
return getRedstone(addr);
|
||||
}
|
||||
@@ -158,4 +160,15 @@ class Peripheral {
|
||||
public static function getAllComputers():Array<Computer> {
|
||||
return [for (addr in findAddrByType(Computer.TYPE_NAME)) new Computer(addr)];
|
||||
}
|
||||
|
||||
public static function getSpeaker(addr:String):Null<Speaker> {
|
||||
var addr = safeGetAddr(addr, Speaker.TYPE_NAME);
|
||||
if (addr == null)
|
||||
return null;
|
||||
return new Speaker(addr);
|
||||
}
|
||||
|
||||
public static function getAllSpeakers():Array<Speaker> {
|
||||
return [for (addr in findAddrByType(Speaker.TYPE_NAME)) new Speaker(addr)];
|
||||
}
|
||||
}
|
||||
|
||||
61
src/kernel/peripherals/Speaker.hx
Normal file
61
src/kernel/peripherals/Speaker.hx
Normal file
@@ -0,0 +1,61 @@
|
||||
package kernel.peripherals;
|
||||
|
||||
import cc.Peripheral;
|
||||
|
||||
using tink.CoreApi;
|
||||
|
||||
class Speaker implements IPeripheral {
|
||||
public static inline final TYPE_NAME:String = "speaker";
|
||||
|
||||
private final addr:String;
|
||||
|
||||
public function new(addr:String) {
|
||||
this.addr = addr;
|
||||
}
|
||||
|
||||
public function getType():String {
|
||||
return Speaker.TYPE_NAME;
|
||||
}
|
||||
|
||||
public function getAddr():String {
|
||||
return this.addr;
|
||||
}
|
||||
|
||||
/**
|
||||
Plays a note block note through the speaker.
|
||||
The pitch argument uses semitones as the unit. This directly maps to the number of clicks on a note block.
|
||||
For reference, 0, 12, and 24 map to F#, and 6 and 18 map to C.
|
||||
A maximum of 8 notes can be played in a single tick. If this limit is hit, this function will return an error.
|
||||
**/
|
||||
public function playNote(instrument:String, ?volume:Float = 1.0, ?pitch:Int = 12):Outcome<Noise, String> {
|
||||
if (Peripheral.call(addr, "playNote", instrument, volume, pitch)) {
|
||||
return Success(null);
|
||||
} else {
|
||||
return Failure("maximum reached");
|
||||
}
|
||||
}
|
||||
|
||||
public function playSound(sound:String, ?volume:Float = 1.0, ?pitch:Float = 1.0):Outcome<Noise, String> {
|
||||
try {
|
||||
if (Peripheral.call(addr, "playSound", sound, volume, pitch)) {
|
||||
return Success(null);
|
||||
} else {
|
||||
return Failure("Sound still playing");
|
||||
}
|
||||
} catch (e) {
|
||||
return Failure("Sound does not exist");
|
||||
}
|
||||
}
|
||||
|
||||
public function playAudio(buffer:Array<Int>, ?volume:Float = 1.0):Outcome<Noise, String> {
|
||||
try {
|
||||
if (Peripheral.call(addr, "playAudio", buffer, volume)) {
|
||||
return Success(null);
|
||||
} else {
|
||||
return Failure("Buffer full");
|
||||
}
|
||||
} catch (e) {
|
||||
return Failure("Buffer malformed");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user