void chdir_kazoo () { const char * KAZOO = getenv("KAZOO"); if (KAZOO) { ASSERTW(not chdir(KAZOO), "directory $KAZOO does not exist"); } else { const char * HOME = getenv("HOME"); if (HOME) { string KAZOO = string(HOME) + string("/kazoo"); ASSERTW(not chdir(KAZOO.c_str()), "directory $HOME/kazoo does not exist"); } else { WARN("HOME is not in environment"); } } }
/*===========================================================================* * get_work * *===========================================================================*/ PRIVATE void get_work(struct worker_thread *worker) { /* Find new work to do. Work can be 'queued', 'pending', or absent. In the * latter case wait for new work to come in. */ struct job *new_job; struct fproc *rfp; ASSERTW(worker); self = worker; /* Do we have queued work to do? */ if ((new_job = worker->w_job.j_next) != NULL) { worker->w_job = *new_job; free(new_job); return; } else if (worker != &sys_worker && worker != &dl_worker && pending > 0) { /* Find pending work */ for (rfp = &fproc[0]; rfp < &fproc[NR_PROCS]; rfp++) { if (rfp->fp_flags & FP_PENDING) { worker->w_job = rfp->fp_job; rfp->fp_job.j_func = NULL; rfp->fp_flags &= ~FP_PENDING; /* No longer pending */ pending--; assert(pending >= 0); return; } } panic("Pending work inconsistency"); } /* Wait for work to come to us */ worker_sleep(worker); }
/*===========================================================================* * worker_init * *===========================================================================*/ PUBLIC void worker_init(struct worker_thread *wp) { /* Initialize worker thread */ if (!init) { threads_init(); if (mthread_attr_init(&tattr) != 0) panic("failed to initialize attribute"); if (mthread_attr_setstacksize(&tattr, TH_STACKSIZE) != 0) panic("couldn't set default thread stack size"); if (mthread_attr_setdetachstate(&tattr, MTHREAD_CREATE_DETACHED) != 0) panic("couldn't set default thread detach state"); pending = 0; init = 1; } ASSERTW(wp); wp->w_job.j_func = NULL; /* Mark not in use */ wp->w_next = NULL; if (mutex_init(&wp->w_event_mutex, NULL) != 0) panic("failed to initialize mutex"); if (cond_init(&wp->w_event, NULL) != 0) panic("failed to initialize conditional variable"); if (mthread_create(&wp->w_tid, &tattr, worker_main, (void *) wp) != 0) panic("unable to start thread"); yield(); }
/*===========================================================================* * worker_main * *===========================================================================*/ PRIVATE void *worker_main(void *arg) { /* Worker thread main loop */ struct worker_thread *me; me = (struct worker_thread *) arg; ASSERTW(me); while(TRUE) { get_work(me); /* Register ourselves in fproc table if possible */ if (me->w_job.j_fp != NULL) { me->w_job.j_fp->fp_wtid = me->w_tid; } /* Carry out work */ me->w_job.j_func(&me->w_job); /* Mark ourselves as done */ me->w_job.j_func = NULL; } return(NULL); /* Unreachable */ }
/*===========================================================================* * worker_stop * *===========================================================================*/ PUBLIC void worker_stop(struct worker_thread *worker) { ASSERTW(worker); /* Make sure we have a valid thread */ if (worker->w_job.j_fp) worker->w_job.j_fp->fp_sendrec->m_type = EIO; else worker->w_job.j_m_in.m_type = EIO; worker_wake(worker); }
/*===========================================================================* * worker_wake * *===========================================================================*/ PRIVATE void worker_wake(struct worker_thread *worker) { /* Signal a worker to wake up */ ASSERTW(worker); if (mutex_lock(&worker->w_event_mutex) != 0) panic("unable to lock event mutex"); if (cond_signal(&worker->w_event) != 0) panic("unable to signal conditional variable"); if (mutex_unlock(&worker->w_event_mutex) != 0) panic("unable to unlock event mutex"); }
/*===========================================================================* * worker_waiting_for * *===========================================================================*/ PRIVATE int worker_waiting_for(struct worker_thread *worker, endpoint_t proc_e) { ASSERTW(worker); /* Make sure we have a valid thread */ if (worker->w_job.j_func != NULL) { if (worker->w_job.j_fp != NULL) { return(worker->w_job.j_fp->fp_task == proc_e); } } return(0); }
/*===========================================================================* * worker_sleep * *===========================================================================*/ PRIVATE void worker_sleep(struct worker_thread *worker) { ASSERTW(worker); assert(self == worker); if (mutex_lock(&worker->w_event_mutex) != 0) panic("unable to lock event mutex"); if (cond_wait(&worker->w_event, &worker->w_event_mutex) != 0) panic("could not wait on conditional variable"); if (mutex_unlock(&worker->w_event_mutex) != 0) panic("unable to unlock event mutex"); self = worker; }
/*===========================================================================* * worker_stop * *===========================================================================*/ void worker_stop(struct worker_thread *worker) { ASSERTW(worker); /* Make sure we have a valid thread */ if (worker->w_task != NONE) { /* This thread is communicating with a driver or file server */ if (worker->w_drv_sendrec != NULL) { /* Driver */ worker->w_drv_sendrec->m_type = EIO; } else if (worker->w_fs_sendrec != NULL) { /* FS */ worker->w_fs_sendrec->m_type = EIO; } else { panic("reply storage consistency error"); /* Oh dear */ } } else { worker->w_job.j_m_in.m_type = EIO; } worker_wake(worker); }
History & History::add (const Vector<float> & present) { ASSERT_SIZE(present, m_size); // advance all frames for (Frame * f = m_frames; f; f = f->next) { f->time += 1; } // merge redundant frames of same rank for (Frame * f = m_frames; f and f->next; f = f->next) { Frame * g = f->next; if (g->rank != f->rank) { ASSERTW(g->rank == f->rank + 1, "found gap in history frames; try using higher density"); continue; } while (g->next and (g->next->rank == g->rank)) { f = g; g = g->next; } const float resolution = 0.25f; if (log_time(g->time) - log_time(f->time) < resolution) { merge_frames(f); } } // pop obsolete frames for (Frame * f = m_frames; f and f->next; f = f->next) { const float padding = 1; if (log_time(f->next->time) >= m_length + padding) { crop_to_frame(f); } } // add new frame add_frame(present); return * this; }
Spline::Spline ( size_t size_in, size_t size_out, const float * fun) : m_size_in(size_in), m_size_out(size_out), m_i0(size_in), m_w0(size_in), m_w1(size_in), m_scale_bwd(size_out), m_scaled_e_rng(size_out), m_tolerance(1e-10) { ASSERTW(size_in >= size_out, "spline is being used : small -> large; consider reversing"); setup(fun); }
Spline::Spline ( size_t size_in, size_t size_out) : m_size_in(size_in), m_size_out(size_out), m_i0(size_in), m_w0(size_in), m_w1(size_in), m_scale_bwd(size_out), m_scaled_e_rng(size_out), m_tolerance(1e-10) { ASSERTW(size_in >= size_out, "spline is being used : small -> large; consider reversing"); Vector<float> vect(m_size_in); sample_uniform(vect); setup(vect); }
void Loudness::transform_bwd ( Vector<float> & loudness_in, Vector<float> & energy_out) { ASSERT_SIZE(loudness_in, m_size); ASSERT_SIZE(energy_out, m_size); // ensure loudness_in is in [0,1] float min_in = min(loudness_in); float max_in = max(loudness_in); ASSERTW(min_in >= 0, "min loudness is too small: " << min_in); ASSERTW(max_in <= 1, "max loudness is too large: " << max_in); if (min_in < m_tolerance) { max_in += min_in + m_tolerance; loudness_in += min_in + m_tolerance; min_in = m_tolerance; } if (max_in > 1 - m_tolerance) { loudness_in *= (1 - m_tolerance) / max_in; min_in *= (1 - m_tolerance) / max_in; max_in = 1 - m_tolerance; } m_max_loudness_mutex.lock(); const float max_loudness = m_max_loudness - m_tolerance; m_max_loudness_mutex.unlock(); energy_out[0] = energy_out[m_size-1] = 0; for (size_t i = 1; i < m_size-1; ++i) { ASSERT1_FINITE(loudness_in[i]); // contract float loudness_new = contract_loudness(loudness_in[i]); // approximate lowpass filter float loudness_int = m_history_bwd[i]; loudness_new += (1-m_ss_factor) * loudness_int; m_current_bwd[i] = loudness_new; // XXX loudness_new may no longer lie in [0,1] // rescale from [0,1] loudness_new *= max_loudness; // apply e^1/3 power law energy_out[i] = powf(loudness_new, 3); ASSERT1_FINITE(energy_out[i]); } // update history float a = m_decay_factor_fast; float b = 1-a; for (size_t i = 1; i < m_size-1; ++i) { float & h = m_history_bwd[i]; h = a * h + b * max (0.0f, m_current_bwd[i] - 0.5f * ( m_current_bwd[i-1] + m_current_bwd[i+1])); } }
/*===========================================================================* * worker_signal * *===========================================================================*/ PUBLIC void worker_signal(struct worker_thread *worker) { ASSERTW(worker); /* Make sure we have a valid thread */ worker_wake(worker); }