开启左侧

给点播放声音的新思路

[复制链接]
Harlotte 作者认证 2024-5-17 06:00:22

还没有账号?赶快去注册吧!

您需要 登录 才可以下载或查看,没有账号?立即注册

×
本帖最后由 Harlotte 于 2024-5-18 15:24 编辑
/ z2 d3 E: \2 z6 d' q8 j& O3 I6 X+ e! D# @5 z
+ p7 j1 A1 A4 A/ o+ B% Y
最近在研究JS 做了一个简单的逻辑来播放声音/ ?1 T0 b4 J: S9 U- |6 x
  1. var pit = 1;//初始化音高(速度)2 }- ~5 G. {) u" n
  2. var wheelsounds = ["mtr:df5g_wheel1","mtr:df5g_wheel2","mtr:df5g_wheel3"]
    + N; ~: j9 I. l) j  F! K) c
  3. function create(ctx, state, train) {2 Z( [, \3 P2 A; I0 J( v: Y
  4.     state.number = new Array();
    % Y# X* {: c( Z& S% F) c+ w: P
  5.     state.soundsspeed = 0;//初始化速度8 q8 l- \; R- \) c
  6.     state.speedsub = 0;
    4 u4 U" w9 o7 Z4 _
  7.     for(let i = 0; i < 100; i++){: |7 n# r3 `- D, w8 Q: S
  8.         state.number[i]=0;//初始化数组
    ' ]0 x; A7 m) n& n
  9.     }
    5 B- |7 j/ C% k0 C9 u1 ?
  10. }" m! g$ L" s: W0 V$ y0 ]# |8 c

  11. ; q% M" E5 V, J- Y& g2 t% P" p' L8 a# _
  12. function render(ctx, state, train) {
    2 r) l9 b% p+ b4 k) C
  13.     ctx.setDebugInfo("sounds=",1);//记录声音程序已启动
    ) @* A0 `, j/ @- s4 _
  14.     pit = 1 + train.speed() / 40;//更新音高(速度)
    8 W9 L' y2 L8 V* d" P
  15.     let gmk = getCurrentTrackModelKey(ctx,state,train);//获取当前轨道的自定义轨道名称    " O- j: r$ ?7 g
  16.     state.speedsub = (train.speed() - state.soundsspeed) * Timing.delta() * 1000000;//计算速度差
    6 ?% w* ?6 S4 V2 ^/ C( C. {
  17.     ctx.setDebugInfo("speedsub=",state.speedsub)1 f" X& k3 U' C  }" W
  18.     if(train.isOnRoute()&&gamerunning==1){//如果工作6 S; g# d2 G$ }0 P9 b9 D
  19.         for (let i = 0; i < train.trainCars(); i++) {
    ' ~" u$ P) X6 h- b9 V
  20.             ctx.setDebugInfo("soundsrun",1);//记录声音开启& n+ ?$ y9 S' j9 T+ ]5 a
  21.             if(state.speedsub<-1){//如果车辆减速
    : O, l2 Y9 f* X4 e% S# b
  22.                 ctx.setDebugInfo("shache",1);//记录刹车9 `" h9 e7 h$ O
  23.                 state.number[0]=sound(ctx,"mtr:df5g_shache",i,soundspos.braking1,pit,state.number[0],16,0.03+train.speed()*20*3.6/800);//更新返回时间 播放刹车声在转向架1# }) K- |* q. z4 e( f+ j, O
  24.                 state.number[0]=sound(ctx,"mtr:df5g_shache",i,soundspos.braking2,pit,state.number[0],16,0.03+train.speed()*20*3.6/800);//更新返回时间 播放刹车声在转向架2  t% y! t6 J9 G! E# k) D
  25.             }else if(state.speedsub>1){//如果车辆加速2 H) E" Z8 s: f5 Z% M; z
  26.                 ctx.setDebugInfo("addspeed",1);//记录加速
    ; I! z5 }: Q* ~2 f
  27.                 state.number[2]=sound(ctx,"mtr:df5g_engine",i,soundspos.engine,pit,state.number[2],20,grnn(0.8,1));//播放加速声$ \+ s1 o% w! L% s1 @
  28.             }: e4 z6 z3 ^6 Y6 d# t2 P
  29.             state.number[1]=sound(ctx,"mtr:df5g_engine2",i,soundspos.engine,pit,state.number[1],16,1);//播放怠速声
    * w+ [: Q$ j0 h
  30.             if(gmk.indexOf("horn")!=-1){//如果在轨道名称里找到了horn,则播放鸣笛声5 E  @. f& c+ Z- v5 _
  31.                 ctx.setDebugInfo("horn",1);//记录鸣笛声开启
    * n* k( w3 M3 I2 o! D2 X4 }. [
  32.                 state.number[3]=sound(ctx,"mtr:df5g_horn",i,soundspos.horn,1,state.number[3],32,5);//播放鸣笛声,单位是秒,推荐是5秒左右,可以根据需要调整 我这里是因为音频不合适循环播放,实际音频在1s左右
    5 _  I4 U0 }/ n
  33.             }  c0 f/ W7 b+ _1 ?, W- e, K' R
  34.             if(train.speed()*20*3.6>5){7 n3 k+ J& `6 p( v* S
  35.                 state.number[4]=sound(ctx,"mtr:df5g_wheelrun",i,soundspos.braking2,pit,state.number[4],16,9);//播放普通轮轨声
    $ ?: }+ O' f" o8 I( \
  36.                 state.number[4]=sound(ctx,"mtr:df5g_wheelrun",i,soundspos.braking1,pit,state.number[4],16,9);//播放普通轮轨声
    8 Q9 |" Z; y: p. K6 `
  37.             }
    ! D* `0 {1 |8 h; ^4 [4 b
  38.             if(train.speed()>0&&grnn(0,20+train.speed()/30)>19){8 K% r1 I/ v5 I% F( ~8 `1 v; N# M
  39.                 state.number[5]=sound(ctx,wheelsounds[Math.floor(grnn(0,3))],i,soundspos.braking2,0.8,state.number[5],32,0.05);//播放撞轨声
    / X) j" v7 f7 P
  40.                 state.number[5]=sound(ctx,wheelsounds[Math.floor(grnn(0,3))],i,soundspos.braking1,0.8,state.number[5],32,0.05);//播放撞轨声
    0 o. g# M+ T; {9 E- a3 n
  41.             }+ H9 q2 {% a' u8 Y
  42.         }
    % a2 _  x. F7 F) ?9 f
  43.     }
    , o  O* O8 _1 c/ v: l. V; N3 H
  44.     state.soundsspeed = train.speed();//更新速度1 l" K; a$ q4 I# e" |/ n' L
  45.     for(let i=0;i<4;i++){* b$ b0 y# K! A) P- y& O& x4 H
  46.         ctx.setDebugInfo(i+"=",state.number[i]);( X7 O0 y6 \- [5 s
  47.     }6 r. g, A# N& J+ U3 ?+ H
  48. }
    ) P- l/ _, ]' p! Z. @: _% [+ {. [( e
  49. function sound(ctx,name2,i,vect,pit,nu,ll,long){//播放声音pit是音高(速度) nu是一个数组,用来记录播放时间,ll是音频响度,long是循环播放时间
    - Y6 o) Q1 ?" M
  50.     if(Timing.elapsed()>nu){//如果时间超过了播放时间1 s( i8 p, x. l. t6 `( {; g
  51.         ctx.playCarSound(Resources.id(name2) , i , vect.x() , vect.y() , vect.z() , ll , pit);//播放
    # `) W7 r( u; k. N( p
  52.         nu = Timing.elapsed()+long;   //更新播放时间
    1 [2 Y7 o  E4 R) P: M' K( H
  53.     }+ o: ]( O, O3 R" t2 b3 v4 y2 j
  54.     return nu;//返回新的播放时间
    % `* ]7 {( A& \1 N
  55. }
    " g# W# x5 _& F- |& o4 C: n1 a
  56. function grnn(min, max) {//随机小数
    5 O5 b# u* A% t5 H. N) S% n
  57.     return  Math.random()*(max-min)+min;
    1 {1 m' y1 P- V5 E3 C4 y! e
  58. }- x( J# T& y1 h
  59. function getCurrentTrackModelKey(ctx, state, train) {//获取当前轨道的自定义轨道名称,让ai写的5 r! q. u, Y/ y
  60.     // 获取列车从车库开出的距离
    & ~2 n5 M, m- _
  61.     let railProgress = train.railProgress();
    : D" `/ \, r! C8 V4 q3 M
  62.     // 获取当前轨道的索引
    ) i) v& c. d% F$ M: n6 m$ i  i; x
  63.     let currentRailIndex = train.getRailIndex(railProgress, true);
    9 f$ x, @/ ]2 P# O& a- ?  u/ f
  64.     // 检查当前轨道索引是否有效
    ) h2 m: ^. i8 t  N* L
  65.     if (currentRailIndex >= 0 && currentRailIndex < train.path().size()) {
    5 K/ K+ f( I8 M0 Y
  66.         // 获取当前路径数据对象+ Z$ e) B8 t# e! w0 Y
  67.         let currentPathData = train.path().get(currentRailIndex);
      |& U' ^/ ]3 U0 B$ Z+ G
  68.         // 获取当前轨道使用的自定义轨道名称" ^1 z! C0 V* h# I# k1 B$ `
  69.         let trackModelKey = currentPathData.rail.getModelKey();
    ! d$ V9 f6 ?1 o/ h! ?- U5 d4 @; B
  70.         return trackModelKey;
    6 `1 {$ h2 ^% Q. o" ^, j
  71.     }
    ' M1 D8 h+ o* [6 L
  72.     return null; // 或者是一个默认值1 a. T7 R3 V; ~' h5 L9 B% H
  73. }
复制代码
相比bve 用这种方式可以搞更多的逻辑,随机叠加音频,实测效果非常不错* R4 B7 I$ _( j$ H
有事加我QQ: 3435494979
要什么名字 作者认证 2024-5-17 17:04:29
bve声音要方便一些吧,他有可视化可预览的编辑工具
LG STUDIO(原CLARSC)成员,制作MTR追加hmmsim/openbve列车及线路,如果有事请通过QQ联络
楼主 Harlotte 作者认证 2024-5-17 19:17:25
要什么名字 发表于 2024-5-17 17:04* q  U0 r/ k+ I2 T& s
bve声音要方便一些吧,他有可视化可预览的编辑工具

" A2 p2 [4 K1 D1 ]嘶 主要是这个把 我懒得学了
有事加我QQ: 3435494979
huliawsl 2024-5-17 21:12:04
本帖最后由 huliawsl 于 2024-5-17 22:09 编辑
( ^' K! R- @! y/ C$ ]9 N/ q/ _5 f. `1 K5 j" E7 K0 Z5 `) G% O
相较于此,我还是认为bve格式会更简单
屑搞3d的 https://space.bilibili.com/403409690
mtrbbs的私信咱很少看 如果需要联系咱可以尝试其他途径
楼主 Harlotte 作者认证 2024-5-18 09:21:18
本帖最后由 Harlotte 于 2024-5-18 15:18 编辑 2 x# ]: n2 [' V# N, k8 u; [2 S( Z/ d" G* d
huliawsl 发表于 2024-5-17 21:12+ k4 ~% T* M. s5 @' `; `
相较于此,我还是认为bve格式会更简单

. R& C  T  q+ W7 p5 }$ n🤔不过吧 这么做花样可以更多倒是
有事加我QQ: 3435494979
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

快速回复 返回顶部 返回列表