Ejemplo n.º 1
0
def playback_task_run(num_iter, num_seek_test, gmhandler):
    log("playback_task_run++")

    if not gmhandler.cache_init:
        log("gmhandler.walk_through()")
        if not gmhandler.walk_through():
            log("failed to walk through the UI of the google music")
            gmhandler.dump()
            return []

        log("gmhandler.cache\n{}".format(
            json.dumps(gmhandler.cache, indent=4, ensure_ascii=False)))
    else:
        log("gmhandler.to_top()")
        gmhandler.to_top()

    if not gmhandler.to_control_panel():
        log("failed to go to the control panel of the google music")
        gmhandler.dump()
        return []

    trials = []

    def gmplayback_pass_check(trial):
        subtasknames = ["pause", "resume", "next", "start", "seek"]
        for subtaskname in subtasknames:
            if not subtaskname in trial.ds["extra"].keys():
                return False

            subtask_result = trial.ds["extra"][subtaskname]
            if isinstance(subtask_result, str) and subtask_result != "pass":
                return False

            if isinstance(subtask_result,
                          dict) and subtask_result["result"] != "pass":
                return False

        return True

    stm = DetectionStateListener()

    for i in range(num_iter):
        log("-------- playback_task #{} --------".format(i + 1))
        trial = Trial(taskname="playback", pass_check=gmplayback_pass_check)
        trial.put_extra(name="iter_id", value=i + 1)

        song = gmhandler.control_panel.get_current_song()
        log("the current song:\n{}".format(
            json.dumps(song, indent=4, ensure_ascii=False)))
        trial.put_extra(name="song_title", value=song["name"])
        target_freq = int(song["name"].split("Hz")[0])

        stm.clear()

        log("ToneDetector.start_listen(target_freq={})".format(target_freq))
        ToneDetector.start_listen(
            target_freq=target_freq,
            cb=lambda event: stm.tone_detected_event_cb(event))

        gmhandler.control_panel.play()
        time.sleep(1)
        if stm.wait_for_event(DetectionStateListener.Event.ACTIVE,
                              timeout=10) < 0:
            log("The {}Hz tone is not detected....".format(target_freq))
            trial.put_extra("start", "failed")
        else:
            log("The {}Hz tone is detected".format(target_freq))
            trial.put_extra("start", "pass")

        gmhandler.control_panel.play_pause()
        if stm.wait_for_event(DetectionStateListener.Event.INACTIVE,
                              timeout=10) < 0:
            log("The {}Hz tone seems not to be stopped".format(target_freq))
            trial.put_extra("pause", "failed")
        else:
            log("The {}Hz tone has been stopped".format(target_freq))
            trial.put_extra("pause", "pass")

        gmhandler.control_panel.play_pause()
        if stm.wait_for_event(DetectionStateListener.Event.ACTIVE,
                              timeout=10) < 0:
            log("The {}Hz tone is not resumed....".format(target_freq))
            trial.put_extra("resume", "failed")
        else:
            log("The {}Hz tone is resumed".format(target_freq))
            trial.put_extra("resume", "pass")

        result = "pass"
        for _ in range(num_seek_test):
            import random
            v = random.uniform(0., 1.) * 0.9
            log("seek to {}".format(v))
            gmhandler.control_panel.seek(v)
            time.sleep(0.1)
            if stm.wait_for_event(DetectionStateListener.Event.ACTIVE,
                                  timeout=10) < 0:
                log("seek failed: the {}Hz tone is not detected".format(
                    target_freq))
                result = "failed"

        trial.put_extra("seek", {
            "num_trials": num_seek_test,
            "result": result
        })
        ToneDetector.stop_listen()
        stm.clear()

        gmhandler.control_panel.next()
        song = gmhandler.control_panel.get_current_song()
        log("the current song:\n{}".format(
            json.dumps(song, indent=4, ensure_ascii=False)))
        trial.put_extra(name="next_song_title", value=song["name"])
        target_freq = int(song["name"].split("Hz")[0])
        log("ToneDetector.start_listen(target_freq={})".format(target_freq))
        ToneDetector.start_listen(
            target_freq=target_freq,
            cb=lambda event: stm.tone_detected_event_cb(event))

        if stm.wait_for_event(DetectionStateListener.Event.ACTIVE,
                              timeout=10) < 0:
            log("The {}Hz tone is not detected....".format(target_freq))
            trial.put_extra("next", "failed")
        else:
            log("The {}Hz tone is detected".format(target_freq))
            trial.put_extra("next", "pass")

        gmhandler.control_panel.play_pause()
        ToneDetector.stop_listen()
        time.sleep(1)

        trials.append(trial)

    log("playback_task_run--")
    return trials
