Skip to content

Commit

Permalink
ran code through crystal tool format
Browse files Browse the repository at this point in the history
  • Loading branch information
jimm committed Apr 19, 2019
1 parent 9f98caf commit e74c194
Show file tree
Hide file tree
Showing 10 changed files with 95 additions and 82 deletions.
2 changes: 1 addition & 1 deletion examples/note_send.cr
Expand Up @@ -15,5 +15,5 @@ output = OutputStream.open(output_device_num)
output.write_short(PortMIDI.message(0x80, note, 0))
end

output.close()
output.close
PortMIDI.terminate
12 changes: 6 additions & 6 deletions examples/sysex_send_and_receive.cr
@@ -1,10 +1,10 @@
require "../src/port_midi"

# A few helpful constant definitions.
SYSEX = 0xF0_u8
EOX = 0xF7_u8
KORG_MANUFACTURER_ID = 0x42_u8
KRONOS_DEVICE_ID = 0x68_u8
SYSEX = 0xF0_u8
EOX = 0xF7_u8
KORG_MANUFACTURER_ID = 0x42_u8
KRONOS_DEVICE_ID = 0x68_u8
FUNC_CODE_CURR_OBJ_DUMP_REQ = 0x74_u8
OBJ_TYPE_SET_LIST_SLOT_NAME = 0x11_u8

Expand Down Expand Up @@ -61,9 +61,9 @@ def midi_to_internal(m_bytes : Array(UInt8))

while m_len > 0
chunk_len = m_len.clamp(0, 8)
(chunk_len-1).times do |i|
(chunk_len - 1).times do |i|
high_bit_set = (m_bytes[m_offset] & (1 << i)) != 0
i_bytes << m_bytes[m_offset+i+1] + (high_bit_set ? 0x80_u8 : 0_u8)
i_bytes << m_bytes[m_offset + i + 1] + (high_bit_set ? 0x80_u8 : 0_u8)
end
m_offset += chunk_len
m_len -= chunk_len
Expand Down
8 changes: 4 additions & 4 deletions spec/port_midi_spec.cr
@@ -1,10 +1,10 @@
require "./spec_helper"

MESSAGE = 0x00ff8090_u32
STATUS = 0x90_u8
DATA1 = 0x80_u8
DATA2 = 0xff_u8
BYTES = StaticArray[STATUS, DATA1, DATA2, 0_u8]
STATUS = 0x90_u8
DATA1 = 0x80_u8
DATA2 = 0xff_u8
BYTES = StaticArray[STATUS, DATA1, DATA2, 0_u8]

describe PortMIDI do
it "creates a PortMidi message from bytes" do
Expand Down
12 changes: 9 additions & 3 deletions src/port_midi/device_info.cr
Expand Up @@ -19,13 +19,19 @@ class DeviceInfo
end

# Returns `true` if this is an input device.
def input?; @is_input; end
def input?
@is_input
end

# Returns `true` if this is an output device.
def output?; @is_output; end
def output?
@is_output
end

# Returns `true` if this device is opened. As noted in the class comment,
# this is a snapshot: the value will not change when the device is opened
# or closed.
def opened?; @is_opened; end
def opened?
@is_opened
end
end
2 changes: 1 addition & 1 deletion src/port_midi/input_stream.cr
Expand Up @@ -9,7 +9,7 @@ class InputStream < PMStream
time_proc : (Void* -> LibPortMIDI::PmTimestamp)? = nil,
time_info : (Void* -> LibPortMIDI::PmTimestamp)? = nil) : InputStream
err = LibPortMIDI.open_input(out input, input_device_num, input_driver_info,
buffer_size, time_proc, time_info)
buffer_size, time_proc, time_info)
if err != LibPortMIDI::PmError::NoError
PortMIDI.raise_error(err, "error opening input device #{input_device_num}")
end
Expand Down
62 changes: 31 additions & 31 deletions src/port_midi/lib_port_midi.cr
Expand Up @@ -6,7 +6,7 @@ lib LibPortMIDI
@[Flags]
enum Filter
Sysex
MTC # MIDI time code
MTC # MIDI time code
SongPosition
SongSelect
Unused1
Expand All @@ -15,12 +15,12 @@ lib LibPortMIDI
Unused3
Clock
Tick
Play = ((1 << 0x0A) | (1 << 0x0C) | (1 << 0x0B))
Play = ((1 << 0x0A) | (1 << 0x0C) | (1 << 0x0B))
UndefinedRealtime = (1 << 0x0D)
ActiveSensing
Reset
Note = ((1 << 0x19) | (1 << 0x18))
PolyAftertouch = (1 << 0x1A)
Note = ((1 << 0x19) | (1 << 0x18))
PolyAftertouch = (1 << 0x1A)
ControlChange
ProgramChange
ChannelAftertouch
Expand All @@ -44,36 +44,36 @@ lib LibPortMIDI

