2 * Patch transfer callback for Emu10k1
4 * Copyright (C) 2000 Takashi iwai <tiwai@suse.de>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 * All the code for loading in a patch. There is very little that is
22 * chip specific here. Just the actual writing to the board.
25 #include "emu10k1_synth_local.h"
29 #define BLANK_LOOP_START 4
30 #define BLANK_LOOP_END 8
31 #define BLANK_LOOP_SIZE 12
32 #define BLANK_HEAD_SIZE 32
35 * allocate a sample block and copy data from userspace
38 snd_emu10k1_sample_new(snd_emux_t
*rec
, snd_sf_sample_t
*sp
,
39 snd_util_memhdr_t
*hdr
, const void __user
*data
, long count
)
42 int truesize
, size
, loopsize
, blocksize
;
43 int loopend
, sampleend
;
44 unsigned int start_addr
;
48 snd_assert(sp
!= NULL
, return -EINVAL
);
49 snd_assert(hdr
!= NULL
, return -EINVAL
);
51 if (sp
->v
.size
== 0) {
52 snd_printd("emu: rom font for sample %d\n", sp
->v
.sample
);
56 /* recalculate address offset */
57 sp
->v
.end
-= sp
->v
.start
;
58 sp
->v
.loopstart
-= sp
->v
.start
;
59 sp
->v
.loopend
-= sp
->v
.start
;
62 /* some samples have invalid data. the addresses are corrected in voice info */
63 sampleend
= sp
->v
.end
;
64 if (sampleend
> sp
->v
.size
)
65 sampleend
= sp
->v
.size
;
66 loopend
= sp
->v
.loopend
;
67 if (loopend
> sampleend
)
70 /* be sure loop points start < end */
71 if (sp
->v
.loopstart
>= sp
->v
.loopend
) {
72 int tmp
= sp
->v
.loopstart
;
73 sp
->v
.loopstart
= sp
->v
.loopend
;
77 /* compute true data size to be loaded */
78 truesize
= sp
->v
.size
+ BLANK_HEAD_SIZE
;
80 #if 0 /* not supported */
81 if (sp
->v
.mode_flags
& (SNDRV_SFNT_SAMPLE_BIDIR_LOOP
|SNDRV_SFNT_SAMPLE_REVERSE_LOOP
))
82 loopsize
= sp
->v
.loopend
- sp
->v
.loopstart
;
85 if (sp
->v
.mode_flags
& SNDRV_SFNT_SAMPLE_NO_BLANK
)
86 truesize
+= BLANK_LOOP_SIZE
;
88 /* try to allocate a memory block */
90 if (! (sp
->v
.mode_flags
& SNDRV_SFNT_SAMPLE_8BITS
))
92 sp
->block
= snd_emu10k1_synth_alloc(emu
, blocksize
);
93 if (sp
->block
== NULL
) {
94 snd_printd("emu10k1: synth malloc failed (size=%d)\n", blocksize
);
95 /* not ENOMEM (for compatibility with OSS) */
98 /* set the total size */
99 sp
->v
.truesize
= blocksize
;
101 /* write blank samples at head */
103 size
= BLANK_HEAD_SIZE
;
104 if (! (sp
->v
.mode_flags
& SNDRV_SFNT_SAMPLE_8BITS
))
106 snd_assert(offset
+ size
<= blocksize
, return -EINVAL
);
107 snd_emu10k1_synth_bzero(emu
, sp
->block
, offset
, size
);
110 /* copy start->loopend */
112 if (! (sp
->v
.mode_flags
& SNDRV_SFNT_SAMPLE_8BITS
))
114 snd_assert(offset
+ size
<= blocksize
, return -EINVAL
);
115 if (snd_emu10k1_synth_copy_from_user(emu
, sp
->block
, offset
, data
, size
)) {
116 snd_emu10k1_synth_free(emu
, sp
->block
);
123 #if 0 /* not suppported yet */
124 /* handle reverse (or bidirectional) loop */
125 if (sp
->v
.mode_flags
& (SNDRV_SFNT_SAMPLE_BIDIR_LOOP
|SNDRV_SFNT_SAMPLE_REVERSE_LOOP
)) {
126 /* copy loop in reverse */
127 if (! (sp
->v
.mode_flags
& SNDRV_SFNT_SAMPLE_8BITS
)) {
129 unsigned short *wblock
= (unsigned short*)block
;
130 woffset
= offset
/ 2;
131 snd_assert(offset
+ loopsize
*2 <= blocksize
, return -EINVAL
);
132 for (i
= 0; i
< loopsize
; i
++)
133 wblock
[woffset
+ i
] = wblock
[woffset
- i
-1];
134 offset
+= loopsize
* 2;
136 snd_assert(offset
+ loopsize
<= blocksize
, return -EINVAL
);
137 for (i
= 0; i
< loopsize
; i
++)
138 block
[offset
+ i
] = block
[offset
- i
-1];
142 /* modify loop pointers */
143 if (sp
->v
.mode_flags
& SNDRV_SFNT_SAMPLE_BIDIR_LOOP
) {
144 sp
->v
.loopend
+= loopsize
;
146 sp
->v
.loopstart
+= loopsize
;
147 sp
->v
.loopend
+= loopsize
;
149 /* add sample pointer */
150 sp
->v
.end
+= loopsize
;
154 /* loopend -> sample end */
155 size
= sp
->v
.size
- loopend
;
156 snd_assert(size
>= 0, return -EINVAL
);
157 if (! (sp
->v
.mode_flags
& SNDRV_SFNT_SAMPLE_8BITS
))
159 if (snd_emu10k1_synth_copy_from_user(emu
, sp
->block
, offset
, data
, size
)) {
160 snd_emu10k1_synth_free(emu
, sp
->block
);
166 /* clear rest of samples (if any) */
167 if (offset
< blocksize
)
168 snd_emu10k1_synth_bzero(emu
, sp
->block
, offset
, blocksize
- offset
);
170 if (sp
->v
.mode_flags
& SNDRV_SFNT_SAMPLE_NO_BLANK
) {
171 /* if no blank loop is attached in the sample, add it */
172 if (sp
->v
.mode_flags
& SNDRV_SFNT_SAMPLE_SINGLESHOT
) {
173 sp
->v
.loopstart
= sp
->v
.end
+ BLANK_LOOP_START
;
174 sp
->v
.loopend
= sp
->v
.end
+ BLANK_LOOP_END
;
178 #if 0 /* not supported yet */
179 if (sp
->v
.mode_flags
& SNDRV_SFNT_SAMPLE_UNSIGNED
) {
180 /* unsigned -> signed */
181 if (! (sp
->v
.mode_flags
& SNDRV_SFNT_SAMPLE_8BITS
)) {
182 unsigned short *wblock
= (unsigned short*)block
;
183 for (i
= 0; i
< truesize
; i
++)
186 for (i
= 0; i
< truesize
; i
++)
192 /* recalculate offset */
193 start_addr
= BLANK_HEAD_SIZE
* 2;
194 if (! (sp
->v
.mode_flags
& SNDRV_SFNT_SAMPLE_8BITS
))
196 sp
->v
.start
+= start_addr
;
197 sp
->v
.end
+= start_addr
;
198 sp
->v
.loopstart
+= start_addr
;
199 sp
->v
.loopend
+= start_addr
;
205 * free a sample block
208 snd_emu10k1_sample_free(snd_emux_t
*rec
, snd_sf_sample_t
*sp
,
209 snd_util_memhdr_t
*hdr
)
214 snd_assert(sp
!= NULL
, return -EINVAL
);
215 snd_assert(hdr
!= NULL
, return -EINVAL
);
218 snd_emu10k1_synth_free(emu
, sp
->block
);