diff --git a/piaudio.py b/piaudio.py index 9afab86f..2d2f2c5d 100644 --- a/piaudio.py +++ b/piaudio.py @@ -9,7 +9,9 @@ from multiprocessing import Value from threading import Thread import pygame -# import alsaaudio +from sys import platform +if platform == "linux" or platform == "linux2": + import alsaaudio import threading from pydub import AudioSegment from multiprocessing import Process, Value, Array, Queue, Manager @@ -17,6 +19,9 @@ import common +def win_audio_loop(fname,ratio,stop_proc): + pass + def audio_loop(fname, ratio, stop_proc): # TODO: As a future improvment, we could precompute resampled versions of the track # at the "steady" playback rates, and only do dynamic resampling when transitioning @@ -31,9 +36,9 @@ def audio_loop(fname, ratio, stop_proc): time.sleep(0.02) wav_data = None - - - song_loaded = False + + + song_loaded = False while(True): if(stop_proc.value == 1): pass @@ -56,7 +61,7 @@ def audio_loop(fname, ratio, stop_proc): device.setformat(alsaaudio.PCM_FORMAT_S16_LE) device.setperiodsize(PERIOD) - + if len(wf.readframes(1)) == 0: raise ValueError("Empty WAV file played.") wf.rewind() @@ -143,20 +148,22 @@ class Music: def __init__(self, name): self.name = name self.transition_future_ = asyncio.Future() - + self.stop_proc = Value('i', 1) self.ratio = Value('d' , 1.0) manager = Manager() self.fname = manager.dict() self.fname['song'] = '' - - self.t = Process(target=audio_loop, args=(self.fname, self.ratio, self.stop_proc)) + if platform == "linux" or platform == "linux2": + self.t = Process(target=audio_loop, args=(self.fname, self.ratio, self.stop_proc)) + elif "win" in platform: + self.t = Process(target=win_audio_loop, args=(self.fname, self.ratio, self.stop_proc)) self.t.start() def load_audio(self, fname): self.fname['song'] = fname - + def start_audio_loop(self): self.stop_proc.value = 0 diff --git a/piparty.py b/piparty.py index 43a063a7..a3f90357 100644 --- a/piparty.py +++ b/piparty.py @@ -1,4 +1,4 @@ -import psmove, pair +import psmove import common, colors, joust, webui import yaml import time, random, json, os, os.path, sys, glob @@ -9,8 +9,10 @@ from sys import platform if platform == "linux" or platform == "linux2": import jm_dbus -elif platform == "windows": + import pair +elif "win" in platform: import win_jm_dbus as jm_dbus + import win_pair as pair import controller_process import update @@ -77,7 +79,7 @@ def track_move(serial, move_num, move, move_opts, force_color, battery, dead_cou if move_button == common.Button.SHAPES: move_opts[Opts.selection.value] = Selections.admin.value move_opts[Opts.holding.value] = Holding.holding.value - + if move_button == common.Button.UPDATE: move_opts[Opts.selection.value] = Selections.update.value move_opts[Opts.holding.value] = Holding.holding.value @@ -233,7 +235,8 @@ def track_move(serial, move_num, move, move_opts, force_color, battery, dead_cou class Menu(): def __init__(self): - self.big_update = update.check_for_update() + if platform == "linux" or platform == "linux2": + self.big_update = update.check_for_update() self.command_queue = Queue() self.joust_manager = Manager() self.ns = self.joust_manager.Namespace() @@ -272,7 +275,7 @@ def __init__(self): self.game_mode = common.Games[self.ns.settings['current_game']] self.old_game_mode = self.game_mode self.pair = pair.Pair() - + self.menu = Value('i', 1) self.controller_game_mode = Value('i',1) self.restart = Value('i',0) @@ -300,14 +303,14 @@ def __init__(self): self.rumble = {} self.kill_controller_proc = {} self.controller_sensitivity = Array('d', [0] *10) - + self.i = 0 #load audio now so it converts before the game begins self.menu_music = Music("menu") self.joust_music = Music("joust") self.zombie_music = Music("zombie") self.commander_music = Music("commander") - + self.choose_new_music() @@ -340,15 +343,16 @@ def check_for_new_moves(self): def enable_bt_scanning(self, on=True): - bt_hcis = list(jm_dbus.get_hci_dict().keys()) - - for hci in bt_hcis: - if jm_dbus.enable_adapter(hci): - self.pair.update_adapters() - if on: - jm_dbus.enable_pairable(hci) - else: - jm_dbus.disable_pairable(hci) + if platform == "linux" or platform == "linux2": + bt_hcis = list(jm_dbus.get_hci_dict().keys()) + + for hci in bt_hcis: + if jm_dbus.enable_adapter(hci): + self.pair.update_adapters() + if on: + jm_dbus.enable_pairable(hci) + else: + jm_dbus.disable_pairable(hci) def pair_usb_move(self, move): move_serial = move.get_serial() @@ -367,7 +371,7 @@ def pair_move(self, move, move_num): if move_serial not in self.tracked_moves: color = Array('i', [0] * 3) opts = Array('i', [0] * 6) - + if move_serial in self.teams: opts[Opts.team.value] = self.teams[move_serial] else: @@ -378,16 +382,16 @@ def pair_move(self, move, move_num): opts[Opts.game_mode.value] = self.game_mode.value #now start tracking the move controller - - - + + + team = Value('i',0) team_color_enum = Array('i',[0]*3) dead_move = Value( 'i',0) zombie_opt = Array('i', [0, 0, 0, 1, 0, 1, 1]) five_controller_opt = Array('i',[0]*5) - - + + commander_move_opt = Array('i', [0] * 5) invincibility = Value('b', True) fight_club_color = Value('i', 0) @@ -395,8 +399,8 @@ def pair_move(self, move, move_num): faked = Value('i', 0) rumble = Value('i', 0) kill_proc = Value('b', False) - - + + proc = Process(target= controller_process.main_track_move, args=(self.menu, self.restart, move_serial, move_num, opts, color, self.show_battery, \ self.dead_count, self.controller_game_mode, team, team_color_enum, self.controller_sensitivity, dead_move, \ self.music_speed, self.werewolf_reveal, self.show_team_colors, self.red_on_kill,zombie_opt,\ @@ -421,9 +425,9 @@ def pair_move(self, move, move_num): self.rumble[move_serial] = rumble self.kill_controller_proc[move_serial] = kill_proc self.out_moves[move.get_serial()] = Alive.on.value - - - + + + self.exclude_out_moves() @@ -435,20 +439,20 @@ def remove_controller(self, move_serial): self.tracked_moves[move_serial].join() self.tracked_moves[move_serial].terminate() del self.move_opts[move_serial] - #del self.tracked_moves[move_serial] - del self.force_color[move_serial] - del self.controller_teams[move_serial] - del self.controller_colors[move_serial] - del self.dead_moves[move_serial] - del self.zombie_opts[move_serial] - del self.commander_move_opts[move_serial] - del self.five_controller_opts[move_serial] - del self.fight_club_colors[move_serial] - del self.invincible_moves[move_serial] - del self.false_colors[move_serial] - del self.was_faked[move_serial] - del self.rumble[move_serial] - del self.kill_controller_proc[move_serial] + #del self.tracked_moves[move_serial] + del self.force_color[move_serial] + del self.controller_teams[move_serial] + del self.controller_colors[move_serial] + del self.dead_moves[move_serial] + del self.zombie_opts[move_serial] + del self.commander_move_opts[move_serial] + del self.five_controller_opts[move_serial] + del self.fight_club_colors[move_serial] + del self.invincible_moves[move_serial] + del self.false_colors[move_serial] + del self.was_faked[move_serial] + del self.rumble[move_serial] + del self.kill_controller_proc[move_serial] def game_mode_announcement(self): if self.game_mode == common.Games.JoustFFA: @@ -509,7 +513,7 @@ def check_change_mode(self): opt[Opts.game_mode.value] = self.game_mode.value if self.ns.settings['play_audio']: self.game_mode_announcement() - + def check_new_admin(self): for move, move_opt in self.move_opts.items(): if move_opt[Opts.selection.value] == Selections.admin.value: @@ -522,8 +526,8 @@ def check_new_admin(self): self.force_color[self.admin_move][2] = 0 self.admin_move = move move_opt[Opts.selection.value] = Selections.nothing.value - - + + #all controllers need to opt-in again in order fo the game to start def reset_controller_game_state(self): for move_opt in self.move_opts.values(): @@ -533,7 +537,7 @@ def reset_controller_game_state(self): for i in range(3): self.force_color[serial][i] = 0 self.random_added = [] - + def check_update(self): for move, move_opt in self.move_opts.items(): if move_opt[Opts.selection.value] == Selections.update.value: @@ -633,7 +637,7 @@ def check_admin_controls(self): Audio('audio/Menu/low.wav').start_effect() elif self.ns.settings['sensitivity'] == Sensitivity.ultra_fast.value: Audio('audio/Menu/ultra_low.wav').start_effect() - + #no admin colors in con custom teams mode if self.game_mode == common.Games.JoustTeams or self.game_mode == common.Games.Random: self.force_color[self.admin_move][0] = 0 @@ -761,6 +765,7 @@ def update_status(self,game_status): } battery_status = {} + # print(self.moves) for move in self.moves: move.poll() battery_status[move.get_serial()] = move.get_battery() @@ -846,18 +851,18 @@ def start_game(self, random_mode=False): self.menu.value = 0 self.restart.value =1 self.update_status('starting') - + self.sensitivity = self.ns.settings['sensitivity'] self.controller_sensitivity[0] = common.SLOW_MAX[self.sensitivity] self.controller_sensitivity[1] = common.SLOW_WARNING[self.sensitivity] self.controller_sensitivity[2] = common.FAST_MAX[self.sensitivity] self.controller_sensitivity[3] = common.FAST_WARNING[self.sensitivity] - + self.controller_sensitivity[4] = common.WERE_SLOW_MAX[self.sensitivity] self.controller_sensitivity[5] = common.WERE_SLOW_WARNING[self.sensitivity] self.controller_sensitivity[6] = common.WERE_FAST_MAX[self.sensitivity] self.controller_sensitivity[7] = common.WERE_FAST_WARNING[self.sensitivity] - + self.controller_sensitivity[8] = common.ZOMBIE_MAX[self.sensitivity] self.controller_sensitivity[9] = common.ZOMBIE_WARNING[self.sensitivity] @@ -931,7 +936,7 @@ def start_game(self, random_mode=False): self.restart.value =0 self.reset_controller_game_state() - + if __name__ == "__main__": InitAudio() piparty = Menu() diff --git a/win_pair.py b/win_pair.py new file mode 100644 index 00000000..e9699b3b --- /dev/null +++ b/win_pair.py @@ -0,0 +1,80 @@ +import psmove +import os + +from sys import platform +print(platform) +if platform == "linux" or platform == "linux2": + import jm_dbus +elif "win" in platform: + import win_jm_dbus as jm_dbus + +class Pair(): + """ + Manage paring move controllers to the server + """ + def __init__(self): + """Use DBus to find bluetooth controllers""" + pass + # self.hci_dict = jm_dbus.get_hci_dict() + # + # devices = self.hci_dict.values() + # self.bt_devices = {} + # for device in devices: + # self.bt_devices[device] = [] + # + # self.pre_existing_devices() + + def pre_existing_devices(self): + """ + Enumerate known devices + + For each device on each adapter, add the device's address to it's adapter's + list of known devices + """ + pass + # for hci, addr in self.hci_dict.items(): + # proxy = jm_dbus.get_adapter_proxy(hci) + # devices = jm_dbus.get_node_child_names(proxy) + # + # self.bt_devices[addr] = jm_dbus.get_attached_addresses(hci) + + def update_adapters(self): + """ + Rescan for bluetooth adapters that may not have existed on program launch + """ + pass + # self.hci_dict = jm_dbus.get_hci_dict() + # + # for addr in self.hci_dict.values(): + # if addr not in self.bt_devices.keys(): + # self.bt_devices[addr] = [] + # + # self.pre_existing_devices() + + def check_if_not_paired(self, addr): + pass + # for devs in self.bt_devices.keys(): + # if addr in self.bt_devices[devs]: + # return False + # return True + + def get_lowest_bt_device(self): + pass + # num = 9999999 + # print(self.bt_devices) + # for dev in self.bt_devices.keys(): + # if len(self.bt_devices[dev]) < num: + # num = len(self.bt_devices[dev]) + # + # for dev in self.bt_devices.keys(): + # if len(self.bt_devices[dev]) == num: + # return dev + # return '' + + def pair_move(self, move): + pass + # if move and move.get_serial(): + # if move.connection_type == psmove.Conn_USB: + # self.pre_existing_devices() + # if self.check_if_not_paired(move.get_serial().upper()): + # move.pair_custom(self.get_lowest_bt_device())