/* *closethepatchifthepatchwasopenedbythisclient.
*/ int
snd_soundfont_close_check(struct snd_sf_list *sflist, int client)
{ unsignedlong flags;
spin_lock_irqsave(&sflist->lock, flags); if (sflist->open_client == client) {
spin_unlock_irqrestore(&sflist->lock, flags); return close_patch(sflist);
}
spin_unlock_irqrestore(&sflist->lock, flags); return0;
}
/* *Dealwithasoundfontpatch.Anydrivercouldusetheseroutines *althoughitwasdesignedfortheAWE64. * *Thesample_writeandcallargsparametersallowacallbackinto *theactualdrivertowritesampledatatotheboardorwhatever *itwantstodowithit.
*/ int
snd_soundfont_load(struct snd_card *card, struct snd_sf_list *sflist, constvoid __user *data, long count, int client)
{ struct soundfont_patch_info patch; unsignedlong flags; int rc;
if (count < (long)sizeof(patch)) {
dev_err(card->dev, "patch record too small %ld\n", count); return -EINVAL;
} if (copy_from_user(&patch, data, sizeof(patch))) return -EFAULT;
count -= sizeof(patch);
data += sizeof(patch);
if (patch.key != SNDRV_OSS_SOUNDFONT_PATCH) {
dev_err(card->dev, "The wrong kind of patch %x\n", patch.key); return -EINVAL;
} if (count < patch.len) {
dev_err(card->dev, "Patch too short %ld, need %d\n",
count, patch.len); return -EINVAL;
} if (patch.len < 0) {
dev_err(card->dev, "poor length %d\n", patch.len); return -EINVAL;
}
if (patch.type == SNDRV_SFNT_OPEN_PATCH) { /* grab sflist to open */
lock_preset(sflist);
rc = open_patch(sflist, data, count, client);
unlock_preset(sflist); return rc;
}
/* check if other client already opened patch */
spin_lock_irqsave(&sflist->lock, flags); if (sflist->open_client != client) {
spin_unlock_irqrestore(&sflist->lock, flags); return -EBUSY;
}
spin_unlock_irqrestore(&sflist->lock, flags);
lock_preset(sflist);
rc = -EINVAL; switch (patch.type) { case SNDRV_SFNT_LOAD_INFO:
rc = load_info(card, sflist, data, count); break; case SNDRV_SFNT_LOAD_DATA:
rc = load_data(sflist, data, count); break; case SNDRV_SFNT_CLOSE_PATCH:
rc = close_patch(sflist); break; case SNDRV_SFNT_REPLACE_DATA: /*rc = replace_data(&patch, data, count);*/ break; case SNDRV_SFNT_MAP_PRESET:
rc = load_map(sflist, data, count); break; case SNDRV_SFNT_PROBE_DATA:
rc = probe_data(sflist, patch.optarg); break; case SNDRV_SFNT_REMOVE_INFO: /* patch must be opened */ if (!sflist->currsf) {
dev_err(card->dev, "soundfont: remove_info: patch not opened\n");
rc = -EINVAL;
} else { int bank, instr;
bank = ((unsignedshort)patch.optarg >> 8) & 0xff;
instr = (unsignedshort)patch.optarg & 0xff; if (! remove_info(sflist, sflist->currsf, bank, instr))
rc = -EINVAL; else
rc = 0;
} break;
}
unlock_preset(sflist);
return rc;
}
/* check if specified type is special font (GUS or preset-alias) */ staticinlineint
is_special_type(int type)
{
type &= 0x0f; return (type == SNDRV_SFNT_PAT_TYPE_GUS ||
type == SNDRV_SFNT_PAT_TYPE_MAP);
}
/* open patch; create sf list */ staticint
open_patch(struct snd_sf_list *sflist, constchar __user *data, int count, int client)
{ struct soundfont_open_parm parm; struct snd_soundfont *sf; unsignedlong flags;
/* check the shared fonts */ if (type & SNDRV_SFNT_PAT_SHARED) { for (sf = sflist->fonts; sf; sf = sf->next) { if (is_identical_font(sf, type, name)) { return sf;
}
}
}
/* not found -- create a new one */
sf = kzalloc(sizeof(*sf), GFP_KERNEL); if (sf == NULL) return NULL;
sf->id = sflist->fonts_size;
sflist->fonts_size++;
/* prepend this record */
sf->next = sflist->fonts;
sflist->fonts = sf;
/* probe sample in the current list -- nothing to be loaded */ staticint
probe_data(struct snd_sf_list *sflist, int sample_id)
{ /* patch must be opened */ if (sflist->currsf) { /* search the specified sample by optarg */ if (find_sample(sflist->currsf, sample_id)) return0;
} return -EINVAL;
}
/* find the sample pointer with the given id in the soundfont */ staticstruct snd_sf_sample *
find_sample(struct snd_soundfont *sf, int sample_id)
{ struct snd_sf_sample *p;
if (sf == NULL) return NULL;
for (p = sf->samples; p; p = p->next) { if (p->v.sample == sample_id) return p;
} return NULL;
}
staticint
validate_sample_info(struct soundfont_sample_info *si)
{ if (si->end < 0 || si->end > si->size) return -EINVAL; if (si->loopstart < 0 || si->loopstart > si->end) return -EINVAL; if (si->loopend < 0 || si->loopend > si->end) return -EINVAL; /* be sure loop points start < end */ if (si->loopstart > si->loopend)
swap(si->loopstart, si->loopend); return0;
}
/* Check for dup */ if (find_sample(sf, sample_info.sample)) { /* if shared sample, skip this data */ if (sf->type & SNDRV_SFNT_PAT_SHARED) return0; return -EINVAL;
}
if (sample_info.size > 0) { if (sample_info.start < 0) return -EINVAL;
// Here we "rebase out" the start address, because the // real start is the start of the provided sample data.
sample_info.end -= sample_info.start;
sample_info.loopstart -= sample_info.start;
sample_info.loopend -= sample_info.start;
sample_info.start = 0;
if (validate_sample_info(&sample_info) < 0) return -EINVAL;
}
/* Allocate a new sample structure */
sp = sf_sample_new(sflist, sf); if (!sp) return -ENOMEM;
/* convert from linear to log value * *conversion:value=log2(amount/base)*ratio * *argument: *amount=linearvalue(unsigned,32bitmax) *offset=baseoffset(:=log2(base)*0x10000) *ratio=divisionratio *
*/ int
snd_sf_linear_to_log(unsignedint amount, int offset, int ratio)
{ int v; int s, low, bit;
if (amount < 2) return0; for (bit = 0; ! (amount & 0x80000000L); bit++)
amount <<= 1;
s = (amount >> 24) & 0x7f;
low = (amount >> 16) & 0xff; /* linear approximation by lower 8 bit */
v = (log_tbl[s + 1] * low + log_tbl[s] * (0x100 - low)) >> 8;
v -= offset;
v = (v * ratio) >> 16;
v += (24 - bit) * ratio; return v;
}
EXPORT_SYMBOL(snd_sf_linear_to_log);
#define OFFSET_MSEC 653117/* base = 1000 */ #define OFFSET_ABSCENT 851781/* base = 8176 */ #define OFFSET_SAMPLERATE 1011119/* base = 44100 */
/* calculate GUS envelope time */ staticint
calc_gus_envelope_time(int rate, int start, int end)
{ int r, p, t;
r = (3 - ((rate >> 6) & 3)) * 3;
p = rate & 0x3f; if (!p)
p = 1;
t = end - start; if (t < 0) t = -t; if (13 > r)
t = t << (13 - r); else
t = t >> (r - 13); return (t * 10) / (p * 441);
}
/* convert envelope time parameter to soundfont parameters */
/* delay time = 0x8000 - msec/92 */ int
snd_sf_calc_parm_hold(int msec)
{ int val = (0x7f * 92 - msec) / 92; if (val < 1) val = 1; if (val >= 126) val = 126; return val;
}
/* search an index for specified time from given time table */ staticint
calc_parm_search(int msec, constshort *table)
{ int left = 1, right = 127, mid; while (left < right) {
mid = (left + right) / 2; if (msec < (int)table[mid])
left = mid + 1; else
right = mid;
} return left;
}
/* attack time: search from time table */ int
snd_sf_calc_parm_attack(int msec)
{ return calc_parm_search(msec, attack_time_tbl);
}
/* decay/release time: search from time table */ int
snd_sf_calc_parm_decay(int msec)
{ return calc_parm_search(msec, decay_time_tbl);
}
if (count < (long)sizeof(patch)) {
dev_err(card->dev, "patch record too small %ld\n", count); return -EINVAL;
} if (copy_from_user(&patch, data, sizeof(patch))) return -EFAULT;
count -= sizeof(patch);
data += sizeof(patch);
/* append to the tail of the list */ /*zone->bank = ctrls[AWE_MD_GUS_BANK];*/
zone->bank = 0;
zone->instr = patch.instr_no;
zone->mapped = 0;
zone->v.sf_id = sf->id;
zone->sample = set_sample(sf, &zone->v);
/* rebuild preset now */
add_preset(sflist, zone);
/* search all fonts and insert each font */ for (sf = sflist->fonts; sf; sf = sf->next) { for (cur = sf->zones; cur; cur = cur->next) { if (! cur->mapped && cur->sample == NULL) { /* try again to search the corresponding sample */
cur->sample = set_sample(sf, &cur->v); if (cur->sample == NULL) continue;
}
zone = search_first_zone(sflist, cur->bank, cur->instr, cur->v.low); if (zone && zone->v.sf_id != cur->v.sf_id) { /* different instrument was already defined */ struct snd_sf_zone *p; /* compare the allocated time */ for (p = zone; p; p = p->next_zone) { if (p->counter > cur->counter) /* the current is older.. skipped */ return;
} /* remove old zones */
delete_preset(sflist, zone);
zone = NULL; /* do not forget to clear this! */
}
/* prepend this zone */
index = get_index(cur->bank, cur->instr, cur->v.low); if (index < 0) return;
cur->next_zone = zone; /* zone link */
cur->next_instr = sflist->presets[index]; /* preset table link */
sflist->presets[index] = cur;
}
index = get_index(zp->bank, zp->instr, zp->v.low); if (index < 0) return; for (p = sflist->presets[index]; p; p = p->next_instr) { while (p->next_instr == zp) {
p->next_instr = zp->next_instr;
zp = zp->next_zone; if (zp == NULL) return;
}
}
}
/* *Searchmatchingzonesfrompresettable. *Thenotecanberewrittenbypresetmapping(alias). *Thefoundzonesarestoredon'table'array.max_layersdefines *themaximumnumberofelementsinthisarray. *Thisfunctionreturnsthenumberoffoundzones.0ifnotfound.
*/ int
snd_soundfont_search_zone(struct snd_sf_list *sflist, int *notep, int vel, int preset, int bank, int def_preset, int def_bank, struct snd_sf_zone **table, int max_layers)
{ int nvoices; unsignedlong flags;
/* this function is supposed to be called atomically, *sowecheckthelock.ifit'sbusy,justreturns0to *tellthecallerthebusystate
*/
spin_lock_irqsave(&sflist->lock, flags); if (sflist->presets_locked) {
spin_unlock_irqrestore(&sflist->lock, flags); return0;
}
nvoices = search_zones(sflist, notep, vel, preset, bank,
table, max_layers, 0); if (! nvoices) { if (preset != def_preset || bank != def_bank)
nvoices = search_zones(sflist, notep, vel,
def_preset, def_bank,
table, max_layers, 0);
}
spin_unlock_irqrestore(&sflist->lock, flags); return nvoices;
}
/* *searchthefirstmatchingzone
*/ staticstruct snd_sf_zone *
search_first_zone(struct snd_sf_list *sflist, int bank, int preset, int key)
{ int index; struct snd_sf_zone *zp;
index = get_index(bank, preset, key); if (index < 0) return NULL; for (zp = sflist->presets[index]; zp; zp = zp->next_instr) { if (zp->instr == preset && zp->bank == bank) return zp;
} return NULL;
}
/* *searchmatchingzonesfromsflist.canbecalledrecursively.
*/ staticint
search_zones(struct snd_sf_list *sflist, int *notep, int vel, int preset, int bank, struct snd_sf_zone **table, int max_layers, int level)
{ struct snd_sf_zone *zp; int nvoices;
zp = search_first_zone(sflist, bank, preset, *notep);
nvoices = 0; for (; zp; zp = zp->next_zone) { if (*notep >= zp->v.low && *notep <= zp->v.high &&
vel >= zp->v.vellow && vel <= zp->v.velhigh) { if (zp->mapped) { /* search preset mapping (aliasing) */ int key = zp->v.fixkey;
preset = zp->v.start;
bank = zp->v.end;
if (level > 5) /* too deep alias level */ return0; if (key < 0)
key = *notep;
nvoices = search_zones(sflist, &key, vel,
preset, bank, table,
max_layers, level + 1); if (nvoices > 0)
*notep = key; break;
}
table[nvoices++] = zp; if (nvoices >= max_layers) break;
}
}
return nvoices;
}
/* calculate the index of preset table: *drumsaremappedfrom128to255accordingtoitsnotekey. *otherinstrumentsaremappedfrom0to127. *iftheindexisoutofrange,return-1.
*/ staticint
get_index(int bank, int instr, int key)
{ int index; if (SF_IS_DRUM_BANK(bank))
index = key + SF_MAX_INSTRUMENTS; else
index = instr;
index = index % SF_MAX_PRESETS; if (index < 0) return -1; return index;
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.