// SPDX-License-Identifier: GPL-2.0-or-later /* * Soundfont generic routines. * It is intended that these should be used by any driver that is willing * to accept soundfont patches. * * Copyright (C) 1999 Steve Ratcliffe * Copyright (c) 1999-2000 Takashi Iwai <tiwai@suse.de>
*/ /* * Deal with reading in of a soundfont. Code follows the OSS way * of doing things so that the old sfxload utility can be used. * Everything may change when there is an alsa way of doing things.
*/ #include <linux/uaccess.h> #include <linux/slab.h> #include <linux/export.h> #include <sound/core.h> #include <sound/soundfont.h> #include <sound/seq_oss_legacy.h>
/* Prototypes for static functions */
staticint open_patch(struct snd_sf_list *sflist, constchar __user *data, int count, int client); staticstruct snd_soundfont *newsf(struct snd_sf_list *sflist, int type, char *name); staticint is_identical_font(struct snd_soundfont *sf, int type, unsignedchar *name); staticint close_patch(struct snd_sf_list *sflist); staticint probe_data(struct snd_sf_list *sflist, int sample_id); staticvoid set_zone_counter(struct snd_sf_list *sflist, struct snd_soundfont *sf, struct snd_sf_zone *zp); staticstruct snd_sf_zone *sf_zone_new(struct snd_sf_list *sflist, struct snd_soundfont *sf); staticvoid set_sample_counter(struct snd_sf_list *sflist, struct snd_soundfont *sf, struct snd_sf_sample *sp); staticstruct snd_sf_sample *sf_sample_new(struct snd_sf_list *sflist, struct snd_soundfont *sf); staticvoid sf_sample_delete(struct snd_sf_list *sflist, struct snd_soundfont *sf, struct snd_sf_sample *sp); staticint load_map(struct snd_sf_list *sflist, constvoid __user *data, int count); staticint load_info(struct snd_card *card, struct snd_sf_list *sflist, constvoid __user *data, long count); staticint remove_info(struct snd_sf_list *sflist, struct snd_soundfont *sf, int bank, int instr); staticvoid init_voice_info(struct soundfont_voice_info *avp); staticvoid init_voice_parm(struct soundfont_voice_parm *pp); staticstruct snd_sf_sample *set_sample(struct snd_soundfont *sf, struct soundfont_voice_info *avp); staticstruct snd_sf_sample *find_sample(struct snd_soundfont *sf, int sample_id); staticint load_data(struct snd_sf_list *sflist, constvoid __user *data, long count); staticvoid rebuild_presets(struct snd_sf_list *sflist); staticvoid add_preset(struct snd_sf_list *sflist, struct snd_sf_zone *cur); staticvoid delete_preset(struct snd_sf_list *sflist, struct snd_sf_zone *zp); staticstruct snd_sf_zone *search_first_zone(struct snd_sf_list *sflist, int bank, int preset, int key); 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); staticint get_index(int bank, int instr, int key); staticvoid snd_sf_init(struct snd_sf_list *sflist); staticvoid snd_sf_clear(struct snd_sf_list *sflist);
/* * close the patch if the patch was opened by this client.
*/ 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); return 0;
}
/* * Deal with a soundfont patch. Any driver could use these routines * although it was designed for the AWE64. * * The sample_write and callargs parameters allow a callback into * the actual driver to write sample data to the board or whatever * it wants to do with it.
*/ 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;
/* * Allocate a new soundfont structure.
*/ staticstruct snd_soundfont *
newsf(struct snd_sf_list *sflist, int type, char *name)
{ struct snd_soundfont *sf;
/* 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)) return 0;
} return -EINVAL;
}
/* * allocate a new sample list record
*/ staticstruct snd_sf_sample *
sf_sample_new(struct snd_sf_list *sflist, struct snd_soundfont *sf)
{ struct snd_sf_sample *sp;
sp = kzalloc(sizeof(*sp), GFP_KERNEL); if (!sp) return NULL;
sp->next = sf->samples;
sf->samples = sp;
set_sample_counter(sflist, sf, sp); return sp;
}
/* * delete sample list -- this is an exceptional job. * only the last allocated sample can be deleted.
*/ staticvoid
sf_sample_delete(struct snd_sf_list *sflist, struct snd_soundfont *sf, struct snd_sf_sample *sp)
{ /* only last sample is accepted */ if (sp == sf->samples) {
sf->samples = sp->next;
kfree(sp);
}
}
/* remove the present instrument layers */ staticint
remove_info(struct snd_sf_list *sflist, struct snd_soundfont *sf, int bank, int instr)
{ struct snd_sf_zone *prev, *next, *p; int removed = 0;
prev = NULL; for (p = sf->zones; p; p = next) {
next = p->next; if (! p->mapped &&
p->bank == bank && p->instr == instr) { /* remove this layer */ if (prev)
prev->next = next; else
sf->zones = next;
removed++;
kfree(p);
} else
prev = p;
} if (removed)
rebuild_presets(sflist); return removed;
}
/* * Read an info record from the user buffer and save it on the current * open soundfont.
*/ staticint
load_info(struct snd_card *card, struct snd_sf_list *sflist, constvoid __user *data, long count)
{ struct snd_soundfont *sf; struct snd_sf_zone *zone; struct soundfont_voice_rec_hdr hdr; int i;
/* patch must be opened */
sf = sflist->currsf; if (!sf) return -EINVAL;
if (is_special_type(sf->type)) return -EINVAL;
if (count < (long)sizeof(hdr)) {
dev_err(card->dev, "Soundfont error: invalid patch zone length\n"); return -EINVAL;
} if (copy_from_user((char*)&hdr, data, sizeof(hdr))) return -EFAULT;
data += sizeof(hdr);
count -= sizeof(hdr);
if (hdr.nvoices <= 0 || hdr.nvoices >= 100) {
dev_err(card->dev, "Soundfont error: Illegal voice number %d\n",
hdr.nvoices); return -EINVAL;
}
if (count < (long)sizeof(struct soundfont_voice_info) * hdr.nvoices) {
dev_err(card->dev, "Soundfont Error: patch length(%ld) is smaller than nvoices(%d)\n",
count, hdr.nvoices); return -EINVAL;
}
switch (hdr.write_mode) { case SNDRV_SFNT_WR_EXCLUSIVE: /* exclusive mode - if the instrument already exists,
return error */ for (zone = sf->zones; zone; zone = zone->next) { if (!zone->mapped &&
zone->bank == hdr.bank &&
zone->instr == hdr.instr) return -EINVAL;
} break; case SNDRV_SFNT_WR_REPLACE: /* replace mode - remove the instrument if it already exists */
remove_info(sflist, sf, hdr.bank, hdr.instr); break;
}
for (i = 0; i < hdr.nvoices; i++) { struct snd_sf_zone tmpzone;
sample = find_sample(sf, avp->sample); if (sample == NULL) return NULL;
/* add in the actual sample offsets: * The voice_info addresses define only the relative offset * from sample pointers. Here we calculate the actual DRAM * offset from sample pointers.
*/
avp->start += sample->v.start;
avp->end += sample->v.end;
avp->loopstart += sample->v.loopstart;
avp->loopend += sample->v.loopend;
/* 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); return 0;
}
/* * Load sample information, this can include data to be loaded onto * the soundcard. It can also just be a pointer into soundcard ROM. * If there is data it will be written to the soundcard via the callback * routine.
*/ staticint
load_data(struct snd_sf_list *sflist, constvoid __user *data, long count)
{ struct snd_soundfont *sf; struct soundfont_sample_info sample_info; struct snd_sf_sample *sp;
/* patch must be opened */
sf = sflist->currsf; if (!sf) return -EINVAL;
if (is_special_type(sf->type)) return -EINVAL;
if (count < (long)sizeof(sample_info)) { return -EINVAL;
} if (copy_from_user(&sample_info, data, sizeof(sample_info))) return -EFAULT;
data += sizeof(sample_info);
count -= sizeof(sample_info);
/* Check for dup */ if (find_sample(sf, sample_info.sample)) { /* if shared sample, skip this data */ if (sf->type & SNDRV_SFNT_PAT_SHARED) return 0; 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;
/* * If there is wave data then load it.
*/ if (sp->v.size > 0) { int rc;
rc = sflist->callback.sample_new
(sflist->callback.private_data, sp, sflist->memhdr,
data, count); if (rc < 0) {
sf_sample_delete(sflist, sf, sp); return rc;
}
sflist->mem_used += sp->v.truesize;
}
/* convert from linear to log value * * conversion: value = log2(amount / base) * ratio * * argument: * amount = linear value (unsigned, 32bit max) * offset = base offset (:= log2(base) * 0x10000) * ratio = division ratio *
*/ int
snd_sf_linear_to_log(unsignedint amount, int offset, int ratio)
{ int v; int s, low, bit;
if (amount < 2) return 0; 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 */
/* convert Hz to AWE32 rate offset: * sample pitch offset for the specified sample rate * rate=44100 is no offset, each 4096 is 1 octave (twice). * eg, when rate is 22050, this offset becomes -4096. * * conversion: offset = log2(Hz / 44100) * 4096
*/ staticint
calc_rate_offset(int hz)
{ return snd_sf_linear_to_log(hz, OFFSET_SAMPLERATE, SAMPLERATE_RATIO);
}
/* 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);
/* * Rebuild the preset table. This is like a hash table in that it allows * quick access to the zone information. For each preset there are zone * structures linked by next_instr and by next_zone. Former is the whole * link for this preset, and latter is the link for zone (i.e. instrument/ * bank/key combination).
*/ staticvoid
rebuild_presets(struct snd_sf_list *sflist)
{ struct snd_soundfont *sf; struct snd_sf_zone *cur;
/* 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;
}
add_preset(sflist, cur);
}
}
}
/* * add the given zone to preset table
*/ staticvoid
add_preset(struct snd_sf_list *sflist, struct snd_sf_zone *cur)
{ struct snd_sf_zone *zone; int index;
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;
}
/* * delete the given zones from preset_table
*/ staticvoid
delete_preset(struct snd_sf_list *sflist, struct snd_sf_zone *zp)
{ int index; struct snd_sf_zone *p;
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;
}
}
}
/* * Search matching zones from preset table. * The note can be rewritten by preset mapping (alias). * The found zones are stored on 'table' array. max_layers defines * the maximum number of elements in this array. * This function returns the number of found zones. 0 if not found.
*/ 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, * so we check the lock. if it's busy, just returns 0 to * tell the caller the busy state
*/
spin_lock_irqsave(&sflist->lock, flags); if (sflist->presets_locked) {
spin_unlock_irqrestore(&sflist->lock, flags); return 0;
}
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;
}
/* * search the first matching zone
*/ 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;
}
/* * search matching zones from sflist. can be called recursively.
*/ 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 */ return 0; 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: * drums are mapped from 128 to 255 according to its note key. * other instruments are mapped from 0 to 127. * if the index is out of range, 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;
}
/* * Free everything allocated off the sflist structure.
*/ void
snd_sf_free(struct snd_sf_list *sflist)
{ if (sflist == NULL) return;
lock_preset(sflist); if (sflist->callback.sample_reset)
sflist->callback.sample_reset(sflist->callback.private_data);
snd_sf_clear(sflist);
unlock_preset(sflist);
kfree(sflist);
}
/* * Remove all samples * The soundcard should be silent before calling this function.
*/ int
snd_soundfont_remove_samples(struct snd_sf_list *sflist)
{
lock_preset(sflist); if (sflist->callback.sample_reset)
sflist->callback.sample_reset(sflist->callback.private_data);
snd_sf_clear(sflist);
unlock_preset(sflist);
return 0;
}
/* * Remove unlocked samples. * The soundcard should be silent before calling this function.
*/ int
snd_soundfont_remove_unlocked(struct snd_sf_list *sflist)
{ struct snd_soundfont *sf; struct snd_sf_zone *zp, *nextzp; struct snd_sf_sample *sp, *nextsp;
lock_preset(sflist);
if (sflist->callback.sample_reset)
sflist->callback.sample_reset(sflist->callback.private_data);
/* to be sure */
memset(sflist->presets, 0, sizeof(sflist->presets));
for (sf = sflist->fonts; sf; sf = sf->next) { for (zp = sf->zones; zp; zp = nextzp) { if (zp->counter < sflist->zone_locked) break;
nextzp = zp->next;
sf->zones = nextzp;
kfree(zp);
}
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.