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
Beispiel #2
0
def run(num_iter=1):
    AudioFunction.init()
    Logger.init(Logger.Mode.BOTH_FILE_AND_STDOUT)
    Adb.init()

    os.system("mkdir -p {}{}ssr_report > {}".format(ROOT_DIR, SEP, STDNUL))
    t = datetime.datetime.now()
    filename = "report_{}{:02d}{:02d}_{:02d}{:02d}{:02d}.json".format(
        t.year, t.month, t.day, t.hour, t.minute, t.second)

    device, serialno = ViewClient.connectToDeviceOrExit(serialno=None)
    push_files(serialno)
    wake_device(device, serialno)
    SSRDumpListener.init(device, serialno)

    # keymap reference:
    #   https://github.com/dtmilano/AndroidViewClient/blob/master/src/com/dtmilano/android/adb/androidkeymap.py
    device.press("HOME")
    time.sleep(1)
    AATApp.launch_app(device)
    time.sleep(1)

    trials = []
    batch_count = 1
    while num_iter > 0:
        log("-------- batch_run #{} --------".format(batch_count))
        AATApp.print_log(
            device,
            severity="i",
            tag=TAG,
            log="-------- batch_run #{} --------".format(batch_count))
        trials_batch = []
        trials_batch += playback_task_run(device,
                                          num_iter=min([num_iter, BATCH_SIZE]))
        trials_batch += record_task_run(device,
                                        serialno,
                                        num_iter=min([num_iter, BATCH_SIZE]))
        trials_batch += voip_task_run(device,
                                      serialno,
                                      num_iter=min([num_iter, BATCH_SIZE]))

        map(lambda trial: trial.put_extra(name="batch_id", value=batch_count),
            trials_batch)
        trials += trials_batch
        with open("{}{}ssr_report{}{}".format(ROOT_DIR, SEP, SEP, filename),
                  "w") as f:
            f.write(TrialHelper.to_json(trials))

        for taskname, tasktrials in TrialHelper.categorize_in(
                trials, lambda t: t.ds["task"]).items():
            valid_trials = zip(
                tasktrials,
                TrialHelper.pass_fail_list(
                    tasktrials, lambda t: t.ds["status"] == "valid"))
            valid_trials = [
                trial for trial, isvalid in valid_trials if isvalid
            ]
            num_valid = len(valid_trials)
            num_pass = len(
                filter(lambda x: x, TrialHelper.pass_fail_list(valid_trials)))
            log("task[{}] valid trials: {}/{}, pass trials: {}/{}".format(
                taskname, num_valid, len(tasktrials), num_pass, num_valid))

        num_iter -= BATCH_SIZE
        batch_count += 1

    AudioFunction.finalize()
    Logger.finalize()
    SSRDumpListener.finalize()