static struct room *room_load(unsigned room_id) { struct room *r; char numbuf[22]; /* big enough for a signed 64-bit decimal */ struct fdb_read_handle *h; const char *name, *value; assert(room_id > 0); if(room_id<=0) return NULL; snprintf(numbuf, sizeof numbuf, "%u", room_id); h=fdb.read_begin("rooms", numbuf); if(!h) { b_log(B_LOG_ERROR, "room", "could not load room \"%s\"", numbuf); return NULL; } r=calloc(1, sizeof *r); if(!r) { /* TODO: do perror? */ b_log(B_LOG_ERROR, "calloc", "not allocate room \"%s\"", numbuf); fdb.read_end(h); return NULL; } while(fdb.read_next(h, &name, &value)) { if(!room_attr_set(r, name, value)) { b_log(B_LOG_ERROR, "room", "could not load room \"%s\"", numbuf); room_ll_free(r); fdb.read_end(h); return NULL; } } fdb.read_end(h); /* r->id wasn't set, this is a problem. */ if(!r->id) { b_log(B_LOG_ERROR, "room", "id not set for room \"%u\"", room_id); room_ll_free(r); return NULL; } /* r->id doesn't match the file the room is stored under. */ if(r->id!=room_id) { b_log(B_LOG_ERROR, "room", "id was set to \"%u\" but should be \"%u\"", r->id, room_id); room_ll_free(r); return NULL; } return r; }
/** * write a room structure to disk, if it is not dirty (dirty_fl). */ static int room_save(struct room *r) { struct attr_entry *curr; struct fdb_write_handle *h; char numbuf[22]; /* big enough for a signed 64-bit decimal */ assert(r != NULL); if(!r->dirty_fl) return 1; /* already saved - don't do it again. */ /* refuse to save room 0. */ if(!r->id) { b_log(B_LOG_ERROR, "room", "attempted to save room \"%u\", but it is reserved", r->id); return 0; } snprintf(numbuf, sizeof numbuf, "%u", r->id); h=fdb.write_begin("rooms", numbuf); if(!h) { b_log(B_LOG_ERROR, "room", "could not save room \"%s\"", numbuf); return 0; /* failure */ } fdb.write_format(h, "id", "%u", r->id); if(r->name.short_str) fdb.write_pair(h, "name.short", r->name.short_str); if(r->name.long_str) fdb.write_pair(h, "name.long", r->name.long_str); if(r->desc.short_str) fdb.write_pair(h, "desc.short", r->desc.short_str); if(r->desc.long_str) fdb.write_pair(h, "desc.long", r->desc.long_str); if(r->owner) fdb.write_pair(h, "owner", r->owner); if(r->creator) fdb.write_pair(h, "creator", r->creator); for(curr=LIST_TOP(r->extra_values);curr;curr=LIST_NEXT(curr, list)) { fdb.write_pair(h, curr->name, curr->value); } if(!fdb.write_end(h)) { b_log(B_LOG_ERROR, "room", "could not save room \"%s\"", numbuf); return 0; /* failure */ } r->dirty_fl=0; b_log(B_LOG_INFO, "room", "saved room \"%s\"", numbuf); return 1; }
/** * load room into cache, if not already loaded, then increase reference count * of room. */ static struct room *room_get(unsigned room_id) { struct room *curr; /* refuse to open room 0. */ if(!room_id) return NULL; /* look for room in the cache. */ for(curr=LIST_TOP(room_cache);curr;curr=LIST_NEXT(curr, room_cache)) { if(curr->id==room_id) break; } if(!curr) { /* not in the cache? load the room. */ curr=room_load(room_id); } if(curr) { /* place entry at the top of the cache. */ LIST_INSERT_HEAD(&room_cache, curr, room_cache); curr->refcount++; } if(!curr) { b_log(B_LOG_WARN, "room", "could not access room \"%u\"", room_id); } return curr; }
static int initialize(void) { struct fdb_iterator *it; const char *id; b_log(B_LOG_INFO, "room", "Room system loaded (" __FILE__ " compiled " __TIME__ " " __DATE__ ")"); LIST_INIT(&room_cache); fdb.domain_init("rooms"); it=fdb.iterator_begin("rooms"); if(!it) { b_log(B_LOG_CRIT, "room", "could not load rooms!"); return 0; /* could not load. */ } /* preflight all of the rooms. */ while((id=fdb.iterator_next(it))) { struct room *r; unsigned room_id; char *endptr; b_log(B_LOG_DEBUG, "room", "Found room: \"%s\"", id); room_id=strtoul(id, &endptr, 10); if(*endptr) { b_log(B_LOG_CRIT, "room", "room id \"%s\" is invalid!", id); fdb.iterator_end(it); return 0; /* could not load */ } r=room_load(room_id); if(!r) { b_log(B_LOG_CRIT, "room", "could not load rooms!"); fdb.iterator_end(it); return 0; /* could not load */ } room_ll_free(r); } fdb.iterator_end(it); service_attach_room(&plugin_class.base_class, &plugin_class.room_interface); return 1; }
static int shutdown(void) { struct room *curr; b_log(B_LOG_INFO, "room", "Room system shutting down.."); /* check to make sure no rooms are still in use. */ for(curr=LIST_TOP(room_cache);curr;curr=LIST_NEXT(curr, room_cache)) { if(curr->refcount>0) { b_log(B_LOG_ERROR, "room", "cannot shut down, room \"%u\" still in use.", curr->id); return 0; /* refuse to unload */ } } /* save all dirty objects and free all data. */ while((curr=LIST_TOP(room_cache))) { LIST_REMOVE(curr, room_cache); room_save(curr); room_ll_free(curr); } service_detach_room(&plugin_class.base_class); b_log(B_LOG_INFO, "room", "Room system ended."); return 1; }
void AGC_stepImpl(const emlrtStack *sp, comm_AGC *obj, const creal_T x[1408], creal_T y[1408]) { real_T g; real_T K; real_T dv1[1408]; real_T dv2[1408]; real_T dv3[99]; real_T logAbsX2[1408]; int32_T p; emlrtStack st; st.prev = sp; st.tls = sp->tls; g = obj->Gain; K = obj->pCastedStepSize; b_abs(x, dv1); st.site = &kb_emlrtRSI; power(&st, dv1, dv2); filter(dv2, obj->FilterState, logAbsX2, dv3); for (p = 0; p < 99; p++) { obj->FilterState[p] = dv3[p]; } st.site = &lb_emlrtRSI; b_log(&st, logAbsX2); for (p = 0; p < 1408; p++) { y[p].re = g; y[p].im = 0.0; g = muDoubleScalarMin(g + K * (0.0 - (logAbsX2[p] + 2.0 * g)), 3.4538776394910684); } b_exp(y); for (p = 0; p < 1408; p++) { K = y[p].re; y[p].re = x[p].re * y[p].re - x[p].im * y[p].im; y[p].im = x[p].re * y[p].im + x[p].im * K; } obj->Gain = g; }
/* Function Definitions */ void mfcc_bare(const real_T samples_in_window[128], const real_T hamming_coeff [128], const real_T mel_filterbank[2016], real_T fftA, real_T fftB, const creal_T dct_coeff[32], creal_T mel[13]) { real_T b_samples_in_window[128]; int32_T i3; creal_T samples_in_freq[128]; int32_T i4; int32_T i5; int32_T i6; int32_T ar; int32_T ib; creal_T samples_in_freq_data[65]; int32_T tmp_data[65]; int32_T ia; int32_T loop_ub; int32_T samples_in_freq_size[2]; int32_T tmp_size[2]; real_T b_tmp_data[65]; real_T b_data[65]; real_T melfilter_resps[32]; real_T b_melfilter_resps[32]; creal_T dcv0[32]; int32_T c_tmp_data[65]; int32_T b_samples_in_freq_size[2]; creal_T dc0; /* initialization */ /* 8kHz sampling frequency. */ /* 128 (size of windows). */ /* 64 step size for subsequent windows. */ /* 26 (# of mel frequency pass band filters). */ /* overrride this to power of 2. */ /* # 12 (# of cepstral coefficients (output features)) */ /* compute features for one input window */ for (i3 = 0; i3 < 128; i3++) { b_samples_in_window[i3] = samples_in_window[i3] * hamming_coeff[i3]; } fft(b_samples_in_window, samples_in_freq); if (fftA > fftB) { i3 = 1; i4 = 0; } else { i3 = (int32_T)fftA; i4 = (int32_T)fftB; } if (fftA > fftB) { i5 = 1; i6 = 0; } else { i5 = (int32_T)fftA; i6 = (int32_T)fftB; } if (fftA > fftB) { ar = 1; ib = 0; } else { ar = (int32_T)fftA; ib = (int32_T)fftB; } ia = (ib - ar) + 1; loop_ub = ib - ar; for (ib = 0; ib <= loop_ub; ib++) { tmp_data[ib] = ar + ib; } samples_in_freq_size[0] = 1; samples_in_freq_size[1] = ia; loop_ub = ia - 1; for (ar = 0; ar <= loop_ub; ar++) { samples_in_freq_data[ar] = samples_in_freq[tmp_data[ar] - 1]; } d_abs(samples_in_freq_data, samples_in_freq_size, b_tmp_data, tmp_size); loop_ub = tmp_size[1] - 1; for (ar = 0; ar <= loop_ub; ar++) { b_data[ar] = b_tmp_data[ar]; } if (tmp_size[1] == 1) { for (ar = 0; ar < 32; ar++) { melfilter_resps[ar] = 0.0; for (ib = 0; ib < 63; ib++) { melfilter_resps[ar] += mel_filterbank[ar + (ib << 5)] * b_data[ib]; } } } else { memset(&melfilter_resps[0], 0, sizeof(real_T) << 5); if (63 == tmp_size[1]) { for (ar = 0; ar < 32; ar++) { melfilter_resps[ar] = 0.0; for (ib = 0; ib < 63; ib++) { melfilter_resps[ar] += mel_filterbank[ar + (ib << 5)] * b_data[ib]; } } } else { memset(&melfilter_resps[0], 0, sizeof(real_T) << 5); ar = -1; for (ib = 0; ib < 63; ib++) { if (b_data[ib] != 0.0) { ia = ar; for (loop_ub = 0; loop_ub < 32; loop_ub++) { ia++; melfilter_resps[loop_ub] += b_data[ib] * mel_filterbank[ia]; } } ar += 32; } } } b_log(melfilter_resps); /* Compute DCT */ for (ar = 0; ar < 16; ar++) { b_melfilter_resps[ar] = melfilter_resps[ar << 1]; } for (ar = 0; ar < 16; ar++) { b_melfilter_resps[ar + 16] = melfilter_resps[31 + -2 * ar]; } b_fft(b_melfilter_resps, dcv0); for (ar = 0; ar < 32; ar++) { melfilter_resps[ar] = dcv0[ar].re * dct_coeff[ar].re - dcv0[ar].im * dct_coeff[ar].im; } ia = (i4 - i3) + 1; loop_ub = i4 - i3; for (i4 = 0; i4 <= loop_ub; i4++) { tmp_data[i4] = i3 + i4; } loop_ub = i6 - i5; for (i3 = 0; i3 <= loop_ub; i3++) { c_tmp_data[i3] = i5 + i3; } b_samples_in_freq_size[0] = 1; b_samples_in_freq_size[1] = ia; loop_ub = ia - 1; for (i3 = 0; i3 <= loop_ub; i3++) { samples_in_freq_data[i3].re = samples_in_freq[tmp_data[i3] - 1].re * samples_in_freq[c_tmp_data[i3] - 1].re - samples_in_freq[tmp_data[i3] - 1] .im * -samples_in_freq[c_tmp_data[i3] - 1].im; samples_in_freq_data[i3].im = samples_in_freq[tmp_data[i3] - 1].re * -samples_in_freq[c_tmp_data[i3] - 1].im + samples_in_freq[tmp_data[i3] - 1] .im * samples_in_freq[c_tmp_data[i3] - 1].re; } dc0 = sum(samples_in_freq_data, b_samples_in_freq_size); c_log(&dc0); for (i3 = 0; i3 < 32; i3++) { b_melfilter_resps[i3] = melfilter_resps[i3] / 8.0; } mel[0] = dc0; for (i3 = 0; i3 < 12; i3++) { mel[i3 + 1].re = b_melfilter_resps[i3 + 1]; mel[i3 + 1].im = 0.0; } }
/* * function [lp]=gaussmixp(y,m,v,w) * y = cat(1, testSamples(1).mfcc{:}); * m = gmm.M; * v = gmm.V; * w = gmm.W; */ void gaussmixp(const real_T y[2004], const real_T m[108], const real_T v[108], const real_T w[9], real_T lp[167]) { real_T b[108]; real_T dv0[108]; real_T b_b[9]; real_T lvm[9]; int32_T ix; real_T mx[167]; real_T kk[1503]; real_T km[1503]; static real_T b_y[18036]; int32_T iy; real_T dv1[18036]; real_T x[1503]; int32_T i; int32_T ixstart; int32_T ixstop; real_T mtmp; int32_T b_ix; boolean_T exitg1; real_T ps[167]; /* GAUSSMIXP calculate probability densities from a Gaussian mixture model */ /* */ /* Inputs: n data values, k mixtures, p parameters, q data vector size */ /* */ /* Y(n,q) = input data */ /* M(k,p) = mixture means for x(p) */ /* V(k,p) or V(p,p,k) variances (diagonal or full) */ /* W(k,1) = weights */ /* A(q,p), B(q) = transformation: y=x*a'+b' (where y and x are row vectors) */ /* if A is omitted, it is assumed to be the first q rows of the */ /* identity matrix. B defaults to zero. */ /* Note that most commonly, q=p and A and B are omitted entirely. */ /* */ /* Outputs */ /* */ /* LP(n,1) = log probability of each data point */ /* RP(n,k) = relative probability of each mixture */ /* KH(n,1) = highest probability mixture */ /* KP(n,1) = relative probability of highest probability mixture */ /* Copyright (C) Mike Brookes 2000-2009 */ /* Version: $Id: gaussmixp.m,v 1.3 2009/04/08 07:51:21 dmb Exp $ */ /* */ /* VOICEBOX is a MATLAB toolbox for speech processing. */ /* Home page: http://www.ee.ic.ac.uk/hp/staff/dmb/voicebox/voicebox.html */ /* */ /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */ /* This program is free software; you can redistribute it and/or modify */ /* it under the terms of the GNU General Public License as published by */ /* the Free Software Foundation; either version 2 of the License, or */ /* (at your option) any later version. */ /* */ /* This program is distributed in the hope that it will be useful, */ /* but WITHOUT ANY WARRANTY; without even the implied warranty of */ /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */ /* GNU General Public License for more details. */ /* */ /* You can obtain a copy of the GNU General Public License from */ /* http://www.gnu.org/copyleft/gpl.html or by writing to */ /* Free Software Foundation, Inc.,675 Mass Ave, Cambridge, MA 02139, USA. */ /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */ /* 'gaussmixp:49' [n,q]=size(y); */ /* 'gaussmixp:50' [k,p]=size(m); */ /* 'gaussmixp:51' memsize=voicebox('memsize'); */ voicebox(); /* set memory size to use */ /* 'gaussmixp:53' lp=zeros(n,1); */ memset((void *)&lp[0], 0, 167U * sizeof(real_T)); /* 'gaussmixp:54' wk=ones(k,1); */ /* 'gaussmixp:56' vi=-0.5*v.^(-1); */ power(v, b); /* data-independent scale factor in exponent */ /* 'gaussmixp:57' lvm=log(w)-0.5*sum(log(v),2); */ memcpy((void *)&dv0[0], (void *)&v[0], 108U * sizeof(real_T)); c_log(dv0); sum(dv0, b_b); memcpy((void *)&lvm[0], (void *)&w[0], 9U * sizeof(real_T)); b_log(lvm); for (ix = 0; ix < 9; ix++) { lvm[ix] -= 0.5 * b_b[ix]; } /* log of external scale factor (excluding -0.5*q*log(2pi) term) */ /* 'gaussmixp:58' ii=1:n; */ /* 'gaussmixp:59' wnj=ones(1,n); */ /* 'gaussmixp:60' kk=repmat(ii,k,1); */ for (ix = 0; ix < 167; ix++) { mx[ix] = 1.0 + (real_T)ix; } repmat(mx, kk); /* 'gaussmixp:61' km=repmat(1:k,1,n); */ for (ix = 0; ix < 9; ix++) { b_b[ix] = 1.0 + (real_T)ix; } b_repmat(b_b, km); /* 'gaussmixp:62' py=reshape(sum((y(kk(:),:)-m(km(:),:)).^2.*vi(km(:),:),2),k,n)+lvm(:,wnj); */ for (ix = 0; ix < 12; ix++) { for (iy = 0; iy < 1503; iy++) { b_y[iy + 1503 * ix] = y[((int32_T)kk[iy] + 167 * ix) - 1] - m[((int32_T) km[iy] + 9 * ix) - 1]; } } b_power(b_y, dv1); for (ix = 0; ix < 12; ix++) { for (iy = 0; iy < 1503; iy++) { b_y[iy + 1503 * ix] = dv1[iy + 1503 * ix] * (-0.5 * b[((int32_T)km[iy] + 9 * ix) - 1]); } } b_sum(b_y, x); memcpy((void *)&kk[0], (void *)&x[0], 1503U * sizeof(real_T)); for (ix = 0; ix < 167; ix++) { for (iy = 0; iy < 9; iy++) { km[iy + 9 * ix] = kk[iy + 9 * ix] + lvm[iy]; } } /* 'gaussmixp:63' mx=max(py,[],1); */ ix = -8; iy = -1; for (i = 0; i < 167; i++) { ix += 9; ixstart = ix; ixstop = ix + 8; mtmp = km[ix - 1]; if (rtIsNaN(km[ix - 1])) { b_ix = ix; exitg1 = 0U; while ((exitg1 == 0U) && (b_ix + 1 <= ixstop)) { ixstart = b_ix + 1; if (!rtIsNaN(km[b_ix])) { mtmp = km[b_ix]; exitg1 = 1U; } else { b_ix++; } } } if (ixstart < ixstop) { while (ixstart + 1 <= ixstop) { if (km[ixstart] > mtmp) { mtmp = km[ixstart]; } ixstart++; } } iy++; mx[iy] = mtmp; } /* find normalizing factor for each data point to prevent underflow when using exp() */ /* 'gaussmixp:64' px=exp(py-mx(wk,:)); */ for (ix = 0; ix < 167; ix++) { for (iy = 0; iy < 9; iy++) { kk[iy + 9 * ix] = km[iy + 9 * ix] - mx[ix]; } } b_exp(kk); /* find normalized probability of each mixture for each datapoint */ /* 'gaussmixp:65' ps=sum(px,1); */ c_sum(kk, ps); /* total normalized likelihood of each data point */ /* 'gaussmixp:66' lp(ii)=log(ps)+mx; */ d_log(ps); for (ix = 0; ix < 167; ix++) { /* 'gaussmixp:67' lp=lp-0.5*q*log(2*pi); */ lp[ix] = (ps[ix] + mx[ix]) - 11.027262398456072; } }