class ZXUtils::AYBasicPlayer

AY-3-8910/8912 Basic player

This is a wrapper over AYMusicPlayer with interfaces suitable to be used directly from the ZX Spectrum's BASIC.

require 'zxutils/ay_music_player'
require 'zxlib/basic'
include ZXLib
include ZXUtils

player = AYBasicPlayer.new 0xEED3
puts player.debug
program = Basic.parse_source <<-EOC
   1 DEF FN m(a)=USR #{player[:init_music]}
  10 CLS: PRINT "Insert tape with a music module"
  20 LOAD ""CODE 32768
 100 REM initialize music
 110 RANDOMIZE FN m(32768)
 200 REM play in loop
 210 PRINT USR #{player[:play_loop]}
 299 STOP
 300 REM play interval
 310 LET ticks=USR #{player[:play_interval]}: PAUSE 1: GO TO 310
 400 REM mute sound
 410 RANDOMIZE USR #{player[:mute_sound]}
 499 STOP
 500 REM current counter
 510 PRINT USR #{player[:get_counter]}
9998 STOP: RUN
9999 CLEAR 32767: LOAD "player"CODE: RANDOMIZE USR #{player[:init_music]}: RUN
EOC
puts program.to_source escape_keywords: true
program.save_tap "player", line: 9999
player.save_tap "player", append: true

Public Instance Methods

get_counter() click to toggle source

Returns the current value of the music counter.

# File lib/zxutils/ay_music_player.rb, line 351
ns :get_counter do
                    ld   bc, [music.music_control.counter]
                    ret
end
init_music() click to toggle source

Initializes music track. Sets up the player.

To setup the player (once):

RANDOMIZE USR #{player[:init_music]}

To initialize a music module:

1 DEF FN m(a)=USR #{player[:init_music]}
LOAD ""CODE 40000
PRINT FN m(40000)
# File lib/zxutils/ay_music_player.rb, line 297
ns :init_music do
                    find_def_fn_args 1, subroutine:false, cf_on_direct:false do
                        with_saved :once, :exx, hl, ret: true do
                          call  setup
                          ld    hl, 0x19CF # 0xCF 0x19 : report_error 'Q Parameter error'
                          ld    [once], hl
                        end
                    end
                    read_positive_int_value d, e
                    ex   de, hl
                    jr   Z, init
                    report_error 'A Invalid argument'
end
play_interval() click to toggle source

Plays single music track tick. Call repeatedly on equal intervals to play music.

Returns the current value of the music counter.

# File lib/zxutils/ay_music_player.rb, line 336
ns :play_interval do
                    exx
                    push hl
                    di
                    push iy
                    ay_music_preserve_io_ports_state(music.music_control, music.play, bc_const_loaded:false, io_ay:io_ay)
                    call music.play
                    pop  iy
                    ei
                    pop hl
                    exx
end
play_loop() click to toggle source

Plays music track in a loop until any key has been pressed.

# File lib/zxutils/ay_music_player.rb, line 313
ns :play_loop do
                    call release_key
  forever           halt
                    call play_interval
                    call check_key
                    jr  Z, forever
                    call release_key
                    push bc
                    call mute_sound
                    pop  bc
                    ret
end