PS2SDK
PS2 Homebrew Libraries
Loading...
Searching...
No Matches
ut_key.c
1/*
2# _____ ___ ____ ___ ____
3# ____| | ____| | | |____|
4# | ___| |____ ___| ____| | \ PS2DEV Open Source Project.
5#-----------------------------------------------------------------------
6# Copyright ps2dev - http://www.ps2dev.org
7# Licenced under Academic Free License version 2.0
8# Review ps2sdk README & LICENSE files for further details.
9*/
10
11#include "libsnd2_internal.h"
12
13s16 SsUtKeyOn(s16 vab_id, s16 prog, s16 tone, s16 note, s16 fine, s16 voll, s16 volr)
14{
15 const ProgAtr *pProg;
16 const VagAtr *pVag;
17 int voice;
18 libsnd2_spu_voice_t *voice_struct;
19
20 if ( _snd_ev_flag == 1 )
21 return -1;
22 _snd_ev_flag = 1;
23 if ( _SsVmVSetUp(vab_id, prog) )
24 {
25 _snd_ev_flag = 0;
26 return -1;
27 }
28 _svm_cur.m_seq_sep_no = 33;
29 _svm_cur.m_note = note;
30 _svm_cur.m_fine = fine;
31 _svm_cur.m_tone = tone;
32 if ( voll == volr )
33 {
34 _svm_cur.m_unk05 = 64;
35 _svm_cur.m_voll = voll;
36 }
37 else
38 {
39 int volr_lshift_6;
40
41 volr_lshift_6 = volr << 6;
42 if ( volr >= voll )
43 {
44 int voll_lshift_6;
45
46 voll_lshift_6 = voll << 6;
47 if ( !volr )
48 __builtin_trap();
49 if ( volr == -1 && (unsigned int)voll_lshift_6 == 0x80000000 )
50 __builtin_trap();
51 _svm_cur.m_voll = volr;
52 _svm_cur.m_unk05 = 127 - voll_lshift_6 / volr;
53 }
54 else
55 {
56 if ( !voll )
57 __builtin_trap();
58 if ( voll == -1 && (unsigned int)volr_lshift_6 == 0x80000000 )
59 __builtin_trap();
60 _svm_cur.m_unk05 = volr_lshift_6 / voll;
61 _svm_cur.m_voll = voll;
62 }
63 }
64 pProg = &_svm_pg[prog];
65 _svm_cur.m_mvol = pProg->mvol;
66 _svm_cur.m_mpan = pProg->mpan;
67 _svm_cur.m_sep_sep_no_tonecount = pProg->tones;
68 pVag = &_svm_tn[16 * _svm_cur.m_fake_program + _svm_cur.m_tone];
69 _svm_cur.m_prior = pVag->prior;
70 _svm_cur.m_vag_idx2 = pVag->vag;
71 _svm_cur.m_vol = pVag->vol;
72 _svm_cur.m_pan = pVag->pan;
73 _svm_cur.m_centre = pVag->center;
74 _svm_cur.m_shift = pVag->shift;
75 _svm_cur.m_mode = pVag->mode;
76 if ( !_svm_cur.m_vag_idx2 || (voice = (u8)_SsVmAlloc(), voice == _SsVmMaxVoice) )
77 {
78 _snd_ev_flag = 0;
79 return -1;
80 }
81 voice = (u8)_SsVmAlloc();
82 if ( voice == _SsVmMaxVoice )
83 {
84 _snd_ev_flag = 0;
85 return -1;
86 }
87 _svm_cur.m_voice_idx = voice;
88 voice_struct = &_svm_voice[voice];
89 voice_struct->m_seq_sep_no = 33;
90 voice_struct->m_vab_id = vab_id;
91 voice_struct->m_prog = prog;
92 voice_struct->m_fake_program = _svm_cur.m_fake_program;
93 voice_struct->m_vag_idx = _svm_cur.m_vag_idx2;
94 voice_struct->m_note = note;
95 voice_struct->m_tone = _svm_cur.m_tone;
96 voice_struct->m_unk1d = 1;
97 voice_struct->m_unk02 = 0;
98 voice_struct->m_voll2 = _svm_cur.m_voll;
99 _SsVmDoAllocate();
100 if ( _svm_cur.m_vag_idx2 == 255 )
101 {
102 vmNoiseOn(voice);
103 }
104 else
105 {
106 _SsVmKeyOnNow(1, note2pitch2(note, fine));
107 }
108 _snd_ev_flag = 0;
109 return voice;
110}
111
112s16 SsUtKeyOff(s16 vc, s16 vab_id, s16 prog, s16 tone, s16 note)
113{
114 libsnd2_spu_voice_t *voice_struct;
115
116 if ( _snd_ev_flag == 1 )
117 return -1;
118 _snd_ev_flag = 1;
119 if ( (u16)vc >= 0x18u )
120 {
121 _snd_ev_flag = 0;
122 return -1;
123 }
124 voice_struct = &_svm_voice[vc];
125 if (
126 voice_struct->m_vab_id == vab_id && voice_struct->m_prog == prog && voice_struct->m_tone == tone
127 && voice_struct->m_note == note && voice_struct->m_vag_idx == 255 )
128 {
129 vmNoiseOff(vc);
130 }
131 else
132 {
133 _svm_cur.m_voice_idx = vc;
134 _SsVmKeyOffNow();
135 }
136 voice_struct->m_b_auto_pan = 0;
137 voice_struct->m_b_auto_vol = 0;
138 _snd_ev_flag = 0;
139 return 0;
140}