Ejemplo n.º 2
0
def record_task_run(device, serialno, num_iter=1, num_freqs=1):
    log("record_task_run++")
    log("launch AAT app")
    AATApp.launch_app(device)
    time.sleep(2)

    trials = []

    log("dev_record_start")
    AATApp.record_start(device)
    time.sleep(2)

    stm = DetectionStateListener()

    def gen_freq():
        import random
        return 440. * 2**(random.randint(-12, 12) / 12.)

    for i in range(num_iter):
        log("-------- record_task #{} --------".format(i + 1))

        trial = Trial(taskname="record", pass_check= \
            lambda t: "result" in t.ds["extra"].keys() and t.ds["extra"]["result"] == "pass")
        trial.put_extra(name="iter_id", value=i + 1)

        AATApp.print_log(device,
                         severity="i",
                         tag=TAG,
                         log="record_task #{}".format(i + 1))

        result = "pass"
        freqs = []
        for _ in range(num_freqs):
            stm.clear()
            target_freq = gen_freq()
            freqs.append(target_freq)

            log("ToneDetector.start_listen(serialno={}, target_freq={})".
                format(serialno, target_freq))
            ToneDetector.start_listen(
                serialno=serialno,
                target_freq=target_freq,
                cb=lambda event: stm.tone_detected_event_cb(event))
            time.sleep(2)

            log("AudioFunction.play_sound(out_freq={})".format(target_freq))
            AudioFunction.play_sound(out_freq=target_freq)

            if stm.wait_for_event(DetectionStateListener.Event.ACTIVE,
                                  timeout=10) < 0:
                log("The {}Hz tone is not detected".format(target_freq))

                now = "_".join("{}".format(datetime.datetime.now()).split())
                log("dump the recorded pcm to \"sdcard/PyAAT/dump_{}\"".format(
                    now))
                AATApp.record_dump(device, "sdcard/PyAAT/dump_{}".format(now))

                log("start dumping the process during capturing the frequency..."
                    )
                ToneDetector.WORK_THREAD.dump()

                result = "failed"

            log("ToneDetectorForDeviceThread.adb-read-prop-max-elapsed: {} ms".format( \
                ToneDetector.WORK_THREAD.extra["adb-read-prop-max-elapsed"]))
            log("ToneDetectorForDeviceThread.freq-cb-max-elapsed: {} ms".format( \
                ToneDetector.WORK_THREAD.extra["freq-cb-max-elapsed"]))

            AudioFunction.stop_audio()
            ToneDetector.stop_listen()
            time.sleep(2)

        trial.put_extra(name="result", value=result)
        trial.put_extra(name="test_freqs", value=freqs)
        trials.append(trial)

    log("dev_record_stop")
    AATApp.record_stop(device)
    time.sleep(2)

    log("record_task_run--")
    return trials