# The error values returned by PortMidi.
enum PmError : Int32
NoError = 0
NoData = 0 # A "no error" return that also indicates no data avail.
GotData = 1, # A "no error" return that also indicates data available
HostError = -10000
InvalidDeviceId, # out of range or
# output device when input is requested or
# input device when output is requested or
# device is already opened
InsufficientMemory,
NoError = 0
NoData = 0 # A "no error" return that also indicates no data avail.
GotData = 1, # A "no error" return that also indicates data available
HostError = -10000
InvalidDeviceId, # out of range or
# output device when input is requested or
# input device when output is requested or
# device is already opened
InsufficientMemory,
BufferTooSmall,
BufferOverflow,
BadPtr, # PortMidiStream parameter is NULL or
# stream is not opened or
# stream is output when input is required or
# stream is input when output is required
BadData, # illegal midi data, e.g. missing EOX
InternalError,
BufferMaxSize # buffer is already as large as it can be
BadPtr, # PortMidiStream parameter is NULL or
# stream is not opened or
# stream is output when input is required or
# stream is input when output is required
BadData, # illegal midi data, e.g. missing EOX
InternalError,
BufferMaxSize # buffer is already as large as it can be
end

type PmTimestamp = UInt32

# The device information returned by PortMidi.
struct DeviceInfo
struct_version : Int32 # internal
interf : UInt8 * # underlying MIDI API
name : UInt8 * # device name
input : Int32 # true iff input is available
output : Int32 # true iff output is available
opened : Int32 # used by generic MidiPort code
struct_version : Int32 # internal
interf : UInt8* # underlying MIDI API
name : UInt8* # device name
input : Int32 # true iff input is available
output : Int32 # true iff output is available
opened : Int32 # used by generic MidiPort code
end

# A single MIDI event sent/received by PortMidi.
Expand All @@ -84,19 +84,19 @@ lib LibPortMIDI

type Stream = Pointer(Void)

fun initialize = Pm_Initialize() : PmError
fun initialize = Pm_Initialize : PmError

fun terminate = Pm_Terminate() : PmError
fun terminate = Pm_Terminate : PmError

fun host_error? = Pm_HasHostError(stream : Stream) : Int32

fun get_error_text = Pm_GetErrorText(errnum : PmError) : UInt8*

fun count_devices = Pm_CountDevices() : Int32
fun count_devices = Pm_CountDevices : Int32

fun get_default_input_device_id = Pm_GetDefaultInputDeviceID() : Int32
fun get_default_input_device_id = Pm_GetDefaultInputDeviceID : Int32

fun get_default_output_device_id = Pm_GetDefaultOutputDeviceID() : Int32
fun get_default_output_device_id = Pm_GetDefaultOutputDeviceID : Int32

fun get_device_info = Pm_GetDeviceInfo(device_id : Int32) : DeviceInfo*

Expand Down
2 changes: 1 addition & 1 deletion src/port_midi/output_stream.cr
Expand Up @@ -10,7 +10,7 @@ class OutputStream < PMStream
time_info : (Void* -> LibPortMIDI::PmTimestamp)? = nil,
latency : Int32 = 0) : OutputStream
err = LibPortMIDI.open_output(out output, output_device_num, output_driver_info,
buffer_size, time_proc, time_info, latency)
buffer_size, time_proc, time_info, latency)
if err != LibPortMIDI::PmError::NoError
PortMIDI.raise_error(err, "error opening output device #{output_device_num}")
end
Expand Down
2 changes: 1 addition & 1 deletion src/port_midi/pm_stream.cr
Expand Up @@ -18,7 +18,7 @@ class PMStream
# Returns `true` if the stream has a pending host error.
#
# The PortMidi docs note that normally you won't have to call this method.
def host_error?() : Bool
def host_error? : Bool
LibPortMIDI.host_error?(@stream) != 0
end

Expand Down
72 changes: 40 additions & 32 deletions src/port_midi/port_midi.cr
Expand Up @@ -8,72 +8,80 @@ module PortMIDI
VERSION = "0.1.0"

class HostError < Exception; end

class InvalidDeviceId < Exception; end