Ejemplo n.º 3
0
def record_task_run(device, serialno, num_iter=1):
    log("record_task_run++")

    trials = []

    log("dev_record_start")
    Adb.execute(
        cmd=["shell", "rm", "-f", "sdcard/AudioFunctionsDemo-record-prop.txt"],
        serialno=serialno)
    AATApp.record_start(device)
    time.sleep(2)

    stm = DetectionStateListener()

    out_freq = OUT_FREQ
    log("ToneDetector.start_listen(serialno={}, target_freq={})".format(
        serialno, out_freq))
    listen_params = {
        "serialno": serialno,
        "target_freq": out_freq,
        "cb": lambda event: stm.tone_detected_event_cb(event),
        "dclass": ToneDetectorForDeviceThread
    }
    try:
        ToneDetector.start_listen(**listen_params)
    except:

        def record_parse_detector(record_info):
            return record_info[1]

        listen_params["params"] = {
            "detector_reg_func": AATApp.record_detector_register,
            "detector_unreg_func": AATApp.record_detector_unregister,
            "detector_setparams_func": AATApp.record_detector_set_params,
            "info_func": AATApp.record_info,
            "parse_detector_func": record_parse_detector
        }
        ToneDetector.start_listen(**listen_params)

    log("AudioFunction.play_sound(out_freq={})".format(out_freq))

    has_triggered_bugreport = False

    time.sleep(3)
    for i in range(num_iter):
        AudioFunction.play_sound(out_freq=out_freq)
        log("-------- record_task #{} --------".format(i + 1))
        Adb.execute(cmd=[
            "shell", "rm", "-f", "sdcard/AudioFunctionsDemo-record-prop.txt"
        ],
                    serialno=serialno)

        trial = Trial(taskname="record",
                      pass_check=lambda t: t.ds["extra"]["elapsed"] > 0)
        trial.put_extra(name="iter_id", value=i + 1)

        AATApp.print_log(device,
                         severity="i",
                         tag=GLOBAL["tag"],
                         log="record_task #{}".format(i + 1))

        ToneDetector.WORK_THREAD.clear_dump()
        stm.clear()
        stm.reset()
        if stm.wait_for_event(DetectionStateListener.Event.ACTIVE,
                              timeout=10) < 0:
            log("the tone was not detected, abort the iteration this time...")
            log("ToneDetectorForDeviceThread.adb-read-prop-max-elapsed: {} ms".format( \
                ToneDetector.WORK_THREAD.extra["adb-read-prop-max-elapsed"]))
            log("ToneDetectorForDeviceThread.freq-cb-max-elapsed: {} ms".format( \
                ToneDetector.WORK_THREAD.extra["freq-cb-max-elapsed"]))
            trial.invalidate(
                errormsg="early return, possible reason: tx no sound")
            trials.append(trial)

            now = "_".join("{}".format(datetime.datetime.now()).split())
            log("dump the recorded pcm to \"sdcard/PyAAT/dump_{}\"".format(
                now))
            AATApp.record_dump(device, "sdcard/PyAAT/dump_{}".format(now))
            ToneDetector.WORK_THREAD.dump()
            continue

        log("trigger_{}()".format(GLOBAL["test_config"]))
        if GLOBAL["test_config"] == "ssr":
            trigger_ssr(serialno)
        else:
            trigger_asr(serialno)

        log("waiting for inactive....")
        if stm.wait_for_event(DetectionStateListener.Event.INACTIVE,
                              timeout=10) >= 0:
            Adb.execute(cmd=[
                "shell", "rm", "-f",
                "sdcard/AudioFunctionsDemo-record-prop.txt"
            ],
                        serialno=serialno)

            log("Waiting for {} recovery".format(
                "SSR" if GLOBAL["test_config"] == "ssr" else "ASR"))
            elapsed = stm.wait_for_event(
                DetectionStateListener.Event.RISING_EDGE, timeout=15)
            if elapsed < 0:
                log("Timeout in waiting for rising event, possibly caused by missing event not being caught"
                    )
                log("Waiting for the tone being detected")
                if stm.wait_for_event(DetectionStateListener.Event.ACTIVE,
                                      timeout=10) < 0:
                    log("The tone is not detected")
                    if not has_triggered_bugreport:
                        AudioFunction.stop_audio()
                        log("get bugreport...")
                        p = trigger_bugreport(device)
                        trial.put_extra("bugreport", p)
                        has_triggered_bugreport = True
                    now = "_".join("{}".format(
                        datetime.datetime.now()).split())
                    log("dump the recorded pcm to \"sdcard/PyAAT/dump_{}\"".
                        format(now))
                    AATApp.record_dump(device,
                                       "sdcard/PyAAT/dump_{}".format(now))

                    log("start dumping the process during capturing the frequency..."
                        )
                    ToneDetector.WORK_THREAD.dump()
                else:
                    log("The tone is detected, please also check the device log for confirming if it is a false alarm"
                        )
                    trial.put_extra(name="msg", value="possible false alarm")
        else:
            log("No inactive event")
            elapsed = 0

        log("elapsed: {} ms".format(elapsed))
        AudioFunction.stop_audio()

        log("ToneDetectorForDeviceThread.adb-read-prop-max-elapsed: {} ms".format( \
            ToneDetector.WORK_THREAD.extra["adb-read-prop-max-elapsed"]))
        log("ToneDetectorForDeviceThread.freq-cb-max-elapsed: {} ms".format( \
            ToneDetector.WORK_THREAD.extra["freq-cb-max-elapsed"]))

        trial.put_extra(name="elapsed", value=elapsed)
        trials.append(trial)

        elapsed = stm.wait_for_event(DetectionStateListener.Event.INACTIVE,
                                     timeout=10)

        import random
        out_freq = OUT_FREQ * 2**(random.randint(0, 12) / 12.)
        ToneDetector.set_target_frequency(target_freq=out_freq)
        log("ToneDetector.set_target_frequency(serialno={}, target_freq={})".
            format(serialno, out_freq))

        if elapsed >= 0:
            time.sleep(30 - elapsed / 1000.)

    log("-------- record_task done --------")
    log("AudioFunction.stop_audio()")

    log("ToneDetector.stop_listen()")
    ToneDetector.stop_listen()

    log("dev_record_stop")
    AATApp.record_stop(device)
    time.sleep(5)

    log("record_task_run--")
    relaunch_app(device)
    return trials