class InsufficientMemory < Exception; end

class BufferTooSmall < Exception; end

class BufferOverflow < Exception; end

class BadPtr < Exception; end

class BadData < Exception; end

class InternalError < Exception; end

class BufferMaxSize < Exception; end

# Given *err*, creates and raises an instance of the corresponding
# `Exception` subclass with the given *msg*.
def self.raise_error(err : LibPortMIDI::PmError, msg : String)
raise case err
when LibPortMIDI::PmError::HostError
HostError.new(msg)
when LibPortMIDI::PmError::InvalidDeviceId
InvalidDeviceId.new(msg)
when LibPortMIDI::PmError::InsufficientMemory
InsufficientMemory.new(msg)
when LibPortMIDI::PmError::BufferTooSmall
BufferTooSmall.new(msg)
when LibPortMIDI::PmError::BufferOverflow
BufferOverflow.new(msg)
when LibPortMIDI::PmError::BadPtr
BadPtr.new(msg)
when LibPortMIDI::PmError::BadData
BadData.new(msg)
when LibPortMIDI::PmError::InternalError
InternalError.new(msg)
when LibPortMIDI::PmError::BufferMaxSize
BufferMaxSize.new(msg)
else
Exception.new(msg)
end
when LibPortMIDI::PmError::HostError
HostError.new(msg)
when LibPortMIDI::PmError::InvalidDeviceId
InvalidDeviceId.new(msg)
when LibPortMIDI::PmError::InsufficientMemory
InsufficientMemory.new(msg)
when LibPortMIDI::PmError::BufferTooSmall
BufferTooSmall.new(msg)
when LibPortMIDI::PmError::BufferOverflow
BufferOverflow.new(msg)
when LibPortMIDI::PmError::BadPtr
BadPtr.new(msg)
when LibPortMIDI::PmError::BadData
BadData.new(msg)
when LibPortMIDI::PmError::InternalError
InternalError.new(msg)
when LibPortMIDI::PmError::BufferMaxSize
BufferMaxSize.new(msg)
else
Exception.new(msg)
end
end

# Initializes the PortMidi system.
def self.init
LibPortMIDI.initialize()
LibPortMIDI.initialize
end

# Terminates PortMidi.
def self.terminate
LibPortMIDI.terminate()
LibPortMIDI.terminate
end

# Returns the error message that corresponds to `errnum`. Note that
# calling this method clears the error flag used by the code underlying
# `PmStream#host_error?`.
def self.get_error_text(errnum : LibPortMIDI::PmError) : String
String.new(LibPortMIDI.get_error_text(errnum))
String.new(LibPortMIDI.get_error_text(errnum))
end

# Returns the number of attached MIDI input and output devices.
def self.count_devices() : Int32
LibPortMIDI.count_devices()
def self.count_devices : Int32
LibPortMIDI.count_devices
end

# Returns the default input device ID, as defined by PortMidi.
def self.get_default_input_device_id() : Int32
LibPortMIDI.get_default_input_device_id()
def self.get_default_input_device_id : Int32
LibPortMIDI.get_default_input_device_id
end

# Returns the default output device ID, as defined by PortMidi.
def self.get_default_output_device_id() : Int32
LibPortMIDI.get_default_output_device_id()
def self.get_default_output_device_id : Int32
LibPortMIDI.get_default_output_device_id
end

# Returns a `DeviceInfo` for device *device_id*.
Expand All @@ -84,8 +92,8 @@ module PortMIDI
# Creates a PortMidi message from three MIDI bytes.
def self.message(status, data1, data2) : UInt32
((((data2.to_u32) << 16) & 0xFF0000_u32) |
(((data1.to_u32) << 8) & 0xFF00_u32) |
((status.to_u32) & 0xFF_u32))
(((data1.to_u32) << 8) & 0xFF00_u32) |
((status.to_u32) & 0xFF_u32))
end

# Extracts the status byte from a PortMidi *message*.
Expand Down
3 changes: 1 addition & 2 deletions src/port_midi/simple_midi_device.cr
Expand Up @@ -4,10 +4,9 @@ require "./output_stream"

# A wrapper around an input/output pair of streams.
class SimpleMIDIDevice

getter :input, :output
delegate :read, :has_data?, :wait_for_data, :set_filter, :set_channel_mask,
to: @input
to: @input
delegate :write, :write_short, :write_sysex, :abort_write, to: @output

# Opens streams on *input_device_num* and *output_device_num* and returns
Expand Down

0 comments on commit e74c194

Please sign in to comment.