SoundMonitor.java
     1: //========================================================================================
     2: //  SoundMonitor.java
     3: //    en:Sound monitor
     4: //    ja:音声モニタ
     5: //  Copyright (C) 2003-2017 Makoto Kamada
     6: //
     7: //  This file is part of the XEiJ (X68000 Emulator in Java).
     8: //  You can use, modify and redistribute the XEiJ if the conditions are met.
     9: //  Read the XEiJ License for more details.
    10: //  http://stdkmd.com/xeij/
    11: //========================================================================================
    12: 
    13: package xeij;
    14: 
    15: import java.awt.*;  //BasicStroke,BorderLayout,BoxLayout,Color,Component,Container,Cursor,Desktop,Dimension,Font,FlowLayout,Frame,Graphics,Graphics2D,GraphicsDevice,GraphicsEnvironment,GridLayout,Image,Insets,Paint,Point,Rectangle,RenderingHints,Robot,Shape,Stroke,TexturePaint,Toolkit
    16: import java.awt.event.*;  //ActionEvent,ActionListener,ComponentAdapter,ComponentEvent,ComponentListener,FocusAdapter,FocusEvent,FocusListener,InputEvent,KeyAdapter,KeyEvent,KeyListener,MouseAdapter,MouseEvent,MouseListener,MouseMotionAdapter,MouseWheelEvent,WindowAdapter,WindowEvent,WindowListener,WindowStateListener
    17: import java.awt.image.*;  //BufferedImage,DataBuffer,DataBufferByte,DataBufferInt,IndexColorModel
    18: import java.lang.*;  //Boolean,Character,Class,Comparable,Double,Exception,Float,IllegalArgumentException,Integer,Long,Math,Number,Object,Runnable,SecurityException,String,StringBuilder,System
    19: import java.util.*;  //ArrayList,Arrays,Calendar,GregorianCalendar,HashMap,Map,Map.Entry,Timer,TimerTask,TreeMap
    20: import javax.swing.*;  //AbstractSpinnerModel,Box,ButtonGroup,DefaultListModel,ImageIcon,JApplet,JButton,JCheckBox,JCheckBoxMenuItem,JDialog,JFileChooser,JFrame,JLabel,JList,JMenu,JMenuBar,JMenuItem,JPanel,JRadioButton,JScrollPane,JSpinner,JTextArea,JTextField,JTextPane,JViewport,ScrollPaneConstants,SpinnerListModel,SpinnerNumberModel,SwingConstants,SwingUtilities,UIManager,UIDefaults,UnsupportedLookAndFeelException
    21: 
    22: public class SoundMonitor {
    23: 
    24:   //パネル
    25:   public static final int SMN_WIDTH = 4 + 8 + 4 * (14 * 8 + 6) + 4;  //鍵盤の幅に合わせる。4の倍数に限る。488
    26:   public static final int SMN_OFFSET = SMN_WIDTH + 3 >> 2;
    27: 
    28:   //色
    29:   public static final byte SMN_BLACK  = (byte) 0b00000000;  //  0  0xff000000  黒  背景
    30:   public static final byte SMN_BLUE   = (byte) 0b01010101;  //  1  0xff2020ff  青  文字,黒鍵
    31:   public static final byte SMN_ORANGE = (byte) 0b10101010;  //  2  0xffff8740  橙  ハイライト
    32:   public static final byte SMN_WHITE  = (byte) 0b11111111;  //  3  0xffffffff  白  文字,白鍵
    33: 
    34:   //フォント
    35:   public static final byte[] SMN_FONT_1 = new byte[5 * 127];
    36:   public static final byte[] SMN_FONT_2 = new byte[5 * 127];
    37:   public static final byte[] SMN_FONT_3 = new byte[5 * 127];
    38: 
    39:   //アイコン
    40: /*
    41:   public static final int[] SMN_ICON_1 = {
    42:     0b01010101,0b01010100,  //0:[1]
    43:     0b01000000,0b00000100,
    44:     0b01000001,0b00000100,
    45:     0b01000001,0b00000100,
    46:     0b01000001,0b00000100,
    47:     0b01000001,0b00000100,
    48:     0b01000001,0b00000100,
    49:     0b01000000,0b00000100,
    50:     0b01010101,0b01010100,
    51:     0b01010101,0b01010100,  //1:[2]
    52:     0b01000000,0b00000100,
    53:     0b01000101,0b01000100,
    54:     0b01000000,0b01000100,
    55:     0b01000101,0b01000100,
    56:     0b01000100,0b00000100,
    57:     0b01000101,0b01000100,
    58:     0b01000000,0b00000100,
    59:     0b01010101,0b01010100,
    60:     0b01010101,0b01010100,  //2:[3]
    61:     0b01000000,0b00000100,
    62:     0b01000101,0b01000100,
    63:     0b01000000,0b01000100,
    64:     0b01000101,0b01000100,
    65:     0b01000000,0b01000100,
    66:     0b01000101,0b01000100,
    67:     0b01000000,0b00000100,
    68:     0b01010101,0b01010100,
    69:     0b01010101,0b01010100,  //3:[4]
    70:     0b01000000,0b00000100,
    71:     0b01000100,0b01000100,
    72:     0b01000100,0b01000100,
    73:     0b01000101,0b01000100,
    74:     0b01000000,0b01000100,
    75:     0b01000000,0b01000100,
    76:     0b01000000,0b00000100,
    77:     0b01010101,0b01010100,
    78:     0b01010101,0b01010100,  //4:[5]
    79:     0b01000000,0b00000100,
    80:     0b01000101,0b01000100,
    81:     0b01000100,0b00000100,
    82:     0b01000101,0b01000100,
    83:     0b01000000,0b01000100,
    84:     0b01000101,0b01000100,
    85:     0b01000000,0b00000100,
    86:     0b01010101,0b01010100,
    87:     0b01010101,0b01010100,  //5:[6]
    88:     0b01000000,0b00000100,
    89:     0b01000101,0b01000100,
    90:     0b01000100,0b00000100,
    91:     0b01000101,0b01000100,
    92:     0b01000100,0b01000100,
    93:     0b01000101,0b01000100,
    94:     0b01000000,0b00000100,
    95:     0b01010101,0b01010100,
    96:     0b01010101,0b01010100,  //6:[7]
    97:     0b01000000,0b00000100,
    98:     0b01000101,0b01000100,
    99:     0b01000000,0b01000100,
   100:     0b01000000,0b01000100,
   101:     0b01000000,0b01000100,
   102:     0b01000000,0b01000100,
   103:     0b01000000,0b00000100,
   104:     0b01010101,0b01010100,
   105:     0b01010101,0b01010100,  //7:[8]
   106:     0b01000000,0b00000100,
   107:     0b01000101,0b01000100,
   108:     0b01000100,0b01000100,
   109:     0b01000101,0b01000100,
   110:     0b01000100,0b01000100,
   111:     0b01000101,0b01000100,
   112:     0b01000000,0b00000100,
   113:     0b01010101,0b01010100,
   114:     0b01010101,0b01010100,  //8:[W]
   115:     0b01000000,0b00000100,
   116:     0b01000100,0b01000100,
   117:     0b01000100,0b01000100,
   118:     0b01000101,0b01000100,
   119:     0b01000101,0b01000100,
   120:     0b01000100,0b01000100,
   121:     0b01000000,0b00000100,
   122:     0b01010101,0b01010100,
   123:     0b01010101,0b01010100,  //9:[S]
   124:     0b01000000,0b00000100,
   125:     0b01000001,0b01000100,
   126:     0b01000100,0b00000100,
   127:     0b01000001,0b00000100,
   128:     0b01000000,0b01000100,
   129:     0b01000101,0b00000100,
   130:     0b01000000,0b00000100,
   131:     0b01010101,0b01010100,
   132:     0b01010101,0b01010100,  //10:[P]
   133:     0b01000000,0b00000100,
   134:     0b01000101,0b00000100,
   135:     0b01000100,0b01000100,
   136:     0b01000101,0b00000100,
   137:     0b01000100,0b00000100,
   138:     0b01000100,0b00000100,
   139:     0b01000000,0b00000100,
   140:     0b01010101,0b01010100,
   141:   };
   142: */
   143:   //  perl misc/itob.pl xeij/SoundMonitor.java SMN_ICON_1
   144:   public static final byte[] SMN_ICON_1 = "UT@\4A\4A\4A\4A\4A\4@\4UTUT@\4ED@DEDD\4ED@\4UTUT@\4ED@DED@DED@\4UTUT@\4DDDDED@D@D@\4UTUT@\4EDD\4ED@DED@\4UTUT@\4EDD\4EDDDED@\4UTUT@\4ED@D@D@D@D@\4UTUT@\4EDDDEDDDED@\4UTUT@\4DDDDEDEDDD@\4UTUT@\4ADD\4A\4@DE\4@\4UTUT@\4E\4DDE\4D\4D\4@\4UT".getBytes (XEiJ.ISO_8859_1);
   145: /*
   146:   public static final int[] SMN_ICON_3 = {
   147:     0b11111111,0b11111100,  //0:[1]
   148:     0b11000000,0b00001100,
   149:     0b11000011,0b00001100,
   150:     0b11000011,0b00001100,
   151:     0b11000011,0b00001100,
   152:     0b11000011,0b00001100,
   153:     0b11000011,0b00001100,
   154:     0b11000000,0b00001100,
   155:     0b11111111,0b11111100,
   156:     0b11111111,0b11111100,  //1:[2]
   157:     0b11000000,0b00001100,
   158:     0b11001111,0b11001100,
   159:     0b11000000,0b11001100,
   160:     0b11001111,0b11001100,
   161:     0b11001100,0b00001100,
   162:     0b11001111,0b11001100,
   163:     0b11000000,0b00001100,
   164:     0b11111111,0b11111100,
   165:     0b11111111,0b11111100,  //2:[3]
   166:     0b11000000,0b00001100,
   167:     0b11001111,0b11001100,
   168:     0b11000000,0b11001100,
   169:     0b11001111,0b11001100,
   170:     0b11000000,0b11001100,
   171:     0b11001111,0b11001100,
   172:     0b11000000,0b00001100,
   173:     0b11111111,0b11111100,
   174:     0b11111111,0b11111100,  //3:[4]
   175:     0b11000000,0b00001100,
   176:     0b11001100,0b11001100,
   177:     0b11001100,0b11001100,
   178:     0b11001111,0b11001100,
   179:     0b11000000,0b11001100,
   180:     0b11000000,0b11001100,
   181:     0b11000000,0b00001100,
   182:     0b11111111,0b11111100,
   183:     0b11111111,0b11111100,  //4:[5]
   184:     0b11000000,0b00001100,
   185:     0b11001111,0b11001100,
   186:     0b11001100,0b00001100,
   187:     0b11001111,0b11001100,
   188:     0b11000000,0b11001100,
   189:     0b11001111,0b11001100,
   190:     0b11000000,0b00001100,
   191:     0b11111111,0b11111100,
   192:     0b11111111,0b11111100,  //5:[6]
   193:     0b11000000,0b00001100,
   194:     0b11001111,0b11001100,
   195:     0b11001100,0b00001100,
   196:     0b11001111,0b11001100,
   197:     0b11001100,0b11001100,
   198:     0b11001111,0b11001100,
   199:     0b11000000,0b00001100,
   200:     0b11111111,0b11111100,
   201:     0b11111111,0b11111100,  //6:[7]
   202:     0b11000000,0b00001100,
   203:     0b11001111,0b11001100,
   204:     0b11000000,0b11001100,
   205:     0b11000000,0b11001100,
   206:     0b11000000,0b11001100,
   207:     0b11000000,0b11001100,
   208:     0b11000000,0b00001100,
   209:     0b11111111,0b11111100,
   210:     0b11111111,0b11111100,  //7:[8]
   211:     0b11000000,0b00001100,
   212:     0b11001111,0b11001100,
   213:     0b11001100,0b11001100,
   214:     0b11001111,0b11001100,
   215:     0b11001100,0b11001100,
   216:     0b11001111,0b11001100,
   217:     0b11000000,0b00001100,
   218:     0b11111111,0b11111100,
   219:     0b11111111,0b11111100,  //8:[W]
   220:     0b11000000,0b00001100,
   221:     0b11001100,0b11001100,
   222:     0b11001100,0b11001100,
   223:     0b11001111,0b11001100,
   224:     0b11001111,0b11001100,
   225:     0b11001100,0b11001100,
   226:     0b11000000,0b00001100,
   227:     0b11111111,0b11111100,
   228:     0b11111111,0b11111100,  //9:[S]
   229:     0b11000000,0b00001100,
   230:     0b11000011,0b11001100,
   231:     0b11001100,0b00001100,
   232:     0b11000011,0b00001100,
   233:     0b11000000,0b11001100,
   234:     0b11001111,0b00001100,
   235:     0b11000000,0b00001100,
   236:     0b11111111,0b11111100,
   237:     0b11111111,0b11111100,  //10:[P]
   238:     0b11000000,0b00001100,
   239:     0b11001111,0b00001100,
   240:     0b11001100,0b11001100,
   241:     0b11001111,0b00001100,
   242:     0b11001100,0b00001100,
   243:     0b11001100,0b00001100,
   244:     0b11000000,0b00001100,
   245:     0b11111111,0b11111100,
   246:   };
   247: */
   248:   //  perl misc/itob.pl xeij/SoundMonitor.java SMN_ICON_3
   249:   public static final byte[] SMN_ICON_3 = "\377\374\300\f\303\f\303\f\303\f\303\f\303\f\300\f\377\374\377\374\300\f\317\314\300\314\317\314\314\f\317\314\300\f\377\374\377\374\300\f\317\314\300\314\317\314\300\314\317\314\300\f\377\374\377\374\300\f\314\314\314\314\317\314\300\314\300\314\300\f\377\374\377\374\300\f\317\314\314\f\317\314\300\314\317\314\300\f\377\374\377\374\300\f\317\314\314\f\317\314\314\314\317\314\300\f\377\374\377\374\300\f\317\314\300\314\300\314\300\314\300\314\300\f\377\374\377\374\300\f\317\314\314\314\317\314\314\314\317\314\300\f\377\374\377\374\300\f\314\314\314\314\317\314\317\314\314\314\300\f\377\374\377\374\300\f\303\314\314\f\303\f\300\314\317\f\300\f\377\374\377\374\300\f\317\f\314\314\317\f\314\f\314\f\300\f\377\374".getBytes (XEiJ.ISO_8859_1);
   250: 
   251:   //波形
   252:   public static final int SMN_WAVE_X = 12;  //4の倍数に限る
   253:   public static final int SMN_WAVE_Y = 4;
   254:   public static final int SMN_WAVE_WIDTH = SMN_WIDTH - 4 - SMN_WAVE_X;
   255:   public static final int SMN_WAVE_VALUE_SHIFT = 11;  //振幅の縮小率
   256:   public static final int SMN_WAVE_HEIGHT = (1 << 16 - SMN_WAVE_VALUE_SHIFT) + 1;  //49
   257:   public static final int SMN_WAVE_SCALE_X_MAX = 5;
   258:   public static final int SMN_WAVE_SCALE_Y_MAX = 3;
   259:   public static final int[] smnOPMBuffer = new int[SoundSource.SND_CHANNELS * YM2151.OPM_BLOCK_SAMPLES];
   260:   public static final int[] smnPCMBuffer = new int[SoundSource.SND_CHANNELS * YM2151.OPM_BLOCK_SAMPLES];
   261:   public static final int[][] smnWaveIndex0 = new int[SMN_WAVE_SCALE_X_MAX + 1][];
   262:   public static final int[][] smnWaveIndex1 = new int[SMN_WAVE_SCALE_X_MAX + 1][];
   263:   public static int smnWaveScaleX;
   264:   public static int smnWaveScaleY;
   265:   public static int smnWaveOffsetMax;
   266:   public static int smnWaveOffset;
   267:   public static int smnWaveElevation;
   268:   public static boolean smnWaveDragOn;
   269:   public static int smnWavePressedX;
   270:   public static int smnWavePressedY;
   271:   public static final int[] smnWaveLastYPCMLeft = new int[2];
   272:   public static final int[] smnWaveLastYPCMRight = new int[2];
   273:   public static final int[] smnWaveLastYOPMLeft = new int[2];
   274:   public static final int[] smnWaveLastYOPMRight = new int[2];
   275: 
   276:   //スペクトラムアナライザ
   277:   //  YM2151.OPM_SAMPLE_FREQ == 62500 && YM2151.OPM_BLOCK_SAMPLES == 2500 && SMN_WIDTH == 480 に固定
   278:   public static final int SMN_SPECTRUM_X = 4;  //4の倍数に限る
   279:   public static final int SMN_SPECTRUM_Y = SMN_WAVE_Y + SMN_WAVE_HEIGHT + 4;
   280:   public static final int SMN_SPECTRUM_WIDTH = 480;  //SMN_WIDTH-4-SMN_SPECTRUM_X
   281:   public static final int SMN_SPECTRUM_HEIGHT = 32;
   282: /*
   283:   public static final int[] SMN_SPECTRUM_MAP = {
   284:     //       C           C#               D              D#               E                               F
   285:     0, 1, 2, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31, 33, 35, 36, 37, 38, 39, 40, 41, 42, 43,
   286:     //          F#               G              G#               A              A#               B
   287:     45, 47, 49, 51, 53, 55, 57, 59, 61, 63, 65, 67, 69, 71, 73, 75, 77, 79, 81, 83, 85, 87, 89, 91, 92, 93, 94, 95,
   288:   };
   289: */
   290:   //  perl misc/itob.pl xeij/SoundMonitor.java SMN_SPECTRUM_MAP
   291:   public static final byte[] SMN_SPECTRUM_MAP = "\0\1\2\3\5\7\t\13\r\17\21\23\25\27\31\33\35\37!#$%&\'()*+-/13579;=?ACEGIKMOQSUWY[\\]^_".getBytes (XEiJ.ISO_8859_1);
   292:   public static final int SMN_SPECTRUM_BIT = 10;  //FFTのサンプル数のbit数
   293:   public static final int SMN_SPECTRUM_N = 1 << SMN_SPECTRUM_BIT;  //FFTのサンプル数
   294:   public static final int SMN_SPECTRUM_PARTITIONS = 6;  //分割数
   295:   public static final int SMN_SPECTRUM_RANGE = 480 / SMN_SPECTRUM_PARTITIONS;  //分割幅。120
   296:   public static final int[] SMN_SPECTRUM_INTERVAL = new int[] { 100, 100, 50, 25, 10, 5 };  //サンプリング間隔
   297:   public static final double[][] smnSpectrumBuffer = new double[SMN_SPECTRUM_PARTITIONS][];  //バッファ。[1]は使わない
   298:   public static final double[] smnSpectrumX = new double[SMN_SPECTRUM_N];  //実数部
   299:   public static final double[] smnSpectrumY = new double[SMN_SPECTRUM_N];  //虚数部
   300:   public static final int[] smnSpectrumIndex = new int[480];  //幅<<16|インデックス
   301:   public static final int[] smnSpectrumValue = new int[480];  //前回の値
   302:   public static final double[] smnSpectrumWindow = new double[SMN_SPECTRUM_N];  //窓関数
   303:   public static final int SMN_SPECTRUM_TRAIL = 2;  //一度に減らす数の上限。小さくするとゆっくり下がる。小さくしすぎると最後に伸びた線がどれかわからなくなる
   304:   public static final double SMN_SPECTRUM_SCALE = 64.0;  //拡大率
   305:   public static FFT smnFFT;  //FFT
   306: 
   307:   //440Hzマーク
   308:   public static final int[] SMN_440HZ_PATTERN = { 18, SMN_BLUE, 0, 0, -1, 1, 1, 1, -2, 2, 2, 2, -1, 3, 1, 3, 0, 4 };
   309: 
   310:   //鍵盤
   311:   public static final int SMN_KEY_X = 12;
   312:   public static final int SMN_KEY_Y = SMN_SPECTRUM_Y + SMN_SPECTRUM_HEIGHT + 1;
   313:   public static final int SMN_KEY_HEIGHT = 6 + 12 * 8;
   314:   public static final byte[] SMN_KEY_COLOR = {
   315:     SMN_WHITE,  //C
   316:     SMN_BLUE,   //C#
   317:     SMN_WHITE,  //D
   318:     SMN_BLUE,   //D#
   319:     SMN_WHITE,  //E
   320:     SMN_WHITE,  //F
   321:     SMN_BLUE,   //F#
   322:     SMN_WHITE,  //G
   323:     SMN_BLUE,   //G#
   324:     SMN_WHITE,  //A
   325:     SMN_BLUE,   //A#
   326:     SMN_WHITE,  //B
   327:   };
   328:   public static final int[] smnKey = new int[8];  //キーオンされているキー。-1=なし,0=O0C
   329: 
   330:   //音色
   331:   public static final int SMN_TONE_X = 20;  //4の倍数に限る
   332:   public static final int SMN_TONE_Y = SMN_KEY_Y + SMN_KEY_HEIGHT + 4;
   333:   public static final int SMN_TONE_BOX_COLS = 3 * 12 + 2;
   334:   public static final int SMN_TONE_BOX_HEIGHT = 6 * 7 + 4;
   335:   public static final int SMN_TONE_HEIGHT = SMN_TONE_BOX_HEIGHT * 3 - 4;
   336:   public static final char[] SMN_TONE_H1 = "FC SL WA SY SP PD AD PS AS PN"   .toCharArray ();
   337:   public static final char[] SMN_TONE_V0 = "00 00 00 00 00 00 00 00 00 00 00".toCharArray ();
   338:   public static final char[] SMN_TONE_H2 = "AR 1R 2R RR 1L TL KS ML T1 T2 AE".toCharArray ();
   339:   public static final char[] SMN_TONE_M1 = "M1".toCharArray ();
   340:   public static final char[] SMN_TONE_V1 = "00 00 00 00 00 00 00 00 00 00 00".toCharArray ();
   341:   public static final char[] SMN_TONE_C1 = "C1".toCharArray ();
   342:   public static final char[] SMN_TONE_V2 = "00 00 00 00 00 00 00 00 00 00 00".toCharArray ();
   343:   public static final char[] SMN_TONE_M2 = "M2".toCharArray ();
   344:   public static final char[] SMN_TONE_V3 = "00 00 00 00 00 00 00 00 00 00 00".toCharArray ();
   345:   public static final char[] SMN_TONE_C2 = "C2".toCharArray ();
   346:   public static final char[] SMN_TONE_V4 = "00 00 00 00 00 00 00 00 00 00 00".toCharArray ();
   347: 
   348:   //PCM
   349:   public static final int SMN_PCM_COL = SMN_TONE_X / 4 + SMN_TONE_BOX_COLS * 2 + 6;
   350:   public static final int SMN_PCM_Y = SMN_TONE_Y + SMN_TONE_BOX_HEIGHT * 2;
   351:   //            11111111112222
   352:   //  012345678901234567890123
   353:   //  II 16MHz 1/512  31.3kHz
   354:   //     ^^      ^^^^ ^^^^
   355:   //  II PLAY DATA LEFT RIGHT
   356:   //     ^^^^ ^^^^ ^^^^ ^^^^^
   357:   public static final char[][] SMN_PCM_OSCILLATOR = {  //[ADPCM.pcmOSCFreqMode<<1|ADPCM.pcmOscillator]
   358:     " 8".toCharArray (),
   359:     " 4".toCharArray (),
   360:     " 8".toCharArray (),
   361:     "16".toCharArray (),
   362:   };
   363:   public static final char[][] SMN_PCM_DIVIDER = {  //[ADPCM.pcmDivider]
   364:     "1024".toCharArray (),
   365:     "768 ".toCharArray (),
   366:     "512 ".toCharArray (),
   367:     "768 ".toCharArray (),
   368:   };
   369:   public static final char[][] SMN_PCM_FREQ = {  //[ADPCM.pcmOSCFreqMode<<3|ADPCM.pcmOscillator<<2|ADPCM.pcmDivider]
   370:     " 7.8".toCharArray (),
   371:     "10.4".toCharArray (),
   372:     "15.6".toCharArray (),
   373:     "10.4".toCharArray (),
   374:     " 3.9".toCharArray (),
   375:     " 5.2".toCharArray (),
   376:     " 7.8".toCharArray (),
   377:     " 5.2".toCharArray (),
   378:     " 7.8".toCharArray (),
   379:     "10.4".toCharArray (),
   380:     "15.6".toCharArray (),
   381:     "10.4".toCharArray (),
   382:     "15.6".toCharArray (),
   383:     "20.8".toCharArray (),
   384:     "31.3".toCharArray (),
   385:     "20.8".toCharArray (),
   386:   };
   387:   public static final char[][] SMN_PCM_PLAY = {  //[ADPCM.pcmActive?1:0]
   388:     "    ".toCharArray (),
   389:     "PLAY".toCharArray (),
   390:   };
   391:   public static final char[][] SMN_PCM_DATA = {  //[ADPCM.pcmEncodedData>=0?1:0]
   392:     "    ".toCharArray (),
   393:     "DATA".toCharArray (),
   394:   };
   395:   public static final char[][] SMN_PCM_LEFT = {  //[ADPCM.pcmPanLeft==0||ADPCM.pcmPanLeft<0x80000000+ADPCM.PCM_ATTACK_SPAN*2?0:1]
   396:     "    ".toCharArray (),
   397:     "LEFT".toCharArray (),
   398:   };
   399:   public static final char[][] SMN_PCM_RIGHT = {  //[SoundSource.SND_CHANNELS==1?ADPCM.pcmPanLeft==0||ADPCM.pcmPanLeft<0x80000000+ADPCM.PCM_ATTACK_SPAN*2?0:1:ADPCM.pcmPanRight==0||ADPCM.pcmPanRight<0x80000000+ADPCM.PCM_ATTACK_SPAN*2?0:1]
   400:     "     ".toCharArray (),
   401:     "RIGHT".toCharArray (),
   402:   };
   403: 
   404:   //パレット
   405:   public static final int SMN_PALET_X = SMN_TONE_X + 4 * SMN_TONE_BOX_COLS * 2;
   406:   public static final int SMN_PALET_Y = SMN_TONE_Y + SMN_TONE_BOX_HEIGHT * 2 + 6 * 2;
   407:   public static final int SMN_PALET_WIDTH = 140;
   408:   public static final int SMN_PALET_HEIGHT = 6 * 5;
   409:   public static final int[][] SMN_SLIDER_ARRAY = {
   410:     { SMN_PALET_X + 4 *  2, SMN_PALET_Y + 6 * 1, 48, SMN_WHITE ,  0 },  //0 H  0
   411:     { SMN_PALET_X + 4 * 16, SMN_PALET_Y + 6 * 1, 32, SMN_WHITE ,  0 },  //0 S  0
   412:     { SMN_PALET_X + 4 * 26, SMN_PALET_Y + 6 * 1, 32, SMN_WHITE ,  0 },  //0 B  0
   413:     { SMN_PALET_X + 4 *  2, SMN_PALET_Y + 6 * 2, 48, SMN_BLUE  , 30 },  //1 H 32
   414:     { SMN_PALET_X + 4 * 16, SMN_PALET_Y + 6 * 2, 32, SMN_BLUE  , 30 },  //1 S 28
   415:     { SMN_PALET_X + 4 * 26, SMN_PALET_Y + 6 * 2, 32, SMN_BLUE  , 32 },  //1 B 32
   416:     { SMN_PALET_X + 4 *  2, SMN_PALET_Y + 6 * 3, 48, SMN_ORANGE,  3 },  //2 H  3
   417:     { SMN_PALET_X + 4 * 16, SMN_PALET_Y + 6 * 3, 32, SMN_ORANGE, 30 },  //2 S 24
   418:     { SMN_PALET_X + 4 * 26, SMN_PALET_Y + 6 * 3, 32, SMN_ORANGE, 32 },  //2 B 32
   419:     { SMN_PALET_X + 4 *  2, SMN_PALET_Y + 6 * 4, 48, SMN_WHITE , 30 },  //3 H  0
   420:     { SMN_PALET_X + 4 * 16, SMN_PALET_Y + 6 * 4, 32, SMN_WHITE ,  3 },  //3 S  0
   421:     { SMN_PALET_X + 4 * 26, SMN_PALET_Y + 6 * 4, 32, SMN_WHITE , 30 },  //3 B 32
   422:   };
   423:   public static final int[][] SMN_SLIDER_PATTERN = {
   424:     { 18, SMN_WHITE ,   1, -2, 2, -2,   0, -1,               3, -1,                 0, 1,             3, 1,   1, 2, 2, 2,
   425:       14, SMN_BLACK ,                          1, -1, 2, -1,          1, 0, 2, 0,         1, 1, 2, 1                     },
   426:     { 26, SMN_BLUE  ,   1, -2, 2, -2,   0, -1, 1, -1, 2, -1, 3, -1,                 0, 1, 1, 1, 2, 1, 3, 1,   1, 2, 2, 2 },
   427:     { 26, SMN_ORANGE,   1, -2, 2, -2,   0, -1, 1, -1, 2, -1, 3, -1,                 0, 1, 1, 1, 2, 1, 3, 1,   1, 2, 2, 2 },
   428:     { 26, SMN_WHITE ,   1, -2, 2, -2,   0, -1, 1, -1, 2, -1, 3, -1,                 0, 1, 1, 1, 2, 1, 3, 1,   1, 2, 2, 2 },
   429:   };
   430:   public static int smnPaletDragSlider;
   431:   public static int smnPaletPressedX;
   432:   public static IndexColorModel smnColorModel;
   433: 
   434:   //一時停止
   435:   public static final int SMN_PAUSE_WIDTH = 16;
   436:   public static final int SMN_PAUSE_HEIGHT = 16;
   437:   public static final int SMN_PAUSE_X = SMN_WIDTH - 4 - SMN_PAUSE_WIDTH;
   438:   public static final int SMN_PAUSE_Y = SMN_TONE_Y + SMN_TONE_BOX_HEIGHT * 2 + 6 * 3 + (6 * 4 - SMN_PAUSE_HEIGHT >> 1);
   439: /*
   440:   public static final int[] SMN_PAUSE_ICON_3 = {
   441:     0b00000000,0b00000000,0b00000000,0b00000000,
   442:     0b00111111,0b11111111,0b11111111,0b11111100,
   443:     0b00110000,0b00000000,0b00000000,0b00001100,
   444:     0b00110000,0b00000000,0b00000000,0b00001100,
   445:     0b00110000,0b11111111,0b11111111,0b00001100,
   446:     0b00110000,0b11111111,0b11111111,0b00001100,
   447:     0b00110000,0b11111111,0b11111111,0b00001100,
   448:     0b00110000,0b11111111,0b11111111,0b00001100,
   449:     0b00110000,0b11111111,0b11111111,0b00001100,
   450:     0b00110000,0b11111111,0b11111111,0b00001100,
   451:     0b00110000,0b11111111,0b11111111,0b00001100,
   452:     0b00110000,0b11111111,0b11111111,0b00001100,
   453:     0b00110000,0b00000000,0b00000000,0b00001100,
   454:     0b00110000,0b00000000,0b00000000,0b00001100,
   455:     0b00111111,0b11111111,0b11111111,0b11111100,
   456:     0b00000000,0b00000000,0b00000000,0b00000000,
   457:   };
   458: */
   459:   //  perl misc/itob.pl xeij/SoundMonitor.java SMN_PAUSE_ICON_3
   460:   public static final byte[] SMN_PAUSE_ICON_3 = "\0\0\0\0?\377\377\3740\0\0\f0\0\0\f0\377\377\f0\377\377\f0\377\377\f0\377\377\f0\377\377\f0\377\377\f0\377\377\f0\377\377\f0\0\0\f0\0\0\f?\377\377\374\0\0\0\0".getBytes (XEiJ.ISO_8859_1);
   461: /*
   462:   public static final int[] SMN_PLAY_ICON_3 = {
   463:     0b00000000,0b00000000,0b00000000,0b00000000,
   464:     0b00111111,0b11111111,0b11111111,0b11111100,
   465:     0b00110000,0b00000000,0b00000000,0b00001100,
   466:     0b00110000,0b00000000,0b00000000,0b00001100,
   467:     0b00110000,0b11110000,0b00000000,0b00001100,
   468:     0b00110000,0b11111111,0b00000000,0b00001100,
   469:     0b00110000,0b11111111,0b11110000,0b00001100,
   470:     0b00110000,0b11111111,0b11111111,0b00001100,
   471:     0b00110000,0b11111111,0b11111111,0b00001100,
   472:     0b00110000,0b11111111,0b11110000,0b00001100,
   473:     0b00110000,0b11111111,0b00000000,0b00001100,
   474:     0b00110000,0b11110000,0b00000000,0b00001100,
   475:     0b00110000,0b00000000,0b00000000,0b00001100,
   476:     0b00110000,0b00000000,0b00000000,0b00001100,
   477:     0b00111111,0b11111111,0b11111111,0b11111100,
   478:     0b00000000,0b00000000,0b00000000,0b00000000,
   479:   };
   480: */
   481:   //  perl misc/itob.pl xeij/SoundMonitor.java SMN_PLAY_ICON_3
   482:   public static final byte[] SMN_PLAY_ICON_3 = "\0\0\0\0?\377\377\3740\0\0\f0\0\0\f0\360\0\f0\377\0\f0\377\360\f0\377\377\f0\377\377\f0\377\360\f0\377\0\f0\360\0\f0\0\0\f0\0\0\f?\377\377\374\0\0\0\0".getBytes (XEiJ.ISO_8859_1);
   483:   public static boolean smnPauseRequest;  //次の更新でsmnPauseUpdateに設定する値
   484:   public static boolean smnPauseUpdate;  //true=更新を一時停止している
   485: 
   486:   //パネル
   487:   public static final int SMN_HEIGHT = SMN_TONE_Y + SMN_TONE_HEIGHT + 4;
   488:   public static boolean smnIsVisible;
   489:   public static BufferedImage smnImage;
   490:   public static byte[] smnBitmap;
   491:   public static JPanel smnPanel;
   492:   public static JFrame smnFrame;
   493: 
   494:   //smnInit ()
   495:   //  初期化
   496:   public static void smnInit () {
   497: 
   498:     //フォント
   499:     {
   500:       final long m = 0b01010100_01010000_01000100_01000000_00010100_00010000_00000100_00000000L;
   501:       int k = 0;
   502:       for (int i = 0; i < 127; i++) {
   503:         int t = Indicator.IND_ASCII_3X5[i];
   504:         int d;
   505:         //d = t >> 14 - 6 & 0b01000000 | t >> 13 - 4 & 0b00010000 | t >> 12 - 2 & 0b00000100;
   506:         d = (int) (m >>> (t >>> 12 - 3 & 7 << 3)) & 255;
   507:         SMN_FONT_1[k] = (byte)  d;
   508:         SMN_FONT_2[k] = (byte) (d * 2);
   509:         SMN_FONT_3[k] = (byte) (d * 3);
   510:         k++;
   511:         //d = t >> 11 - 6 & 0b01000000 | t >> 10 - 4 & 0b00010000 | t >>  9 - 2 & 0b00000100;
   512:         d = (int) (m >>> (t >>>  9 - 3 & 7 << 3)) & 255;
   513:         SMN_FONT_1[k] = (byte)  d;
   514:         SMN_FONT_2[k] = (byte) (d * 2);
   515:         SMN_FONT_3[k] = (byte) (d * 3);
   516:         k++;
   517:         //d = t >>  8 - 6 & 0b01000000 | t >>  7 - 4 & 0b00010000 | t >>  6 - 2 & 0b00000100;
   518:         d = (int) (m >>> (t >>>  6 - 3 & 7 << 3)) & 255;
   519:         SMN_FONT_1[k] = (byte)  d;
   520:         SMN_FONT_2[k] = (byte) (d * 2);
   521:         SMN_FONT_3[k] = (byte) (d * 3);
   522:         k++;
   523:         //d = t <<  6 - 5 & 0b01000000 | t           & 0b00010000 | t >>  3 - 2 & 0b00000100;
   524:         d = (int) (m >>> (t            & 7 << 3)) & 255;
   525:         SMN_FONT_1[k] = (byte)  d;
   526:         SMN_FONT_2[k] = (byte) (d * 2);
   527:         SMN_FONT_3[k] = (byte) (d * 3);
   528:         k++;
   529:         //d = t <<  6 - 2 & 0b01000000 | t <<  4 - 1 & 0b00010000 | t <<  2 - 0 & 0b00000100;
   530:         d = (int) (m >>> (t <<   6 - 3 & 7 << 3)) & 255;
   531:         SMN_FONT_1[k] = (byte)  d;
   532:         SMN_FONT_2[k] = (byte) (d * 2);
   533:         SMN_FONT_3[k] = (byte) (d * 3);
   534:         k++;
   535:       }
   536:     }
   537: 
   538:     //波形
   539:     //smnOPMBuffer = new int[SoundSource.SND_CHANNELS * YM2151.OPM_BLOCK_SAMPLES];
   540:     //smnPCMBuffer = new int[SoundSource.SND_CHANNELS * YM2151.OPM_BLOCK_SAMPLES];
   541:     for (int scaleX = 0; scaleX <= SMN_WAVE_SCALE_X_MAX; scaleX++) {
   542:       int limit = SMN_WAVE_WIDTH << scaleX;
   543:       int[] index0 = smnWaveIndex0[scaleX] = new int[limit];
   544:       if (SoundSource.SND_CHANNELS == 1) {  //モノラル
   545:         for (int x = 0; x < limit; x++) {
   546:           index0[x] = x * YM2151.OPM_BLOCK_SAMPLES / limit;
   547:         }
   548:       } else {  //ステレオ
   549:         int[] index1 = smnWaveIndex1[scaleX] = new int[limit];
   550:         for (int x = 0; x < limit; x++) {
   551:           index1[x] = (index0[x] = x * YM2151.OPM_BLOCK_SAMPLES / limit << 1) + 1;
   552:         }
   553:       }
   554:     }
   555:     smnWaveScaleX = 0;
   556:     smnWaveScaleY = 0;
   557:     smnWaveOffsetMax = (SMN_WAVE_WIDTH << smnWaveScaleX) - SMN_WAVE_WIDTH;
   558:     smnWaveOffset = 0;
   559:     smnWaveElevation = 0;
   560:     smnWaveDragOn = false;
   561:     smnWavePressedX = -1;
   562:     smnWavePressedY = -1;
   563:     //smnWaveLastYPCMLeft = new int[2];
   564:     //smnWaveLastYPCMRight = new int[2];
   565:     //smnWaveLastYOPMLeft = new int[2];
   566:     //smnWaveLastYOPMRight = new int[2];
   567:     smnWaveLastYPCMLeft[0] = 0;
   568:     smnWaveLastYPCMLeft[1] = 0;
   569:     smnWaveLastYPCMRight[0] = 0;
   570:     smnWaveLastYPCMRight[1] = 0;
   571:     smnWaveLastYOPMLeft[0] = 0;
   572:     smnWaveLastYOPMLeft[1] = 0;
   573:     smnWaveLastYOPMRight[0] = 0;
   574:     smnWaveLastYOPMRight[1] = 0;
   575: 
   576:     //スペクトラムアナライザ
   577:     //smnSpectrumBuffer = new double[SMN_SPECTRUM_PARTITIONS][];  //バッファ
   578:     //smnSpectrumX = new double[SMN_SPECTRUM_N];  //実数部
   579:     //smnSpectrumY = new double[SMN_SPECTRUM_N];  //虚数部
   580:     //smnSpectrumIndex = new int[480];  //幅<<16|インデックス
   581:     //smnSpectrumValue = new int[480];  //前回の値
   582:     for (int partition = 0; partition < SMN_SPECTRUM_PARTITIONS; partition++) {
   583:       if (partition != 1) {
   584:         smnSpectrumBuffer[partition] = new double[SMN_SPECTRUM_N];
   585:       }
   586:       double coeff = (double) SMN_SPECTRUM_N * 440.0 / 62500.0 * (double) SMN_SPECTRUM_INTERVAL[partition];
   587:       for (int x = SMN_SPECTRUM_RANGE * partition, x1 = x + SMN_SPECTRUM_RANGE; x < x1; x++) {
   588:         int k = (int) Math.floor (coeff * Math.pow (
   589:           2.0,
   590:           (double) (((56 - 8 + x) / 56 - 1) * 96 + SMN_SPECTRUM_MAP[(56 - 8 + x) % 56] - (96 * 4 + 75)) / 96.0));
   591:         int n = (int) Math.floor (coeff * Math.pow (
   592:           2.0,
   593:           (double) (((56 - 8 + 1 + x) / 56 - 1) * 96 + SMN_SPECTRUM_MAP[(56 - 8 + 1 + x) % 56] - (96 * 4 + 75)) / 96.0)) - k;
   594:         smnSpectrumIndex[x] = n << 16 | k;
   595:         //smnSpectrumValue[x] = -1;
   596:       }
   597:     }  //for partition
   598:     Arrays.fill (smnSpectrumValue, -1);
   599:     //smnSpectrumWindow = new double[SMN_SPECTRUM_N];  //窓関数
   600:     for (int i = 0; i < SMN_SPECTRUM_N; i++) {
   601:       smnSpectrumWindow[i] = 0.5 * (1.0 - Math.cos (2.0 * Math.PI / (double) SMN_SPECTRUM_N * (double) i));  //Hanning窓
   602:     }
   603:     smnFFT = new FFT (SMN_SPECTRUM_N);
   604: 
   605:     //一時停止
   606:     //  初回の更新で一時停止の解除を行うことで一時停止アイコンを描かせる
   607:     smnPauseRequest = false;
   608:     smnPauseUpdate = true;
   609: 
   610:     //スライダ
   611:     smnPaletDragSlider = -1;
   612:     smnPaletPressedX = 0;
   613: 
   614:   }  //smnInit()
   615: 
   616:   //smnStart ()
   617:   public static void smnStart () {
   618:     if (RestorableFrame.rfmGetOpened (Settings.SGS_SMN_FRAME_KEY)) {
   619:       smnOpen ();
   620:     }
   621:   }  //smnStart()
   622: 
   623:   //smnOpen ()
   624:   //  音声モニタを開く
   625:   public static void smnOpen () {
   626:     if (smnFrame == null) {
   627:       smnMakeFrame ();
   628:       smnIsVisible = true;
   629:       smnUpdate ();
   630:       smnFrame.setVisible (true);
   631:     } else {
   632:       smnIsVisible = true;
   633:       smnUpdate ();
   634:       smnFrame.setVisible (true);
   635:     }
   636:   }  //smnOpen()
   637: 
   638:   //smnMakeFrame ()
   639:   //  ウインドウを作る
   640:   //  ここでは開かない
   641:   public static void smnMakeFrame () {
   642: 
   643:     //イメージ
   644:     smnMakeImage ();
   645: 
   646:     //パネル
   647:     smnPanel = ComponentFactory.setFixedSize (new JPanel () {
   648:       @Override public void paint (Graphics g) {
   649:         g.drawImage (smnImage, 0, 0, null);
   650:       }
   651:       @Override protected void paintComponent (Graphics g) {
   652:       }
   653:       @Override protected void paintBorder (Graphics g) {
   654:       }
   655:       @Override protected void paintChildren (Graphics g) {
   656:       }
   657:       @Override public void update (Graphics g) {
   658:       }
   659:     }, SMN_WIDTH, SMN_HEIGHT);
   660: 
   661:     //マウスリスナー
   662:     ComponentFactory.addListener (
   663:       smnPanel,
   664:       new MouseAdapter () {
   665:         @Override public void mouseClicked (MouseEvent me) {
   666:           int x = me.getX ();
   667:           int y = me.getY ();
   668:           int button = me.getButton ();
   669:           int modifiersEx = me.getModifiersEx ();
   670:           if (SMN_WAVE_X <= x && x < SMN_WAVE_X + SMN_WAVE_WIDTH &&
   671:               SMN_WAVE_Y <= y && y < SMN_WAVE_Y + SMN_WAVE_HEIGHT) {  //波形
   672:             if ((modifiersEx & InputEvent.SHIFT_DOWN_MASK) == 0) {  //Shiftが押されていない
   673:               int d = 0;
   674:               if (button == MouseEvent.BUTTON1) {  //左ボタンが押された
   675:                 if (smnWaveScaleX < SMN_WAVE_SCALE_X_MAX) {  //増やせる
   676:                   d = 1;
   677:                 }
   678:               } else if (button == MouseEvent.BUTTON3) {  //右ボタンが押された
   679:                 if (0 < smnWaveScaleX) {  //減らせる
   680:                   d = -1;
   681:                 }
   682:               }
   683:               if (d != 0) {
   684:                 smnWaveScaleX += d;
   685:                 smnWaveOffsetMax = (SMN_WAVE_WIDTH << smnWaveScaleX) - SMN_WAVE_WIDTH;
   686:                 int o = x - SMN_WAVE_X;  //SMN_WAVE_WIDTH/2。拡大縮小の中心
   687:                 smnWaveOffset = Math.max (0, Math.min (smnWaveOffsetMax, (d >= 0 ? smnWaveOffset + o << d : smnWaveOffset + o >> -d) - o));
   688:                 if (smnPauseUpdate) {  //一時停止中
   689:                   smnWavePaint ();
   690:                   smnPanel.repaint ();
   691:                 }
   692:               }
   693:             } else {  //Shiftが押されている
   694:               int d = 0;
   695:               if (button == MouseEvent.BUTTON1) {  //左ボタンが押された
   696:                 if (smnWaveScaleY < SMN_WAVE_SCALE_Y_MAX) {  //増やせる
   697:                   d = 1;
   698:                 }
   699:               } else if (button == MouseEvent.BUTTON3) {  //右ボタンが押された
   700:                 if (0 < smnWaveScaleY) {  //減らせる
   701:                   d = -1;
   702:                 }
   703:               }
   704:               if (d != 0) {
   705:                 //  smnWaveElevationはグラフが下にずれて上の方が見えているときマイナス、上にずれて下の方が見えているいるときプラス
   706:                 //  グラフの高さSMN_WAVE_HEIGHT*2^smnWaveScaleY
   707:                 //  グラフの上端から画面の中央までSMN_WAVE_HEIGHT*2^smnWaveScaleY/2+smnWaveElevation
   708:                 //  画面の中央からクリックした位置までo=y-(SMN_WAVE_Y+SMN_WAVE_HEIGHT/2)
   709:                 //  グラフの上端からクリックした位置までSMN_WAVE_HEIGHT*2^smnWaveScaleY/2+smnWaveElevation+o
   710:                 //  スケールを変更する
   711:                 //  グラフの上端からクリックした位置まで(SMN_WAVE_HEIGHT*2^smnWaveScaleY/2+smnWaveElevation+o)*2^d
   712:                 //  グラフの上端から画面の中央まで(SMN_WAVE_HEIGHT*2^smnWaveScaleY/2+smnWaveElevation+o)*2^d-o
   713:                 //  新しいelevation=(SMN_WAVE_HEIGHT*2^smnWaveScaleY/2+smnWaveElevation+o)*2^d-o-SMN_WAVE_HEIGHT*2^smnWaveScaleY*2^d/2
   714:                 //                 =(smnWaveElevation+o)*2^d-o
   715:                 smnWaveScaleY += d;
   716:                 int spaceY = (SMN_WAVE_HEIGHT >> 1 << smnWaveScaleY) - (SMN_WAVE_HEIGHT >> 1);
   717:                 int o = y - (SMN_WAVE_Y + SMN_WAVE_HEIGHT / 2);
   718:                 smnWaveElevation = Math.max (-spaceY, Math.min (spaceY, (d >= 0 ? smnWaveElevation + o << d : smnWaveElevation + o >> -d) - o));
   719:                 if (smnPauseUpdate) {  //一時停止中
   720:                   smnWavePaint ();
   721:                   smnPanel.repaint ();
   722:                 }
   723:               }
   724:             }
   725:           } else if (SMN_PAUSE_X <= x && x < SMN_PAUSE_X + SMN_PAUSE_WIDTH &&
   726:                      SMN_PAUSE_Y <= y && y < SMN_PAUSE_Y + SMN_PAUSE_HEIGHT) {  //一時停止
   727:             smnPauseRequest = !smnPauseRequest;
   728:           }
   729:         }
   730:         @Override public void mousePressed (MouseEvent me) {
   731:           int x = me.getX ();
   732:           int y = me.getY ();
   733:           smnWaveDragOn = false;
   734:           smnPaletDragSlider = -1;
   735:           if (SMN_WAVE_X <= x && x < SMN_WAVE_X + SMN_WAVE_WIDTH &&
   736:               SMN_WAVE_Y <= y && y < SMN_WAVE_Y + SMN_WAVE_HEIGHT) {  //波形
   737:             smnWaveDragOn = true;
   738:             smnWavePressedX = x;
   739:             smnWavePressedY = y;
   740:           } else if (SMN_PALET_X <= x && x < SMN_PALET_X + SMN_PALET_WIDTH &&
   741:                      SMN_PALET_Y <= y && y < SMN_PALET_Y + SMN_PALET_HEIGHT) {  //パレット
   742:             for (int n = 0; n < SMN_SLIDER_ARRAY.length; n++) {
   743:               int[] slider = SMN_SLIDER_ARRAY[n];
   744:               int x0 = slider[0];
   745:               int y0 = slider[1];
   746:               int max = slider[2];
   747:               int value = slider[4];
   748:               if (x0 <= x && x < x0 + max + 4 && y0 <= y && y < y0 + 5) {  //スライダの範囲内
   749:                 smnPaletDragSlider = n;
   750:                 smnPaletPressedX = x;
   751:                 if (x < x0 + value || x0 + value + 4 <= x) {  //つまみの範囲外
   752:                   slider[4] = Math.max (0, Math.min (max, x - x0));
   753:                   smnDrawPaletSlider ();
   754:                   smnUpdateColor ();
   755:                 }
   756:                 break;
   757:               }
   758:             }
   759:           }
   760:         }
   761:         @Override public void mouseReleased (MouseEvent me) {
   762:           smnWaveDragOn = false;
   763:           smnPaletDragSlider = -1;
   764:         }
   765:       });
   766: 
   767:     //マウスモーションリスナー
   768:     ComponentFactory.addListener (
   769:       smnPanel,
   770:       new MouseMotionAdapter () {
   771:         @Override public void mouseDragged (MouseEvent me) {
   772:           int x = me.getX ();
   773:           int y = me.getY ();
   774:           if (smnWaveDragOn) {  //波形のドラッグ中
   775:             int offset = Math.max (0, Math.min (smnWaveOffsetMax, smnWaveOffset - (x - smnWavePressedX)));
   776:             int spaceY = (SMN_WAVE_HEIGHT >> 1 << smnWaveScaleY) - (SMN_WAVE_HEIGHT >> 1);
   777:             int elevation = Math.max (-spaceY, Math.min (spaceY, smnWaveElevation - (y - smnWavePressedY)));
   778:             smnWavePressedX = x;
   779:             smnWavePressedY = y;
   780:             if (smnWaveOffset != offset || smnWaveElevation != elevation) {
   781:               smnWaveOffset = offset;
   782:               smnWaveElevation = elevation;
   783:               if (smnPauseUpdate) {  //一時停止中
   784:                 smnWavePaint ();
   785:                 smnPanel.repaint ();
   786:               }
   787:             }
   788:           } else if (smnPaletDragSlider >= 0) {  //パレットのスライダのドラッグ中
   789:             int[] slider = SMN_SLIDER_ARRAY[smnPaletDragSlider];
   790:             int max = slider[2];
   791:             int value = slider[4];
   792:             slider[4] = Math.max (0, Math.min (max, value + x - smnPaletPressedX));
   793:             smnPaletPressedX = x;
   794:             smnDrawPaletSlider ();
   795:             smnUpdateColor ();
   796:           }
   797:         }
   798:       });
   799: 
   800:     //ウインドウ
   801:     smnFrame = Multilingual.mlnTitle (
   802:       ComponentFactory.createRestorableSubFrame (
   803:         Settings.SGS_SMN_FRAME_KEY,
   804:         "Sound Monitor",
   805:         null,
   806:         smnPanel
   807:         ),
   808:       "ja", "音声モニタ");
   809:     ComponentFactory.addListener (
   810:       smnFrame,
   811:       new WindowAdapter () {
   812:         @Override public void windowClosing (WindowEvent we) {
   813:           smnIsVisible = false;
   814:         }
   815:       });
   816: 
   817:   }  //smnMakeFrame()
   818: 
   819:   //smnMakeImage ()
   820:   //  イメージを作る
   821:   public static void smnMakeImage () {
   822: 
   823:     //ビットマップ
   824:     smnMakeColorModel ();
   825:     smnImage = new BufferedImage (SMN_WIDTH, SMN_HEIGHT, BufferedImage.TYPE_BYTE_BINARY, smnColorModel);
   826:     byte[] bb = smnBitmap = ((DataBufferByte) smnImage.getRaster ().getDataBuffer ()).getData ();
   827: 
   828:     //波形
   829:     //アイコン
   830:     //smnDrawIcon1 (SMN_WAVE_X / 4 - 2, SMN_WAVE_Y, 8);
   831: 
   832:     //スペクトラムアナライザ
   833:     //アイコン
   834:     smnDrawIcon3 (SMN_SPECTRUM_X / 4, SMN_SPECTRUM_Y, 9);
   835: 
   836:     //440Hzマーク
   837:     smnDrawPattern (SMN_KEY_X + 56 * 4 + 43, SMN_KEY_Y, SMN_440HZ_PATTERN);
   838: 
   839:     //鍵盤と音色
   840:     for (int ch = 0; ch < 8; ch++) {
   841:       YM2151.OPMChannel channel = YM2151.opmChannel[ch];
   842:       //鍵盤
   843:       smnDrawIcon3 (SMN_KEY_X / 4 - 2, SMN_KEY_Y + 6 + 12 * ch, ch);  //鍵盤の左のチャンネルアイコン
   844:       smnKey[ch] = -1;
   845:       for (int o = 0, i = SMN_KEY_X / 4 + SMN_OFFSET * (SMN_KEY_Y + 6 + 12 * ch); ; o++, i += 14) {
   846:         smnDrawChar1 (SMN_KEY_X / 4 + 14 * o, SMN_KEY_Y, '0' + o);  //オクターブ
   847:         smnPaintKey (i     ,  0, SMN_KEY_COLOR[ 0]);  //C
   848:         smnPaintKey (i +  1,  1, SMN_KEY_COLOR[ 1]);  //C#
   849:         smnPaintKey (i +  2,  2, SMN_KEY_COLOR[ 2]);  //D
   850:         smnPaintKey (i +  3,  3, SMN_KEY_COLOR[ 3]);  //D#
   851:         smnPaintKey (i +  4,  4, SMN_KEY_COLOR[ 4]);  //E
   852:         if (o == 8) {
   853:           break;
   854:         }
   855:         smnPaintKey (i +  6,  5, SMN_KEY_COLOR[ 5]);  //F
   856:         smnPaintKey (i +  7,  6, SMN_KEY_COLOR[ 6]);  //F#
   857:         smnPaintKey (i +  8,  7, SMN_KEY_COLOR[ 7]);  //G
   858:         smnPaintKey (i +  9,  8, SMN_KEY_COLOR[ 8]);  //G#
   859:         smnPaintKey (i + 10,  9, SMN_KEY_COLOR[ 9]);  //A
   860:         smnPaintKey (i + 11, 10, SMN_KEY_COLOR[10]);  //A#
   861:         smnPaintKey (i + 12, 11, SMN_KEY_COLOR[11]);  //B
   862:       }
   863:       //音色
   864:       //                     1  1  1  2  2  2  3
   865:       //         0  3  6  9  2  5  8  1  4  7  0
   866:       //  0     FC SL WA SY SP PD AD PS AS PN
   867:       //  1     XX XX XX XX XX XX XX XX XX XX XX
   868:       //  2     AR 1R 2R RR 1L TL KS ML T1 T2 AE
   869:       //  3  M1 XX XX XX XX XX XX XX XX XX XX XX
   870:       //  4  C1 XX XX XX XX XX XX XX XX XX XX XX
   871:       //  5  M2 XX XX XX XX XX XX XX XX XX XX XX
   872:       //  6  C2 XX XX XX XX XX XX XX XX XX XX XX
   873:       {
   874:         int x = SMN_TONE_X / 4 + SMN_TONE_BOX_COLS * (0b01_00_10_01_00_10_01_00 >> (ch << 1) & 3);
   875:         int y = SMN_TONE_Y + SMN_TONE_BOX_HEIGHT * (0b10_10_01_01_01_00_00_00 >> (ch << 1) & 3);
   876:         smnDrawIcon3 (x, y, ch);  //音色の左上のチャンネルアイコン
   877:         smnDrawString1 (3 + x,         y, SMN_TONE_H1);  //FC SL WA SY SP PD AD PS AS PN
   878:         smnDrawString1 (3 + x, 6 * 2 + y, SMN_TONE_H2);  //AR 1R 2R RR 1L TL KS ML T1 T2 AE
   879:         smnDrawString1 (    x, 6 * 3 + y, SMN_TONE_M1);  //M1
   880:         smnDrawString1 (    x, 6 * 4 + y, SMN_TONE_C1);  //C1
   881:         smnDrawString1 (    x, 6 * 5 + y, SMN_TONE_M2);  //M2
   882:         smnDrawString1 (    x, 6 * 6 + y, SMN_TONE_C2);  //C2
   883:       }
   884:     }
   885: 
   886:     //PCM
   887:     smnDrawIcon3 (SMN_PCM_COL, SMN_PCM_Y, 10);  //アイコン
   888:     smnDrawString3 (SMN_PCM_COL +  5, SMN_PCM_Y    , "MHz");
   889:     smnDrawString3 (SMN_PCM_COL +  9, SMN_PCM_Y    , "1/");
   890:     smnDrawString3 (SMN_PCM_COL + 20, SMN_PCM_Y    , "kHz");
   891: 
   892:     //パレット
   893:     //            1111111111222222222233333
   894:     //  01234567890123456789012345678901234
   895:     //          H           S         B
   896:     //  0 hhhhhhhhhhhhh sssssssss bbbbbbbbb
   897:     //  1 hhhhhhhhhhhhh sssssssss bbbbbbbbb
   898:     //  2 hhhhhhhhhhhhh sssssssss bbbbbbbbb
   899:     //  3 hhhhhhhhhhhhh sssssssss bbbbbbbbb
   900:     smnDrawChar1 (SMN_PALET_X / 4 +  8, SMN_PALET_Y, 'H');
   901:     smnDrawChar1 (SMN_PALET_X / 4 + 20, SMN_PALET_Y, 'S');
   902:     smnDrawChar1 (SMN_PALET_X / 4 + 30, SMN_PALET_Y, 'B');
   903:     for (int p = 0; p < 4; p++) {
   904:       smnDrawChar1 (SMN_PALET_X / 4, SMN_PALET_Y + 6 + 6 * p, '0' + p);
   905:     }
   906:     smnDrawPaletSlider ();
   907: 
   908:   }  //smnMakeImage()
   909: 
   910:   //smnDrawPaletSlider ()
   911:   //  パレットのスライダを描く
   912:   public static void smnDrawPaletSlider () {
   913:     for (int p = 0; p < 4; p++) {
   914:       int[] pattern = SMN_SLIDER_PATTERN[p];
   915:       smnDrawSlider (SMN_SLIDER_ARRAY[3 * p    ], pattern);
   916:       smnDrawSlider (SMN_SLIDER_ARRAY[3 * p + 1], pattern);
   917:       smnDrawSlider (SMN_SLIDER_ARRAY[3 * p + 2], pattern);
   918:     }
   919:   }  //smnDrawPaletSlider()
   920: 
   921:   //smnMakeColorModel ()
   922:   //  SMN_SLIDER_ARRAYのvalueからsmnColorModelを作る
   923:   public static void smnMakeColorModel () {
   924:     byte[] r = new byte[4];
   925:     byte[] g = new byte[4];
   926:     byte[] b = new byte[4];
   927:     for (int p = 0; p < 4; p++) {
   928:       int rgb = Color.HSBtoRGB ((float) SMN_SLIDER_ARRAY[3 * p    ][4] / 48F,
   929:                                 (float) SMN_SLIDER_ARRAY[3 * p + 1][4] / 32F,
   930:                                 (float) SMN_SLIDER_ARRAY[3 * p + 2][4] / 32F);
   931:       if (false) {
   932:         System.out.printf ("%d 0x%08x %2d %2d %2d\n", p, rgb,
   933:                            SMN_SLIDER_ARRAY[3 * p    ][4],
   934:                            SMN_SLIDER_ARRAY[3 * p + 1][4],
   935:                            SMN_SLIDER_ARRAY[3 * p + 2][4]);
   936:       }
   937:       r[p] = (byte) (rgb >> 16);
   938:       g[p] = (byte) (rgb >>  8);
   939:       b[p] = (byte)  rgb;
   940:     }
   941:     smnColorModel = new IndexColorModel (2, 4, r, g, b);
   942:   }  //smnMakeColorModel()
   943: 
   944:   //smnUpdateColor ()
   945:   public static void smnUpdateColor () {
   946:     smnMakeColorModel ();
   947:     smnImage = new BufferedImage (smnColorModel, smnImage.getRaster(), false, null);
   948:     //smnBitmap = ((DataBufferByte) smnImage.getRaster ().getDataBuffer ()).getData ();
   949:     smnPanel.repaint ();
   950:   }  //smnUpdateColor()
   951: 
   952:   //smnUpdate ()
   953:   //  音声モニタを更新する
   954:   public static void smnUpdate () {
   955:     byte[] bb = smnBitmap;
   956: 
   957:     //一時停止の処理
   958:     if (smnPauseUpdate) {  //更新を一時停止している
   959:       if (smnPauseRequest) {  //一時停止の解除の要求はない
   960:         return;  //何もしない
   961:       }
   962:       smnPauseUpdate = false;  //一時停止を解除する
   963:       int i = SMN_PAUSE_X / 4 + SMN_OFFSET * SMN_PAUSE_Y;
   964:       int j = 0;
   965:       for (int v = 0; v < SMN_PAUSE_HEIGHT; v++) {
   966:         for (int u = 0; u < SMN_PAUSE_WIDTH; u += 4) {
   967:           bb[i++] = SMN_PAUSE_ICON_3[j++];
   968:         }
   969:         i += SMN_OFFSET - SMN_PAUSE_WIDTH / 4;
   970:       }
   971:     } else if (smnPauseRequest) {  //一時停止していないが一時停止の要求がある
   972:       smnPauseUpdate = true;  //一時停止する
   973:       int i = SMN_PAUSE_X / 4 + SMN_OFFSET * SMN_PAUSE_Y;
   974:       int j = 0;
   975:       for (int v = 0; v < SMN_PAUSE_HEIGHT; v++) {
   976:         for (int u = 0; u < SMN_PAUSE_WIDTH; u += 4) {
   977:           bb[i++] = SMN_PLAY_ICON_3[j++];
   978:         }
   979:         i += SMN_OFFSET - SMN_PAUSE_WIDTH / 4;
   980:       }
   981:       smnPanel.repaint ();
   982:       return;
   983:     }
   984: 
   985:     //波形
   986:     System.arraycopy (YM2151.opmBuffer, 0, smnOPMBuffer, 0, SoundSource.SND_CHANNELS * YM2151.OPM_BLOCK_SAMPLES);
   987:     System.arraycopy (ADPCM.pcmBuffer, 0, smnPCMBuffer, 0, SoundSource.SND_CHANNELS * YM2151.OPM_BLOCK_SAMPLES);
   988:     smnWavePaint ();
   989: 
   990:     //スペクトラムアナライザ
   991:     //
   992:     //  x座標と周波数の関係
   993:     //    |          1111111111222222222233333333334444444444555555|5
   994:     //    |01234567890123456789012345678901234567890123456789012345|6
   995:     //    |       1111122222333333344444445555566666777778888899999|9
   996:     //    |01235791357913579135678901235791357913579135791357912345|6
   997:     //    |   *   *   *   *   *       *   *   *   *   *   *   *    |
   998:     //    |WWW BBBBBBB BBBBBBB WWW WWW BBBBBBB BBBBBBB BBBBBBB WWW |
   999:     //    |WWW BBBBBBB BBBBBBB WWW WWW BBBBBBB BBBBBBB BBBBBBB WWW |
  1000:     //    |WWW BBBBBBB BBBBBBB WWW WWW BBBBBBB BBBBBBB BBBBBBB WWW |
  1001:     //    |WWW BBBBBBB BBBBBBB WWW WWW BBBBBBB BBBBBBB BBBBBBB WWW |
  1002:     //    |WWW BBBBBBB BBBBBBB WWW WWW BBBBBBB BBBBBBB BBBBBBB WWW |
  1003:     //    |WWW                 WWW WWW                         WWW |
  1004:     //    |WWWWWWW WWWWWWW WWWWWWW WWWWWWW WWWWWWW WWWWWWW WWWWWWW |
  1005:     //    |WWWWWWW WWWWWWW WWWWWWW WWWWWWW WWWWWWW WWWWWWW WWWWWWW |
  1006:     //    |WWWWWWW WWWWWWW WWWWWWW WWWWWWW WWWWWWW WWWWWWW WWWWWWW |
  1007:     //    |WWWWWWW WWWWWWW WWWWWWW WWWWWWW WWWWWWW WWWWWWW WWWWWWW |
  1008:     //    |WWWWWWW WWWWWWW WWWWWWW WWWWWWW WWWWWWW WWWWWWW WWWWWWW |
  1009:     //    鍵盤とスペクトラムアナライザの位置を合わせるため、x座標-8を56で割った商を96倍、余りをSMN_SPECTRUM_MAPで0~95に変換する
  1010:     //    O4Aのx=8+56*4+43=275の変換結果の96*4+75=459を440Hzに合わせる
  1011:     //    perl -e "@m=(0,1,2,3,5,7,9,11,13,15,17,19,21,23,25,27,29,31,33,35,36,37,38,39,40,41,42,43,45,47,49,51,53,55,57,59,61,63,65,67,69,71,73,75,77,79,81,83,85,87,89,91,92,93,94,95);for$x(0..479){$x%10 or print'    //    ';printf'|%4d%8.2f',$x,440*2**(((int(($x-8+56)/56)-1)*96+$m[($x-8+56)%56]-(96*4+75))/96);$x%10==9 and printf'|%c',10;}"
  1012:     //    |   0   14.78|   1   14.99|   2   15.21|   3   15.43|   4   15.55|   5   15.66|   6   15.77|   7   15.89|   8   16.00|   9   16.12|
  1013:     //    |  10   16.23|  11   16.35|  12   16.59|  13   16.83|  14   17.08|  15   17.32|  16   17.58|  17   17.83|  18   18.09|  19   18.35|
  1014:     //    |  20   18.62|  21   18.89|  22   19.17|  23   19.45|  24   19.73|  25   20.02|  26   20.31|  27   20.60|  28   20.75|  29   20.90|
  1015:     //    |  30   21.05|  31   21.21|  32   21.36|  33   21.51|  34   21.67|  35   21.83|  36   22.14|  37   22.47|  38   22.79|  39   23.12|
  1016:     //    |  40   23.46|  41   23.80|  42   24.15|  43   24.50|  44   24.86|  45   25.22|  46   25.58|  47   25.96|  48   26.33|  49   26.72|
  1017:     //    |  50   27.11|  51   27.50|  52   27.90|  53   28.31|  54   28.72|  55   29.14|  56   29.56|  57   29.99|  58   30.43|  59   30.87|
  1018:     //    |  60   31.09|  61   31.32|  62   31.54|  63   31.77|  64   32.00|  65   32.23|  66   32.47|  67   32.70|  68   33.18|  69   33.66|
  1019:     //    |  70   34.15|  71   34.65|  72   35.15|  73   35.66|  74   36.18|  75   36.71|  76   37.24|  77   37.78|  78   38.33|  79   38.89|
  1020:     //    |  80   39.46|  81   40.03|  82   40.61|  83   41.20|  84   41.50|  85   41.80|  86   42.11|  87   42.41|  88   42.72|  89   43.03|
  1021:     //    |  90   43.34|  91   43.65|  92   44.29|  93   44.93|  94   45.59|  95   46.25|  96   46.92|  97   47.60|  98   48.30|  99   49.00|
  1022:     //    | 100   49.71| 101   50.44| 102   51.17| 103   51.91| 104   52.67| 105   53.43| 106   54.21| 107   55.00| 108   55.80| 109   56.61|
  1023:     //    | 110   57.44| 111   58.27| 112   59.12| 113   59.98| 114   60.85| 115   61.74| 116   62.18| 117   62.63| 118   63.09| 119   63.54|
  1024:     //    | 120   64.00| 121   64.47| 122   64.94| 123   65.41| 124   66.36| 125   67.32| 126   68.30| 127   69.30| 128   70.30| 129   71.33|
  1025:     //    | 130   72.36| 131   73.42| 132   74.48| 133   75.57| 134   76.67| 135   77.78| 136   78.91| 137   80.06| 138   81.23| 139   82.41|
  1026:     //    | 140   83.00| 141   83.61| 142   84.21| 143   84.82| 144   85.44| 145   86.06| 146   86.68| 147   87.31| 148   88.58| 149   89.87|
  1027:     //    | 150   91.17| 151   92.50| 152   93.84| 153   95.21| 154   96.59| 155   98.00| 156   99.42| 157  100.87| 158  102.34| 159  103.83|
  1028:     //    | 160  105.34| 161  106.87| 162  108.42| 163  110.00| 164  111.60| 165  113.22| 166  114.87| 167  116.54| 168  118.24| 169  119.96|
  1029:     //    | 170  121.70| 171  123.47| 172  124.37| 173  125.27| 174  126.17| 175  127.09| 176  128.01| 177  128.94| 178  129.87| 179  130.81|
  1030:     //    | 180  132.72| 181  134.65| 182  136.60| 183  138.59| 184  140.61| 185  142.65| 186  144.73| 187  146.83| 188  148.97| 189  151.13|
  1031:     //    | 190  153.33| 191  155.56| 192  157.83| 193  160.12| 194  162.45| 195  164.81| 196  166.01| 197  167.21| 198  168.42| 199  169.64|
  1032:     //    | 200  170.87| 201  172.11| 202  173.36| 203  174.61| 204  177.15| 205  179.73| 206  182.34| 207  185.00| 208  187.69| 209  190.42|
  1033:     //    | 210  193.19| 211  196.00| 212  198.85| 213  201.74| 214  204.68| 215  207.65| 216  210.67| 217  213.74| 218  216.85| 219  220.00|
  1034:     //    | 220  223.20| 221  226.45| 222  229.74| 223  233.08| 224  236.47| 225  239.91| 226  243.40| 227  246.94| 228  248.73| 229  250.53|
  1035:     //    | 230  252.35| 231  254.18| 232  256.02| 233  257.87| 234  259.74| 235  261.63| 236  265.43| 237  269.29| 238  273.21| 239  277.18|
  1036:     //    | 240  281.21| 241  285.30| 242  289.45| 243  293.66| 244  297.94| 245  302.27| 246  306.67| 247  311.13| 248  315.65| 249  320.24|
  1037:     //    | 250  324.90| 251  329.63| 252  332.02| 253  334.42| 254  336.85| 255  339.29| 256  341.74| 257  344.22| 258  346.72| 259  349.23|
  1038:     //    | 260  354.31| 261  359.46| 262  364.69| 263  369.99| 264  375.38| 265  380.84| 266  386.38| 267  392.00| 268  397.70| 269  403.48|
  1039:     //    | 270  409.35| 271  415.30| 272  421.35| 273  427.47| 274  433.69| 275  440.00| 276  446.40| 277  452.89| 278  459.48| 279  466.16|
  1040:     //    | 280  472.94| 281  479.82| 282  486.80| 283  493.88| 284  497.46| 285  501.07| 286  504.70| 287  508.36| 288  512.04| 289  515.75|
  1041:     //    | 290  519.49| 291  523.25| 292  530.86| 293  538.58| 294  546.42| 295  554.37| 296  562.43| 297  570.61| 298  578.91| 299  587.33|
  1042:     //    | 300  595.87| 301  604.54| 302  613.33| 303  622.25| 304  631.30| 305  640.49| 306  649.80| 307  659.26| 308  664.03| 309  668.84|
  1043:     //    | 310  673.69| 311  678.57| 312  683.49| 313  688.44| 314  693.43| 315  698.46| 316  708.62| 317  718.92| 318  729.38| 319  739.99|
  1044:     //    | 320  750.75| 321  761.67| 322  772.75| 323  783.99| 324  795.39| 325  806.96| 326  818.70| 327  830.61| 328  842.69| 329  854.95|
  1045:     //    | 330  867.38| 331  880.00| 332  892.80| 333  905.79| 334  918.96| 335  932.33| 336  945.89| 337  959.65| 338  973.61| 339  987.77|
  1046:     //    | 340  994.92| 341 1002.13| 342 1009.40| 343 1016.71| 344 1024.08| 345 1031.50| 346 1038.97| 347 1046.50| 348 1061.72| 349 1077.17|
  1047:     //    | 350 1092.83| 351 1108.73| 352 1124.86| 353 1141.22| 354 1157.82| 355 1174.66| 356 1191.74| 357 1209.08| 358 1226.67| 359 1244.51|
  1048:     //    | 360 1262.61| 361 1280.97| 362 1299.61| 363 1318.51| 364 1328.06| 365 1337.69| 366 1347.38| 367 1357.15| 368 1366.98| 369 1376.89|
  1049:     //    | 370 1386.86| 371 1396.91| 372 1417.23| 373 1437.85| 374 1458.76| 375 1479.98| 376 1501.50| 377 1523.34| 378 1545.50| 379 1567.98|
  1050:     //    | 380 1590.79| 381 1613.93| 382 1637.40| 383 1661.22| 384 1685.38| 385 1709.90| 386 1734.77| 387 1760.00| 388 1785.60| 389 1811.57|
  1051:     //    | 390 1837.92| 391 1864.66| 392 1891.78| 393 1919.29| 394 1947.21| 395 1975.53| 396 1989.85| 397 2004.27| 398 2018.79| 399 2033.42|
  1052:     //    | 400 2048.16| 401 2063.00| 402 2077.95| 403 2093.00| 404 2123.45| 405 2154.33| 406 2185.67| 407 2217.46| 408 2249.71| 409 2282.44|
  1053:     //    | 410 2315.64| 411 2349.32| 412 2383.49| 413 2418.16| 414 2453.33| 415 2489.02| 416 2525.22| 417 2561.95| 418 2599.21| 419 2637.02|
  1054:     //    | 420 2656.13| 421 2675.38| 422 2694.76| 423 2714.29| 424 2733.96| 425 2753.77| 426 2773.73| 427 2793.83| 428 2834.46| 429 2875.69|
  1055:     //    | 430 2917.52| 431 2959.96| 432 3003.01| 433 3046.69| 434 3091.00| 435 3135.96| 436 3181.58| 437 3227.85| 438 3274.80| 439 3322.44|
  1056:     //    | 440 3370.76| 441 3419.79| 442 3469.53| 443 3520.00| 444 3571.20| 445 3623.14| 446 3675.84| 447 3729.31| 448 3783.55| 449 3838.59|
  1057:     //    | 450 3894.42| 451 3951.07| 452 3979.70| 453 4008.54| 454 4037.58| 455 4066.84| 456 4096.31| 457 4126.00| 458 4155.89| 459 4186.01|
  1058:     //    | 460 4246.90| 461 4308.67| 462 4371.34| 463 4434.92| 464 4499.43| 465 4564.88| 466 4631.27| 467 4698.64| 468 4766.98| 469 4836.32|
  1059:     //    | 470 4906.66| 471 4978.03| 472 5050.44| 473 5123.90| 474 5198.43| 475 5274.04| 476 5312.26| 477 5350.75| 478 5389.53| 479 5428.58|
  1060:     //
  1061:     //  周波数の範囲の分割
  1062:     //    周波数の範囲が広いので1回のFFTで全体を処理しようとすると端の方で大きな誤差または大きな無駄が生じる
  1063:     //    周波数の範囲を5分割してサンプリング間隔を変えて処理する
  1064:     //    分割数が少ないと区間毎のサンプリング期間の違いが大きくなり遅延時間にずれが生じて区間の境目に壁ができてしまう
  1065:     //    周波数の低い区間では完全に分解しようとすると大きな遅延が生じてしまうので分解能よりもレスポンスを優先する
  1066:     //
  1067:     //                                                サンプリング          更新サンプル数
  1068:     //             x座標          周波数         間隔   周波数   数    期間      /ブロック
  1069:     //      0,1    0..159    14.78Hz..103.83Hz    100    625Hz  1024  1.638s        25
  1070:     //       2   160..239   105.34Hz..277.18Hz     50   1250Hz  1024  0.819s        50
  1071:     //       3   240..319   281.21Hz..739.99Hz     25   2500Hz  1024  0.410s       100
  1072:     //       4   320..399   750.75Hz..2033.42Hz    10   6250Hz  1024  0.164s       250
  1073:     //       5   400..479  2048.16Hz..5428.58Hz     5  12500Hz  1024  0.082s       500
  1074:     //
  1075:     //  x座標とフーリエ変換の結果のインデックスの関係
  1076:     //
  1077:     //    perl -e "@m=(0,1,2,3,5,7,9,11,13,15,17,19,21,23,25,27,29,31,33,35,36,37,38,39,40,41,42,43,45,47,49,51,53,55,57,59,61,63,65,67,69,71,73,75,77,79,81,83,85,87,89,91,92,93,94,95);for$r([0,160,100],[160,240,50],[240,320,25],[320,400,10],[400,480,5]){for$x($r->[0]..$r->[1]){$x%10 or print'    //    ';printf'|%4d%4d',$x,int($r->[2]*1024/62500*440*2**(((int(($x-8+56)/56)-1)*96+$m[($x-8+56)%56]-(96*4+75))/96));$x%10==9 and printf'|%c',10;}printf'|%c',10;}"
  1078:     //    |   0  24|   1  24|   2  24|   3  25|   4  25|   5  25|   6  25|   7  26|   8  26|   9  26|
  1079:     //    |  10  26|  11  26|  12  27|  13  27|  14  27|  15  28|  16  28|  17  29|  18  29|  19  30|
  1080:     //    |  20  30|  21  30|  22  31|  23  31|  24  32|  25  32|  26  33|  27  33|  28  33|  29  34|
  1081:     //    |  30  34|  31  34|  32  34|  33  35|  34  35|  35  35|  36  36|  37  36|  38  37|  39  37|
  1082:     //    |  40  38|  41  38|  42  39|  43  40|  44  40|  45  41|  46  41|  47  42|  48  43|  49  43|
  1083:     //    |  50  44|  51  45|  52  45|  53  46|  54  47|  55  47|  56  48|  57  49|  58  49|  59  50|
  1084:     //    |  60  50|  61  51|  62  51|  63  52|  64  52|  65  52|  66  53|  67  53|  68  54|  69  55|
  1085:     //    |  70  55|  71  56|  72  57|  73  58|  74  59|  75  60|  76  61|  77  61|  78  62|  79  63|
  1086:     //    |  80  64|  81  65|  82  66|  83  67|  84  67|  85  68|  86  68|  87  69|  88  69|  89  70|
  1087:     //    |  90  71|  91  71|  92  72|  93  73|  94  74|  95  75|  96  76|  97  77|  98  79|  99  80|
  1088:     //    | 100  81| 101  82| 102  83| 103  85| 104  86| 105  87| 106  88| 107  90| 108  91| 109  92|
  1089:     //    | 110  94| 111  95| 112  96| 113  98| 114  99| 115 101| 116 101| 117 102| 118 103| 119 104|
  1090:     //    | 120 104| 121 105| 122 106| 123 107| 124 108| 125 110| 126 111| 127 113| 128 115| 129 116|
  1091:     //    | 130 118| 131 120| 132 122| 133 123| 134 125| 135 127| 136 129| 137 131| 138 133| 139 135|
  1092:     //    | 140 135| 141 136| 142 137| 143 138| 144 139| 145 140| 146 142| 147 143| 148 145| 149 147|
  1093:     //    | 150 149| 151 151| 152 153| 153 155| 154 158| 155 160| 156 162| 157 165| 158 167| 159 170|
  1094:     //    | 160 172|
  1095:     //    | 160  86| 161  87| 162  88| 163  90| 164  91| 165  92| 166  94| 167  95| 168  96| 169  98|
  1096:     //    | 170  99| 171 101| 172 101| 173 102| 174 103| 175 104| 176 104| 177 105| 178 106| 179 107|
  1097:     //    | 180 108| 181 110| 182 111| 183 113| 184 115| 185 116| 186 118| 187 120| 188 122| 189 123|
  1098:     //    | 190 125| 191 127| 192 129| 193 131| 194 133| 195 135| 196 135| 197 136| 198 137| 199 138|
  1099:     //    | 200 139| 201 140| 202 142| 203 143| 204 145| 205 147| 206 149| 207 151| 208 153| 209 155|
  1100:     //    | 210 158| 211 160| 212 162| 213 165| 214 167| 215 170| 216 172| 217 175| 218 177| 219 180|
  1101:     //    | 220 182| 221 185| 222 188| 223 190| 224 193| 225 196| 226 199| 227 202| 228 203| 229 205|
  1102:     //    | 230 206| 231 208| 232 209| 233 211| 234 212| 235 214| 236 217| 237 220| 238 223| 239 227|
  1103:     //    | 240 230|
  1104:     //    | 240 115| 241 116| 242 118| 243 120| 244 122| 245 123| 246 125| 247 127| 248 129| 249 131|
  1105:     //    | 250 133| 251 135| 252 135| 253 136| 254 137| 255 138| 256 139| 257 140| 258 142| 259 143|
  1106:     //    | 260 145| 261 147| 262 149| 263 151| 264 153| 265 155| 266 158| 267 160| 268 162| 269 165|
  1107:     //    | 270 167| 271 170| 272 172| 273 175| 274 177| 275 180| 276 182| 277 185| 278 188| 279 190|
  1108:     //    | 280 193| 281 196| 282 199| 283 202| 284 203| 285 205| 286 206| 287 208| 288 209| 289 211|
  1109:     //    | 290 212| 291 214| 292 217| 293 220| 294 223| 295 227| 296 230| 297 233| 298 237| 299 240|
  1110:     //    | 300 244| 301 247| 302 251| 303 254| 304 258| 305 262| 306 266| 307 270| 308 271| 309 273|
  1111:     //    | 310 275| 311 277| 312 279| 313 281| 314 284| 315 286| 316 290| 317 294| 318 298| 319 303|
  1112:     //    | 320 307|
  1113:     //    | 320 123| 321 124| 322 126| 323 128| 324 130| 325 132| 326 134| 327 136| 328 138| 329 140|
  1114:     //    | 330 142| 331 144| 332 146| 333 148| 334 150| 335 152| 336 154| 337 157| 338 159| 339 161|
  1115:     //    | 340 163| 341 164| 342 165| 343 166| 344 167| 345 169| 346 170| 347 171| 348 173| 349 176|
  1116:     //    | 350 179| 351 181| 352 184| 353 186| 354 189| 355 192| 356 195| 357 198| 358 200| 359 203|
  1117:     //    | 360 206| 361 209| 362 212| 363 216| 364 217| 365 219| 366 220| 367 222| 368 223| 369 225|
  1118:     //    | 370 227| 371 228| 372 232| 373 235| 374 239| 375 242| 376 246| 377 249| 378 253| 379 256|
  1119:     //    | 380 260| 381 264| 382 268| 383 272| 384 276| 385 280| 386 284| 387 288| 388 292| 389 296|
  1120:     //    | 390 301| 391 305| 392 309| 393 314| 394 319| 395 323| 396 326| 397 328| 398 330| 399 333|
  1121:     //    | 400 335|
  1122:     //    | 400 167| 401 169| 402 170| 403 171| 404 173| 405 176| 406 179| 407 181| 408 184| 409 186|
  1123:     //    | 410 189| 411 192| 412 195| 413 198| 414 200| 415 203| 416 206| 417 209| 418 212| 419 216|
  1124:     //    | 420 217| 421 219| 422 220| 423 222| 424 223| 425 225| 426 227| 427 228| 428 232| 429 235|
  1125:     //    | 430 239| 431 242| 432 246| 433 249| 434 253| 435 256| 436 260| 437 264| 438 268| 439 272|
  1126:     //    | 440 276| 441 280| 442 284| 443 288| 444 292| 445 296| 446 301| 447 305| 448 309| 449 314|
  1127:     //    | 450 319| 451 323| 452 326| 453 328| 454 330| 455 333| 456 335| 457 338| 458 340| 459 342|
  1128:     //    | 460 347| 461 352| 462 358| 463 363| 464 368| 465 373| 466 379| 467 384| 468 390| 469 396|
  1129:     //    | 470 401| 471 407| 472 413| 473 419| 474 425| 475 432| 476 435| 477 438| 478 441| 479 444|
  1130:     //    | 480 447|
  1131:     //
  1132:     //クリア
  1133:     //  毎回減った分だけ消しているので不要
  1134:     //Arrays.fill (bb, SMN_OFFSET * SMN_SPECTRUM_Y, SMN_OFFSET * (SMN_SPECTRUM_Y + SMN_SPECTRUM_HEIGHT), SMN_BLACK);
  1135:     //アイコン
  1136:     //smnDrawIcon3 (SMN_SPECTRUM_X / 4, SMN_SPECTRUM_Y, 9);
  1137:     //バッファを更新する
  1138:     double[] buffer0 = smnSpectrumBuffer[0];
  1139:     double[] buffer2 = smnSpectrumBuffer[2];
  1140:     double[] buffer3 = smnSpectrumBuffer[3];
  1141:     double[] buffer4 = smnSpectrumBuffer[4];
  1142:     double[] buffer5 = smnSpectrumBuffer[5];
  1143:     //  区間5
  1144:     System.arraycopy (buffer5, 500, buffer5, 0, SMN_SPECTRUM_N - 500);
  1145:     if (SoundSource.SND_CHANNELS == 1) {  //モノラル
  1146:       for (int i = SMN_SPECTRUM_N - 500, k = 0; i < SMN_SPECTRUM_N; i++, k += 5) {
  1147:         buffer5[i] = (double) (YM2151.opmBuffer[k    ] + ADPCM.pcmBuffer[k    ] +
  1148:                                YM2151.opmBuffer[k + 1] + ADPCM.pcmBuffer[k + 1] +
  1149:                                YM2151.opmBuffer[k + 2] + ADPCM.pcmBuffer[k + 2] +
  1150:                                YM2151.opmBuffer[k + 3] + ADPCM.pcmBuffer[k + 3] +
  1151:                                YM2151.opmBuffer[k + 4] + ADPCM.pcmBuffer[k + 4]) * (0.2 / 32768.0);
  1152:       }
  1153:     } else {  //ステレオ
  1154:       for (int i = SMN_SPECTRUM_N - 500, k = 0; i < SMN_SPECTRUM_N; i++, k += 10) {
  1155:         buffer5[i] = (double) (YM2151.opmBuffer[k    ] + YM2151.opmBuffer[k + 1] + ADPCM.pcmBuffer[k    ] + ADPCM.pcmBuffer[k + 1] +
  1156:                                YM2151.opmBuffer[k + 2] + YM2151.opmBuffer[k + 3] + ADPCM.pcmBuffer[k + 2] + ADPCM.pcmBuffer[k + 3] +
  1157:                                YM2151.opmBuffer[k + 4] + YM2151.opmBuffer[k + 5] + ADPCM.pcmBuffer[k + 4] + ADPCM.pcmBuffer[k + 5] +
  1158:                                YM2151.opmBuffer[k + 6] + YM2151.opmBuffer[k + 7] + ADPCM.pcmBuffer[k + 6] + ADPCM.pcmBuffer[k + 7] +
  1159:                                YM2151.opmBuffer[k + 8] + YM2151.opmBuffer[k + 9] + ADPCM.pcmBuffer[k + 8] + ADPCM.pcmBuffer[k + 9]) * (0.1 / 32768.0);
  1160:       }
  1161:     }
  1162:     //  区間4
  1163:     System.arraycopy (buffer4, 250, buffer4, 0, SMN_SPECTRUM_N - 250);
  1164:     for (int i = SMN_SPECTRUM_N - 250, k = SMN_SPECTRUM_N - 500; i < SMN_SPECTRUM_N; i++, k += 2) {
  1165:       buffer4[i] = (buffer5[k] + buffer5[k + 1]) * 0.5;
  1166:     }
  1167:     //  区間3
  1168:     System.arraycopy (buffer3, 100, buffer3, 0, SMN_SPECTRUM_N - 100);
  1169:     for (int i = SMN_SPECTRUM_N - 100, k = SMN_SPECTRUM_N - 500; i < SMN_SPECTRUM_N; i++, k += 5) {
  1170:       buffer3[i] = (buffer5[k] + buffer5[k + 1] + buffer5[k + 2] + buffer5[k + 3] + buffer5[k + 4]) * 0.2;
  1171:     }
  1172:     //  区間2
  1173:     System.arraycopy (buffer2, 50, buffer2, 0, SMN_SPECTRUM_N - 50);
  1174:     for (int i = SMN_SPECTRUM_N - 50, k = SMN_SPECTRUM_N - 100; i < SMN_SPECTRUM_N; i++, k += 2) {
  1175:       buffer2[i] = (buffer3[k] + buffer3[k + 1]) * 0.5;
  1176:     }
  1177:     //  区間0
  1178:     System.arraycopy (buffer0, 25, buffer0, 0, SMN_SPECTRUM_N - 25);
  1179:     for (int i = SMN_SPECTRUM_N - 25, k = SMN_SPECTRUM_N - 50; i < SMN_SPECTRUM_N; i++, k += 2) {
  1180:       buffer0[i] = (buffer2[k] + buffer2[k + 1]) * 0.5;
  1181:     }
  1182:     for (int partition = 0; partition < SMN_SPECTRUM_PARTITIONS; partition++) {
  1183:       if (partition != 1) {  //区間1は区間0の結果を使う
  1184:         //実数部
  1185:         double[] buffer = smnSpectrumBuffer[partition];
  1186:         for (int i = 0; i < SMN_SPECTRUM_N; i++) {
  1187:           smnSpectrumX[i] = smnSpectrumWindow[i] * buffer[i];
  1188:         }
  1189:         //虚数部
  1190:         Arrays.fill (smnSpectrumY, 0, SMN_SPECTRUM_N, 0.0);
  1191:         //FFTを呼び出す
  1192:         //smnFFT.fftSandeTukey2 (smnSpectrumX, smnSpectrumY);  //基数2
  1193:         smnFFT.fftSandeTukey4 (smnSpectrumX, smnSpectrumY);  //基数4
  1194:       }
  1195:       //グラフを描く
  1196:       //  区間0はx=8から描く
  1197:       for (int x = partition == 0 ? 8 : SMN_SPECTRUM_RANGE * partition, x1 = x + SMN_SPECTRUM_RANGE, b = 6; x < x1; x++, b = b - 2 & 6) {  //b=6,4,2,0
  1198:         int k = smnSpectrumIndex[x];
  1199:         int n = k >> 16;
  1200:         k = (char) k;
  1201:         double level = smnSpectrumX[k];
  1202:         while (--n > 0) {
  1203:           level = Math.max (level, smnSpectrumX[++k]);
  1204:         }
  1205:         int value = Math.max (0, Math.min (SMN_SPECTRUM_HEIGHT - 1, (int) ((double) SMN_SPECTRUM_HEIGHT / (double) SMN_SPECTRUM_N * SMN_SPECTRUM_SCALE * level)));  //今回の値。0=最小,SMN_SPECTRUM_HEIGHT-1=最大
  1206:         int value0 = smnSpectrumValue[x];  //前回の値
  1207:         if (value > value0) {  //増えたとき
  1208:           smnSpectrumValue[x] = value;
  1209:           int i = SMN_OFFSET * (SMN_SPECTRUM_Y + SMN_SPECTRUM_HEIGHT - 1 - value) + SMN_SPECTRUM_X / 4 + (x >> 2);
  1210:           while (value > value0) {
  1211:             bb[i] = (byte) (bb[i] & ~(3 << b) | (SMN_WHITE & 3) << b);
  1212:             i += SMN_OFFSET;
  1213:             value--;
  1214:           }
  1215:         } else if (value < value0) {  //減ったとき
  1216:           if (value < value0 - SMN_SPECTRUM_TRAIL) {
  1217:             value = value0 - SMN_SPECTRUM_TRAIL;
  1218:           }
  1219:           smnSpectrumValue[x] = value;
  1220:           int i = SMN_OFFSET * (SMN_SPECTRUM_Y + SMN_SPECTRUM_HEIGHT - 1 - value0) + SMN_SPECTRUM_X / 4 + (x >> 2);
  1221:           while (value < value0) {
  1222:             bb[i] = (byte) (bb[i] & ~(3 << b));
  1223:             i += SMN_OFFSET;
  1224:             value++;
  1225:           }
  1226:         }
  1227:       }  //for i
  1228:     }  //for partition
  1229: 
  1230:     //OPM
  1231:     for (int ch = 0; ch < 8; ch++) {
  1232:       YM2151.OPMChannel channel = YM2151.opmChannel[ch];
  1233:       //鍵盤
  1234:       int k0 = smnKey[ch];  //前回キーオンされていたかリリース中だったキー。0x10000+キー=リリース中。-1=なし
  1235:       //  -=ATTACK/DECAY/SUSTAIN,0=RELEASE,+=SILENCE
  1236:       int m1 = channel.m1.slStage.ordinal () - YM2151.OPMStage.RELEASE.ordinal ();
  1237:       int m2 = channel.m2.slStage.ordinal () - YM2151.OPMStage.RELEASE.ordinal ();
  1238:       int c1 = channel.c1.slStage.ordinal () - YM2151.OPMStage.RELEASE.ordinal ();
  1239:       int c2 = channel.c2.slStage.ordinal () - YM2151.OPMStage.RELEASE.ordinal ();
  1240:       int k1;  //今回キーオンされているかリリース中のキー。0x10000+キー=リリース中。-1=なし
  1241:       if (m1 > 0 && m2 > 0 && c1 > 0 && c2 > 0) {  //すべてSILENCE。キーオフ
  1242:         k1 = -1;
  1243:       } else {
  1244:         //  KC=74のときKF=5.5くらいで440Hzになるので、KFが38以上のときKCを1増やす
  1245:         k1 = channel.chKeyIndex + ((3 << 6) + (64 - 38) - 768) >> 6;  //今回キーオンされているか最後にキーオンされたキー
  1246:         if (m1 >= 0 && m2 >= 0 && c1 >= 0 && c2 >= 0) {  //1個以上のRELEASEと0個以上のSILENCE。リリース中
  1247:           k1 |= 0x10000;
  1248:         }
  1249:       }
  1250:       if (k0 != k1) {  //キーオンされているキーまたはキーの状態が変わった
  1251:         int i = SMN_KEY_X / 4 + SMN_OFFSET * (SMN_KEY_Y + 6 + 12 * ch);  //鍵盤の左端
  1252:         if (k0 >= 0 && (short) k0 != (short) k1) {  //前回キーオンされていたかリリース中だったキーがあって、キーが変わった
  1253:           //perl optdiv.pl 97 12
  1254:           //  x/12==x*43>>>9 (0<=x<=130) [97*43==4171]
  1255:           int kk = (short) k0;
  1256:           int o = kk * 43 >>> 9;  //オクターブ
  1257:           kk -= 12 * o;  //鍵の番号。0..11
  1258:           smnPaintKey (i + 14 * o + (kk < 5 ? kk : kk + 1), kk, SMN_KEY_COLOR[kk]);
  1259:         }
  1260:         if (k1 >= 0 && k0 != k1) {  //今回キーオンされているかリリース中のキーがあって、キーまたはキーの状態が変わった
  1261:           //perl optdiv.pl 97 12
  1262:           //  x/12==x*43>>>9 (0<=x<=130) [97*43==4171]
  1263:           int kk = (short) k1;
  1264:           int o = kk * 43 >>> 9;  //オクターブ
  1265:           kk -= 12 * o;  //鍵の番号。0..11
  1266:           if (k1 >> 16 == 0) {  //キーオンされている
  1267:             smnPaintKey (i + 14 * o + (kk < 5 ? kk : kk + 1), kk, SMN_ORANGE);
  1268:           } else {  //リリース中
  1269:             smnPaintKey2 (i + 14 * o + (kk < 5 ? kk : kk + 1), kk, SMN_KEY_COLOR[kk], SMN_ORANGE);
  1270:           }
  1271:         }
  1272:         smnKey[ch] = k1;
  1273:       }
  1274:       //音色
  1275:       {
  1276:         XEiJ.fmtHex2 (SMN_TONE_V0,  0, channel.chFLCON);  //FLCON
  1277:         XEiJ.fmtHex2 (SMN_TONE_V0,  3, channel.chSLOT);  //SLOT
  1278:         XEiJ.fmtHex2 (SMN_TONE_V0,  6, YM2151.opmWAVE256 >> 8);  //WAVE
  1279:         XEiJ.fmtHex2 (SMN_TONE_V0,  9, channel.chSYNC ? 1 : 0);  //SYNC
  1280:         XEiJ.fmtHex2 (SMN_TONE_V0, 12, 18 - Integer.numberOfTrailingZeros (YM2151.opmLFOPeriodMinor) << 4 | YM2151.opmLFOPeriodMajor - 16);  //SPEED
  1281:         XEiJ.fmtHex2 (SMN_TONE_V0, 15, YM2151.opmPMD);  //PMD
  1282:         XEiJ.fmtHex2 (SMN_TONE_V0, 18, YM2151.opmAMD);  //AMD
  1283:         XEiJ.fmtHex2 (SMN_TONE_V0, 21, channel.chPMS);  //PMS
  1284:         XEiJ.fmtHex2 (SMN_TONE_V0, 24, channel.chAMS);  //AMS
  1285:         XEiJ.fmtHex2 (SMN_TONE_V0, 27, channel.chPAN);  //PAN
  1286:         XEiJ.fmtHex2 (SMN_TONE_V1,  0, Math.max (0, channel.m1.slAttackRate - 32) >> 1);  //AR
  1287:         XEiJ.fmtHex2 (SMN_TONE_V2,  0, Math.max (0, channel.c1.slAttackRate - 32) >> 1);  //AR
  1288:         XEiJ.fmtHex2 (SMN_TONE_V3,  0, Math.max (0, channel.m2.slAttackRate - 32) >> 1);  //AR
  1289:         XEiJ.fmtHex2 (SMN_TONE_V4,  0, Math.max (0, channel.c2.slAttackRate - 32) >> 1);  //AR
  1290:         XEiJ.fmtHex2 (SMN_TONE_V1,  3, Math.max (0, channel.m1.slDecayRate - 32) >> 1);  //D1R
  1291:         XEiJ.fmtHex2 (SMN_TONE_V2,  3, Math.max (0, channel.c1.slDecayRate - 32) >> 1);  //D1R
  1292:         XEiJ.fmtHex2 (SMN_TONE_V3,  3, Math.max (0, channel.m2.slDecayRate - 32) >> 1);  //D1R
  1293:         XEiJ.fmtHex2 (SMN_TONE_V4,  3, Math.max (0, channel.c2.slDecayRate - 32) >> 1);  //D1R
  1294:         XEiJ.fmtHex2 (SMN_TONE_V1,  6, Math.max (0, channel.m1.slSustainRate - 32) >> 1);  //D2R
  1295:         XEiJ.fmtHex2 (SMN_TONE_V2,  6, Math.max (0, channel.c1.slSustainRate - 32) >> 1);  //D2R
  1296:         XEiJ.fmtHex2 (SMN_TONE_V3,  6, Math.max (0, channel.m2.slSustainRate - 32) >> 1);  //D2R
  1297:         XEiJ.fmtHex2 (SMN_TONE_V4,  6, Math.max (0, channel.c2.slSustainRate - 32) >> 1);  //D2R
  1298:         XEiJ.fmtHex2 (SMN_TONE_V1,  9, channel.m1.slReleaseRate - 34 >> 2);  //RR
  1299:         XEiJ.fmtHex2 (SMN_TONE_V2,  9, channel.c1.slReleaseRate - 34 >> 2);  //RR
  1300:         XEiJ.fmtHex2 (SMN_TONE_V3,  9, channel.m2.slReleaseRate - 34 >> 2);  //RR
  1301:         XEiJ.fmtHex2 (SMN_TONE_V4,  9, channel.c2.slReleaseRate - 34 >> 2);  //RR
  1302:         XEiJ.fmtHex2 (SMN_TONE_V1, 12, channel.m1.slDecayLevel >> YM2151.OPM_TL_BITS - 5 & 15);  //D1L
  1303:         XEiJ.fmtHex2 (SMN_TONE_V2, 12, channel.c1.slDecayLevel >> YM2151.OPM_TL_BITS - 5 & 15);  //D1L
  1304:         XEiJ.fmtHex2 (SMN_TONE_V3, 12, channel.m2.slDecayLevel >> YM2151.OPM_TL_BITS - 5 & 15);  //D1L
  1305:         XEiJ.fmtHex2 (SMN_TONE_V4, 12, channel.c2.slDecayLevel >> YM2151.OPM_TL_BITS - 5 & 15);  //D1L
  1306:         XEiJ.fmtHex2 (SMN_TONE_V1, 15, channel.m1.slTotalLevel >> YM2151.OPM_TL_BITS - 7);  //TL
  1307:         XEiJ.fmtHex2 (SMN_TONE_V2, 15, channel.c1.slTotalLevel >> YM2151.OPM_TL_BITS - 7);  //TL
  1308:         XEiJ.fmtHex2 (SMN_TONE_V3, 15, channel.m2.slTotalLevel >> YM2151.OPM_TL_BITS - 7);  //TL
  1309:         XEiJ.fmtHex2 (SMN_TONE_V4, 15, channel.c2.slTotalLevel >> YM2151.OPM_TL_BITS - 7);  //TL
  1310:         XEiJ.fmtHex2 (SMN_TONE_V1, 18, 5 - channel.m1.slKeyScale);  //KS
  1311:         XEiJ.fmtHex2 (SMN_TONE_V2, 18, 5 - channel.c1.slKeyScale);  //KS
  1312:         XEiJ.fmtHex2 (SMN_TONE_V3, 18, 5 - channel.m2.slKeyScale);  //KS
  1313:         XEiJ.fmtHex2 (SMN_TONE_V4, 18, 5 - channel.c2.slKeyScale);  //KS
  1314:         XEiJ.fmtHex2 (SMN_TONE_V1, 21, channel.m1.slMultiply >> 1);  //MUL
  1315:         XEiJ.fmtHex2 (SMN_TONE_V2, 21, channel.c1.slMultiply >> 1);  //MUL
  1316:         XEiJ.fmtHex2 (SMN_TONE_V3, 21, channel.m2.slMultiply >> 1);  //MUL
  1317:         XEiJ.fmtHex2 (SMN_TONE_V4, 21, channel.c2.slMultiply >> 1);  //MUL
  1318:         XEiJ.fmtHex2 (SMN_TONE_V1, 24, channel.m1.slDetune1Page >> 5);  //DT1
  1319:         XEiJ.fmtHex2 (SMN_TONE_V2, 24, channel.c1.slDetune1Page >> 5);  //DT1
  1320:         XEiJ.fmtHex2 (SMN_TONE_V3, 24, channel.m2.slDetune1Page >> 5);  //DT1
  1321:         XEiJ.fmtHex2 (SMN_TONE_V4, 24, channel.c2.slDetune1Page >> 5);  //DT1
  1322:         //perl -e "for$n(0,384,500,608){printf'%d ',$n*15>>7&3}"
  1323:         XEiJ.fmtHex2 (SMN_TONE_V1, 27, channel.m1.slDetune2Depth * 15 >> 7 & 3);  //DT2
  1324:         XEiJ.fmtHex2 (SMN_TONE_V2, 27, channel.c1.slDetune2Depth * 15 >> 7 & 3);  //DT2
  1325:         XEiJ.fmtHex2 (SMN_TONE_V3, 27, channel.m2.slDetune2Depth * 15 >> 7 & 3);  //DT2
  1326:         XEiJ.fmtHex2 (SMN_TONE_V4, 27, channel.c2.slDetune2Depth * 15 >> 7 & 3);  //DT2
  1327:         XEiJ.fmtHex2 (SMN_TONE_V1, 30, channel.m1.slAMSMask & 1);  //AMSEN
  1328:         XEiJ.fmtHex2 (SMN_TONE_V2, 30, channel.c1.slAMSMask & 1);  //AMSEN
  1329:         XEiJ.fmtHex2 (SMN_TONE_V3, 30, channel.m2.slAMSMask & 1);  //AMSEN
  1330:         XEiJ.fmtHex2 (SMN_TONE_V4, 30, channel.c2.slAMSMask & 1);  //AMSEN
  1331:         int x = SMN_TONE_X / 4 + SMN_TONE_BOX_COLS * (0b01_00_10_01_00_10_01_00 >> (ch << 1) & 3);
  1332:         int y = SMN_TONE_Y + SMN_TONE_BOX_HEIGHT * (0b10_10_01_01_01_00_00_00 >> (ch << 1) & 3);
  1333:         smnDrawString3 (3 + x, 6     + y, SMN_TONE_V0);
  1334:         smnDrawString3 (3 + x, 6 * 3 + y, SMN_TONE_V1);
  1335:         smnDrawString3 (3 + x, 6 * 4 + y, SMN_TONE_V2);
  1336:         smnDrawString3 (3 + x, 6 * 5 + y, SMN_TONE_V3);
  1337:         smnDrawString3 (3 + x, 6 * 6 + y, SMN_TONE_V4);
  1338:       }
  1339:     }  //for ch
  1340: 
  1341:     //PCM
  1342:     smnDrawString3 (SMN_PCM_COL +  3, SMN_PCM_Y    , SMN_PCM_OSCILLATOR[ADPCM.pcmOSCFreqMode << 1 | ADPCM.pcmOscillator]);
  1343:     smnDrawString3 (SMN_PCM_COL + 11, SMN_PCM_Y    , SMN_PCM_DIVIDER[ADPCM.pcmDivider]);
  1344:     smnDrawString3 (SMN_PCM_COL + 16, SMN_PCM_Y    , SMN_PCM_FREQ[ADPCM.pcmOSCFreqMode << 3 | ADPCM.pcmOscillator << 2 | ADPCM.pcmDivider]);
  1345:     smnDrawString2 (SMN_PCM_COL +  3, SMN_PCM_Y + 6, SMN_PCM_PLAY[ADPCM.pcmActive ? 1 : 0]);
  1346:     smnDrawString2 (SMN_PCM_COL +  8, SMN_PCM_Y + 6, SMN_PCM_DATA[ADPCM.pcmEncodedData >= 0 ? 1 : 0]);
  1347:     smnDrawString2 (SMN_PCM_COL + 13, SMN_PCM_Y + 6, SMN_PCM_LEFT[ADPCM.pcmPanLeft == 0 || ADPCM.pcmPanLeft < 0x80000000 + ADPCM.PCM_ATTACK_SPAN * 2 ? 0 : 1]);
  1348:     smnDrawString2 (SMN_PCM_COL + 18, SMN_PCM_Y + 6, SMN_PCM_RIGHT[SoundSource.SND_CHANNELS == 1 ?
  1349:                                                                    ADPCM.pcmPanLeft == 0 || ADPCM.pcmPanLeft < 0x80000000 + ADPCM.PCM_ATTACK_SPAN * 2 ? 0 : 1 :
  1350:                                                                    ADPCM.pcmPanRight == 0 || ADPCM.pcmPanRight < 0x80000000 + ADPCM.PCM_ATTACK_SPAN * 2 ? 0 : 1]);
  1351: 
  1352:     smnPanel.repaint ();
  1353: 
  1354:   }  //smnUpdate()
  1355: 
  1356:   //smnWavePaint ()
  1357:   //  波形を描く
  1358:   public static void smnWavePaint () {
  1359:     byte[] bb = smnBitmap;
  1360:     //クリア
  1361:     Arrays.fill (bb, SMN_OFFSET * SMN_WAVE_Y, SMN_OFFSET * (SMN_WAVE_Y + SMN_WAVE_HEIGHT), SMN_BLACK);
  1362:     //アイコン
  1363:     smnDrawIcon3 (SMN_WAVE_X / 4 - 2, SMN_WAVE_Y, 8);
  1364:     //時間軸
  1365:     if (-SMN_WAVE_HEIGHT / 2 <= smnWaveElevation && smnWaveElevation <= SMN_WAVE_HEIGHT / 2) {
  1366:       int i = SMN_OFFSET * (SMN_WAVE_Y + SMN_WAVE_HEIGHT / 2 - smnWaveElevation) + SMN_WAVE_X / 4;
  1367:       Arrays.fill (bb, i, i + SMN_WAVE_WIDTH / 4, SMN_BLUE);
  1368:     }
  1369:     //時間目盛り
  1370:     int ms0 = (1000 / SoundSource.SND_BLOCK_FREQ * smnWaveOffset + (SMN_WAVE_WIDTH << smnWaveScaleX) - 1) / (SMN_WAVE_WIDTH << smnWaveScaleX);
  1371:     int ms1 = 1000 / SoundSource.SND_BLOCK_FREQ * (smnWaveOffset + SMN_WAVE_WIDTH) / (SMN_WAVE_WIDTH << smnWaveScaleX);
  1372:     for (int ms = ms0; ms <= ms1; ms++) {
  1373:       //  perl optdiv.pl 11800 1000
  1374:       //  x/1000==x*8389>>>23 (0<=x<=21998) [11800*8389==98990200]
  1375:       //int x = SoundSource.SND_BLOCK_FREQ * SMN_WAVE_WIDTH * ms / 1000;
  1376:       int x = Math.max (0, Math.min (SMN_WAVE_WIDTH - 1, (SoundSource.SND_BLOCK_FREQ * SMN_WAVE_WIDTH * ms * 8389 >>> 23 << smnWaveScaleX) - smnWaveOffset));
  1377:       int b = (~x & 3) << 1;  //0→6,1→4,2→2,3→0
  1378:       if (ms1 - ms0 <= 10 ||
  1379:           ms1 - ms0 <= 20 && (1L << 63 -  0 |
  1380:                               1L << 63 -  5 |
  1381:                               1L << 63 - 10 |
  1382:                               1L << 63 - 15 |
  1383:                               1L << 63 - 20 |
  1384:                               1L << 63 - 25 |
  1385:                               1L << 63 - 30 |
  1386:                               1L << 63 - 35 |
  1387:                               1L << 63 - 40 |
  1388:                               1L << 63 - 45 |
  1389:                               1L << 63 - 50 |
  1390:                               1L << 63 - 55 |
  1391:                               1L << 63 - 60) << ms < 0 ||
  1392:           (1L << 63 -  0 |
  1393:            1L << 63 - 10 |
  1394:            1L << 63 - 20 |
  1395:            1L << 63 - 30 |
  1396:            1L << 63 - 40 |
  1397:            1L << 63 - 50 |
  1398:            1L << 63 - 60) << ms < 0) {
  1399:         int t = XEiJ.FMT_BCD4[ms];
  1400:         int col = SMN_WAVE_X / 4 + Math.max (0, Math.min (SMN_WAVE_WIDTH / 4 - (t >= 0x10 ? 4 : 3), (x + 3 >> 2) - (t >= 0x10 ? 2 : 1)));
  1401:         if (t >= 0x10) {
  1402:           smnDrawChar1 (col++, SMN_WAVE_Y + SMN_WAVE_HEIGHT - 6, '0' | t >> 4);
  1403:         }
  1404:         smnDrawChar1 (col    , SMN_WAVE_Y + SMN_WAVE_HEIGHT - 6, '0' | t & 15);
  1405:         smnDrawChar1 (col + 1, SMN_WAVE_Y + SMN_WAVE_HEIGHT - 6, 'm');
  1406:         smnDrawChar1 (col + 2, SMN_WAVE_Y + SMN_WAVE_HEIGHT - 6, 's');
  1407:       }
  1408:       int v0, v1;
  1409:       if ((1L << 63 -  0 |
  1410:            1L << 63 - 10 |
  1411:            1L << 63 - 20 |
  1412:            1L << 63 - 30 |
  1413:            1L << 63 - 40 |
  1414:            1L << 63 - 50 |
  1415:            1L << 63 - 60) << ms < 0) {
  1416:         v0 = SMN_WAVE_HEIGHT / 2 - SMN_WAVE_HEIGHT * 4 / 16;  //0
  1417:         v1 = SMN_WAVE_HEIGHT / 2 + SMN_WAVE_HEIGHT * 4 / 16;  //SMN_WAVE_HEIGHT-1
  1418:       } else if ((1L << 63 -  0 |
  1419:                   1L << 63 -  5 |
  1420:                   1L << 63 - 10 |
  1421:                   1L << 63 - 15 |
  1422:                   1L << 63 - 20 |
  1423:                   1L << 63 - 25 |
  1424:                   1L << 63 - 30 |
  1425:                   1L << 63 - 35 |
  1426:                   1L << 63 - 40 |
  1427:                   1L << 63 - 45 |
  1428:                   1L << 63 - 50 |
  1429:                   1L << 63 - 55 |
  1430:                   1L << 63 - 60) << ms < 0) {
  1431:         v0 = SMN_WAVE_HEIGHT / 2 - SMN_WAVE_HEIGHT * 3 / 16;
  1432:         v1 = SMN_WAVE_HEIGHT / 2 + SMN_WAVE_HEIGHT * 3 / 16;
  1433:       } else {
  1434:         v0 = SMN_WAVE_HEIGHT / 2 - SMN_WAVE_HEIGHT * 2 / 16;
  1435:         v1 = SMN_WAVE_HEIGHT / 2 + SMN_WAVE_HEIGHT * 2 / 16;
  1436:       }
  1437:       v0 -= smnWaveElevation;
  1438:       v1 -= smnWaveElevation;
  1439:       if (v0 < 0) {
  1440:         v0 = 0;
  1441:       }
  1442:       if (v1 > SMN_WAVE_HEIGHT - 1) {
  1443:         v1 = SMN_WAVE_HEIGHT - 1;
  1444:       }
  1445:       int i = SMN_OFFSET * (SMN_WAVE_Y + v0) + SMN_WAVE_X / 4 + (x >> 2);
  1446:       for (int v = v0; v <= v1; v++) {
  1447:         bb[i] = (byte) (bb[i] & ~(3 << b) | (SMN_BLUE & 3) << b);
  1448:         i += SMN_OFFSET;
  1449:       }
  1450:     }
  1451:     if (SoundSource.SND_CHANNELS == 1) {  //モノラル
  1452:       smnWavePaint1 (smnPCMBuffer, smnWaveIndex0[smnWaveScaleX], SMN_ORANGE & 3, smnWaveLastYPCMLeft);  //PCM
  1453:       smnWavePaint1 (smnOPMBuffer, smnWaveIndex0[smnWaveScaleX], SMN_WHITE & 3, smnWaveLastYOPMLeft);  //OPM
  1454:     } else {  //ステレオ
  1455:       smnWavePaint1 (smnPCMBuffer, smnWaveIndex0[smnWaveScaleX], SMN_ORANGE & 3, smnWaveLastYPCMLeft);  //PCM left
  1456:       smnWavePaint1 (smnPCMBuffer, smnWaveIndex1[smnWaveScaleX], SMN_ORANGE & 3, smnWaveLastYPCMRight);  //PCM right
  1457:       smnWavePaint1 (smnOPMBuffer, smnWaveIndex0[smnWaveScaleX], SMN_WHITE & 3, smnWaveLastYOPMLeft);  //OPM left
  1458:       smnWavePaint1 (smnOPMBuffer, smnWaveIndex1[smnWaveScaleX], SMN_WHITE & 3, smnWaveLastYOPMRight);  //OPM right
  1459:     }
  1460:   }  //smnWavePaint()
  1461: 
  1462:   //smnWavePaint1 (buffer, index, palet, lastY)
  1463:   public static void smnWavePaint1 (int[] buffer, int[] index, int palet, int[] lastY) {
  1464:     byte[] bb = smnBitmap;
  1465:     int o = smnWaveOffset;
  1466:     int t;
  1467:     int y0 = lastY[0];
  1468:     int y1 = lastY[1];
  1469:     int scaleY = SMN_WAVE_VALUE_SHIFT - smnWaveScaleY;
  1470:     for (int x = 0, b = 6; x < SMN_WAVE_WIDTH; x++, b = b - 2 & 6) {  //b=6,4,2,0
  1471:       int y2 = SMN_WAVE_HEIGHT / 2 - ((t = buffer[index[o + x]]) + (t >>> -scaleY) >> scaleY);
  1472:       int min, max;  //y1がminまたはmaxのときはy1まで、y0またはy2がminまたはmaxのときはy0+y1>>1またはy1+y2>>1まで描く
  1473:       if (y0 < y1) {  //y0<y1
  1474:         if (y1 < y2) {  //y0<y1<y2
  1475:           min = y0 + y1 >> 1;
  1476:           max = y1 + y2 >> 1;
  1477:         } else if (y0 < y2) {  //y0<y2<y1
  1478:           min = y0 + y1 >> 1;
  1479:           max = y1;
  1480:         } else {  //y2<y0<y1
  1481:           min = y1 + y2 >> 1;
  1482:           max = y1;
  1483:         }
  1484:       } else {  //y1<y0
  1485:         if (y0 < y2) {  //y1<y0<y2
  1486:           min = y1;
  1487:           max = y1 + y2 >> 1;
  1488:         } else if (y1 < y2) {  //y1<y2<y0
  1489:           min = y1;
  1490:           max = y0 + y1 >> 1;
  1491:         } else {  //y2<y1<y0
  1492:           min = y1 + y2 >> 1;
  1493:           max = y0 + y1 >> 1;
  1494:         }
  1495:       }
  1496:       min -= smnWaveElevation;
  1497:       max -= smnWaveElevation;
  1498:       //[min,max]と[0,SMN_WAVE_HEIGHT-1]の重なる部分だけ描く
  1499:       if (min < 0) {
  1500:         min = 0;
  1501:       }
  1502:       if (max > SMN_WAVE_HEIGHT - 1) {
  1503:         max = SMN_WAVE_HEIGHT - 1;
  1504:       }
  1505:       for (int y = min; y <= max; y++) {
  1506:         int i = SMN_OFFSET * (SMN_WAVE_Y + y) + SMN_WAVE_X / 4 + (x >> 2);
  1507:         bb[i] = (byte) (bb[i] & ~(3 << b) | palet << b);
  1508:       }
  1509:       y0 = y1;
  1510:       y1 = y2;
  1511:     }  //for x,b
  1512:     lastY[0] = y0;
  1513:     lastY[1] = y1;
  1514:   }  //smnWavePaint1(int[],int[],int,int[])
  1515: 
  1516:   //smnPaintKey (x, k, c1111)
  1517:   //  鍵を描く
  1518:   //  x      x座標
  1519:   //  k      鍵の番号。0..11
  1520:   //  c1111  4ドット分の色
  1521:   //    |    |   1|    |   3|    |    |    |   6|    |   8|    |  10|    |    |
  1522:   //    |    |  C#|    |  D#|    |    |    |  F#|    |  G#|    |  A#|    |    |
  1523:   //    |   0|   1|   2|   3|   4|   5|   6|   7|   8|   9|  10|  11|  12|  13|
  1524:   //   0|WWW |BBBB|BBB |BBBB|BBB |WWW |WWW |BBBB|BBB |BBBB|BBB |BBBB|BBB |WWW |
  1525:   //   1|WWW |BBBB|BBB |BBBB|BBB |WWW |WWW |BBBB|BBB |BBBB|BBB |BBBB|BBB |WWW |
  1526:   //   2|WWW |BBBB|BBB |BBBB|BBB |WWW |WWW |BBBB|BBB |BBBB|BBB |BBBB|BBB |WWW |
  1527:   //   3|WWW |BBBB|BBB |BBBB|BBB |WWW |WWW |BBBB|BBB |BBBB|BBB |BBBB|BBB |WWW |
  1528:   //   4|WWW |BBBB|BBB |BBBB|BBB |WWW |WWW |BBBB|BBB |BBBB|BBB |BBBB|BBB |WWW |
  1529:   //   5|WWW |    |    |    |    |WWW |WWW |    |    |    |    |    |    |WWW |
  1530:   //   6|WWWW|WWW |WWWW|WWW |WWWW|WWW |WWWW|WWW |WWWW|WWW |WWWW|WWW |WWWW|WWW |
  1531:   //   7|WWWW|WWW |WWWW|WWW |WWWW|WWW |WWWW|WWW |WWWW|WWW |WWWW|WWW |WWWW|WWW |
  1532:   //   8|WWWW|WWW |WWWW|WWW |WWWW|WWW |WWWW|WWW |WWWW|WWW |WWWW|WWW |WWWW|WWW |
  1533:   //   9|WWWW|WWW |WWWW|WWW |WWWW|WWW |WWWW|WWW |WWWW|WWW |WWWW|WWW |WWWW|WWW |
  1534:   //  10|WWWW|WWW |WWWW|WWW |WWWW|WWW |WWWW|WWW |WWWW|WWW |WWWW|WWW |WWWW|WWW |
  1535:   //    |   0|   1|   2|   3|   4|   5|   6|   7|   8|   9|  10|  11|  12|  13|
  1536:   //    |   C|    |   D|    |   E|    |   F|    |   G|    |   A|    |   B|    |
  1537:   //    |   0|    |   2|    |   4|    |   5|    |   7|    |   9|    |  11|    |
  1538:   public static void smnPaintKey (int i, int k, int c1111) {
  1539:     int c1110 = c1111 & 0b11111100;
  1540:     byte[] bb = smnBitmap;
  1541:     switch (k) {
  1542:     case 0:  //C
  1543:     case 5:  //F
  1544:       bb[       SMN_OFFSET *  6 + i] = (
  1545:         bb[     SMN_OFFSET *  7 + i] =
  1546:         bb[     SMN_OFFSET *  8 + i] =
  1547:         bb[     SMN_OFFSET *  9 + i] =
  1548:         bb[     SMN_OFFSET * 10 + i] = (byte) c1111);
  1549:       bb[                       + i] = (
  1550:         bb[     SMN_OFFSET      + i] =
  1551:         bb[     SMN_OFFSET *  2 + i] =
  1552:         bb[     SMN_OFFSET *  3 + i] =
  1553:         bb[     SMN_OFFSET *  4 + i] =
  1554:         bb[     SMN_OFFSET *  5 + i] =
  1555:         bb[ 1 + SMN_OFFSET *  6 + i] =
  1556:         bb[ 1 + SMN_OFFSET *  7 + i] =
  1557:         bb[ 1 + SMN_OFFSET *  8 + i] =
  1558:         bb[ 1 + SMN_OFFSET *  9 + i] =
  1559:         bb[ 1 + SMN_OFFSET * 10 + i] = (byte) c1110);
  1560:       break;
  1561:     case 1:  //C#
  1562:     case 3:  //D#
  1563:     case 6:  //F#
  1564:     case 8:  //G#
  1565:     case 10:  //A#
  1566:       bb[                         i] = (
  1567:         bb[     SMN_OFFSET      + i] =
  1568:         bb[     SMN_OFFSET *  2 + i] =
  1569:         bb[     SMN_OFFSET *  3 + i] =
  1570:         bb[     SMN_OFFSET *  4 + i] = (byte) c1111);
  1571:       bb[   1 +                 + i] = (
  1572:         bb[ 1 + SMN_OFFSET      + i] =
  1573:         bb[ 1 + SMN_OFFSET *  2 + i] =
  1574:         bb[ 1 + SMN_OFFSET *  3 + i] =
  1575:         bb[ 1 + SMN_OFFSET *  4 + i] = (byte) c1110);
  1576:       break;
  1577:     case 2:  //D
  1578:     case 7:  //G
  1579:     case 9:  //A
  1580:       bb[       SMN_OFFSET *  6 + i] = (
  1581:         bb[     SMN_OFFSET *  7 + i] =
  1582:         bb[     SMN_OFFSET *  8 + i] =
  1583:         bb[     SMN_OFFSET *  9 + i] =
  1584:         bb[     SMN_OFFSET * 10 + i] = (byte) c1111);
  1585:       bb[   1 + SMN_OFFSET *  6 + i] = (
  1586:         bb[ 1 + SMN_OFFSET *  7 + i] =
  1587:         bb[ 1 + SMN_OFFSET *  8 + i] =
  1588:         bb[ 1 + SMN_OFFSET *  9 + i] =
  1589:         bb[ 1 + SMN_OFFSET * 10 + i] = (byte) c1110);
  1590:       break;
  1591:     case 4:  //E
  1592:     case 11:  //B
  1593:       bb[       SMN_OFFSET *  6 + i] = (
  1594:         bb[     SMN_OFFSET *  7 + i] =
  1595:         bb[     SMN_OFFSET *  8 + i] =
  1596:         bb[     SMN_OFFSET *  9 + i] =
  1597:         bb[     SMN_OFFSET * 10 + i] = (byte) c1111);
  1598:       bb[   1 +                 + i] = (
  1599:         bb[ 1 + SMN_OFFSET      + i] =
  1600:         bb[ 1 + SMN_OFFSET *  2 + i] =
  1601:         bb[ 1 + SMN_OFFSET *  3 + i] =
  1602:         bb[ 1 + SMN_OFFSET *  4 + i] =
  1603:         bb[ 1 + SMN_OFFSET *  5 + i] =
  1604:         bb[ 1 + SMN_OFFSET *  6 + i] =
  1605:         bb[ 1 + SMN_OFFSET *  7 + i] =
  1606:         bb[ 1 + SMN_OFFSET *  8 + i] =
  1607:         bb[ 1 + SMN_OFFSET *  9 + i] =
  1608:         bb[ 1 + SMN_OFFSET * 10 + i] = (byte) c1110);
  1609:       break;
  1610:     }  //switch k
  1611:   }  //smnPaintKey(int,int,int)
  1612: 
  1613:   //smnPaintKey2 (x, k, c1111a, c1111b)
  1614:   //  鍵を市松模様に塗る
  1615:   //  x       x座標
  1616:   //  k       鍵の番号。0..11
  1617:   //  c1111a  4ドット分の色a
  1618:   //  c1111b  4ドット分の色b
  1619:   //    |    |   1|    |   3|    |    |    |   6|    |   8|    |  10|    |    |
  1620:   //    |    |  C#|    |  D#|    |    |    |  F#|    |  G#|    |  A#|    |    |
  1621:   //    |   0|   1|   2|   3|   4|   5|   6|   7|   8|   9|  10|  11|  12|  13|
  1622:   //   0|WWW |BBBB|BBB |BBBB|BBB |WWW |WWW |BBBB|BBB |BBBB|BBB |BBBB|BBB |WWW |
  1623:   //   1|WWW |BBBB|BBB |BBBB|BBB |WWW |WWW |BBBB|BBB |BBBB|BBB |BBBB|BBB |WWW |
  1624:   //   2|WWW |BBBB|BBB |BBBB|BBB |WWW |WWW |BBBB|BBB |BBBB|BBB |BBBB|BBB |WWW |
  1625:   //   3|WWW |BBBB|BBB |BBBB|BBB |WWW |WWW |BBBB|BBB |BBBB|BBB |BBBB|BBB |WWW |
  1626:   //   4|WWW |BBBB|BBB |BBBB|BBB |WWW |WWW |BBBB|BBB |BBBB|BBB |BBBB|BBB |WWW |
  1627:   //   5|WWW |    |    |    |    |WWW |WWW |    |    |    |    |    |    |WWW |
  1628:   //   6|WWWW|WWW |WWWW|WWW |WWWW|WWW |WWWW|WWW |WWWW|WWW |WWWW|WWW |WWWW|WWW |
  1629:   //   7|WWWW|WWW |WWWW|WWW |WWWW|WWW |WWWW|WWW |WWWW|WWW |WWWW|WWW |WWWW|WWW |
  1630:   //   8|WWWW|WWW |WWWW|WWW |WWWW|WWW |WWWW|WWW |WWWW|WWW |WWWW|WWW |WWWW|WWW |
  1631:   //   9|WWWW|WWW |WWWW|WWW |WWWW|WWW |WWWW|WWW |WWWW|WWW |WWWW|WWW |WWWW|WWW |
  1632:   //  10|WWWW|WWW |WWWW|WWW |WWWW|WWW |WWWW|WWW |WWWW|WWW |WWWW|WWW |WWWW|WWW |
  1633:   //    |   0|   1|   2|   3|   4|   5|   6|   7|   8|   9|  10|  11|  12|  13|
  1634:   //    |   C|    |   D|    |   E|    |   F|    |   G|    |   A|    |   B|    |
  1635:   //    |   0|    |   2|    |   4|    |   5|    |   7|    |   9|    |  11|    |
  1636:   public static void smnPaintKey2 (int i, int k, int c1111a, int c1111b) {
  1637:     int c1111ab = c1111a & 0b11001100 | c1111b & 0b00110011;
  1638:     int c1111ba = c1111b & 0b11001100 | c1111a & 0b00110011;
  1639:     int c1110ab = c1111ab & 0b11111100;
  1640:     int c1110ba = c1111ba & 0b11111100;
  1641:     byte[] bb = smnBitmap;
  1642:     switch (k) {
  1643:     case 0:  //C
  1644:     case 5:  //F
  1645:       bb[       SMN_OFFSET *  6 + i] = (
  1646:         bb[     SMN_OFFSET *  8 + i] =
  1647:         bb[     SMN_OFFSET * 10 + i] = (byte) c1111ab);
  1648:       bb[       SMN_OFFSET *  7 + i] = (
  1649:         bb[     SMN_OFFSET *  9 + i] = (byte) c1111ba);
  1650:       bb[                       + i] = (
  1651:         bb[     SMN_OFFSET *  2 + i] =
  1652:         bb[     SMN_OFFSET *  4 + i] =
  1653:         bb[ 1 + SMN_OFFSET *  6 + i] =
  1654:         bb[ 1 + SMN_OFFSET *  8 + i] =
  1655:         bb[ 1 + SMN_OFFSET * 10 + i] = (byte) c1110ab);
  1656:       bb[       SMN_OFFSET      + i] = (
  1657:         bb[     SMN_OFFSET *  3 + i] =
  1658:         bb[     SMN_OFFSET *  5 + i] =
  1659:         bb[ 1 + SMN_OFFSET *  7 + i] =
  1660:         bb[ 1 + SMN_OFFSET *  9 + i] = (byte) c1110ba);
  1661:       break;
  1662:     case 1:  //C#
  1663:     case 3:  //D#
  1664:     case 6:  //F#
  1665:     case 8:  //G#
  1666:     case 10:  //A#
  1667:       bb[                         i] = (
  1668:         bb[     SMN_OFFSET *  2 + i] =
  1669:         bb[     SMN_OFFSET *  4 + i] = (byte) c1111ab);
  1670:       bb[       SMN_OFFSET      + i] = (
  1671:         bb[     SMN_OFFSET *  3 + i] = (byte) c1111ba);
  1672:       bb[   1 +                 + i] = (
  1673:         bb[ 1 + SMN_OFFSET *  2 + i] =
  1674:         bb[ 1 + SMN_OFFSET *  4 + i] = (byte) c1110ab);
  1675:       bb[   1 + SMN_OFFSET      + i] = (
  1676:         bb[ 1 + SMN_OFFSET *  3 + i] = (byte) c1110ba);
  1677:       break;
  1678:     case 2:  //D
  1679:     case 7:  //G
  1680:     case 9:  //A
  1681:       bb[       SMN_OFFSET *  6 + i] = (
  1682:         bb[     SMN_OFFSET *  8 + i] =
  1683:         bb[     SMN_OFFSET * 10 + i] = (byte) c1111ab);
  1684:       bb[       SMN_OFFSET *  7 + i] = (
  1685:         bb[     SMN_OFFSET *  9 + i] = (byte) c1111ba);
  1686:       bb[   1 + SMN_OFFSET *  6 + i] = (
  1687:         bb[ 1 + SMN_OFFSET *  8 + i] =
  1688:         bb[ 1 + SMN_OFFSET * 10 + i] = (byte) c1110ab);
  1689:       bb[   1 + SMN_OFFSET *  7 + i] = (
  1690:         bb[ 1 + SMN_OFFSET *  9 + i] = (byte) c1110ba);
  1691:       break;
  1692:     case 4:  //E
  1693:     case 11:  //B
  1694:       bb[       SMN_OFFSET *  6 + i] = (
  1695:         bb[     SMN_OFFSET *  8 + i] =
  1696:         bb[     SMN_OFFSET * 10 + i] = (byte) c1111ab);
  1697:       bb[       SMN_OFFSET *  7 + i] = (
  1698:         bb[     SMN_OFFSET *  9 + i] = (byte) c1111ba);
  1699:       bb[   1 +                 + i] = (
  1700:         bb[ 1 + SMN_OFFSET *  2 + i] =
  1701:         bb[ 1 + SMN_OFFSET *  4 + i] =
  1702:         bb[ 1 + SMN_OFFSET *  6 + i] =
  1703:         bb[ 1 + SMN_OFFSET *  8 + i] =
  1704:         bb[ 1 + SMN_OFFSET * 10 + i] = (byte) c1110ab);
  1705:       bb[   1 + SMN_OFFSET      + i] = (
  1706:         bb[ 1 + SMN_OFFSET *  3 + i] =
  1707:         bb[ 1 + SMN_OFFSET *  5 + i] =
  1708:         bb[ 1 + SMN_OFFSET *  7 + i] =
  1709:         bb[ 1 + SMN_OFFSET *  9 + i] = (byte) c1110ba);
  1710:       break;
  1711:     }  //switch k
  1712:   }  //smnPaintKey2(int,int,int,int)
  1713: 
  1714:   //smnDrawIcon1 (x, y, c)
  1715:   //  パレットコード1でアイコンを描く
  1716:   public static void smnDrawIcon1 (int x, int y, int c) {
  1717:     byte[] bb = smnBitmap;
  1718:     x += SMN_OFFSET * y;
  1719:     c *= 18;
  1720:     bb[                    + x] = SMN_ICON_1[c     ];
  1721:     bb[1                   + x] = SMN_ICON_1[c +  1];
  1722:     bb[    SMN_OFFSET      + x] = SMN_ICON_1[c +  2];
  1723:     bb[1 + SMN_OFFSET      + x] = SMN_ICON_1[c +  3];
  1724:     bb[    SMN_OFFSET *  2 + x] = SMN_ICON_1[c +  4];
  1725:     bb[1 + SMN_OFFSET *  2 + x] = SMN_ICON_1[c +  5];
  1726:     bb[    SMN_OFFSET *  3 + x] = SMN_ICON_1[c +  6];
  1727:     bb[1 + SMN_OFFSET *  3 + x] = SMN_ICON_1[c +  7];
  1728:     bb[    SMN_OFFSET *  4 + x] = SMN_ICON_1[c +  8];
  1729:     bb[1 + SMN_OFFSET *  4 + x] = SMN_ICON_1[c +  9];
  1730:     bb[    SMN_OFFSET *  5 + x] = SMN_ICON_1[c + 10];
  1731:     bb[1 + SMN_OFFSET *  5 + x] = SMN_ICON_1[c + 11];
  1732:     bb[    SMN_OFFSET *  6 + x] = SMN_ICON_1[c + 12];
  1733:     bb[1 + SMN_OFFSET *  6 + x] = SMN_ICON_1[c + 13];
  1734:     bb[    SMN_OFFSET *  7 + x] = SMN_ICON_1[c + 14];
  1735:     bb[1 + SMN_OFFSET *  7 + x] = SMN_ICON_1[c + 15];
  1736:     bb[    SMN_OFFSET *  8 + x] = SMN_ICON_1[c + 16];
  1737:     bb[1 + SMN_OFFSET *  8 + x] = SMN_ICON_1[c + 17];
  1738:   }  //smnDrawIcon1(int,int,int)
  1739: 
  1740:   //smnDrawIcon3 (x, y, c)
  1741:   //  パレットコード3でアイコンを描く
  1742:   public static void smnDrawIcon3 (int x, int y, int c) {
  1743:     byte[] bb = smnBitmap;
  1744:     x += SMN_OFFSET * y;
  1745:     c *= 18;
  1746:     bb[                    + x] = SMN_ICON_3[c     ];
  1747:     bb[1                   + x] = SMN_ICON_3[c +  1];
  1748:     bb[    SMN_OFFSET      + x] = SMN_ICON_3[c +  2];
  1749:     bb[1 + SMN_OFFSET      + x] = SMN_ICON_3[c +  3];
  1750:     bb[    SMN_OFFSET *  2 + x] = SMN_ICON_3[c +  4];
  1751:     bb[1 + SMN_OFFSET *  2 + x] = SMN_ICON_3[c +  5];
  1752:     bb[    SMN_OFFSET *  3 + x] = SMN_ICON_3[c +  6];
  1753:     bb[1 + SMN_OFFSET *  3 + x] = SMN_ICON_3[c +  7];
  1754:     bb[    SMN_OFFSET *  4 + x] = SMN_ICON_3[c +  8];
  1755:     bb[1 + SMN_OFFSET *  4 + x] = SMN_ICON_3[c +  9];
  1756:     bb[    SMN_OFFSET *  5 + x] = SMN_ICON_3[c + 10];
  1757:     bb[1 + SMN_OFFSET *  5 + x] = SMN_ICON_3[c + 11];
  1758:     bb[    SMN_OFFSET *  6 + x] = SMN_ICON_3[c + 12];
  1759:     bb[1 + SMN_OFFSET *  6 + x] = SMN_ICON_3[c + 13];
  1760:     bb[    SMN_OFFSET *  7 + x] = SMN_ICON_3[c + 14];
  1761:     bb[1 + SMN_OFFSET *  7 + x] = SMN_ICON_3[c + 15];
  1762:     bb[    SMN_OFFSET *  8 + x] = SMN_ICON_3[c + 16];
  1763:     bb[1 + SMN_OFFSET *  8 + x] = SMN_ICON_3[c + 17];
  1764:   }  //smnDrawIcon3(int,int,int)
  1765: 
  1766:   //smnDrawChar1 (x, y, c)
  1767:   //  パレットコード1で文字を描く
  1768:   public static void smnDrawChar1 (int x, int y, int c) {
  1769:     byte[] bb = smnBitmap;
  1770:     x += SMN_OFFSET * y;
  1771:     c *= 5;
  1772:     bb[x                 ] = SMN_FONT_1[c    ];
  1773:     bb[x + SMN_OFFSET    ] = SMN_FONT_1[c + 1];
  1774:     bb[x + SMN_OFFSET * 2] = SMN_FONT_1[c + 2];
  1775:     bb[x + SMN_OFFSET * 3] = SMN_FONT_1[c + 3];
  1776:     bb[x + SMN_OFFSET * 4] = SMN_FONT_1[c + 4];
  1777:   }  //smnDrawChar1(int,int,int)
  1778: 
  1779:   //smnDrawChar2 (x, y, c)
  1780:   //  パレットコード2で文字を描く
  1781:   public static void smnDrawChar2 (int x, int y, int c) {
  1782:     byte[] bb = smnBitmap;
  1783:     x += SMN_OFFSET * y;
  1784:     c *= 5;
  1785:     bb[x                 ] = SMN_FONT_2[c    ];
  1786:     bb[x + SMN_OFFSET    ] = SMN_FONT_2[c + 1];
  1787:     bb[x + SMN_OFFSET * 2] = SMN_FONT_2[c + 2];
  1788:     bb[x + SMN_OFFSET * 3] = SMN_FONT_2[c + 3];
  1789:     bb[x + SMN_OFFSET * 4] = SMN_FONT_2[c + 4];
  1790:   }  //smnDrawChar2(int,int,int)
  1791: 
  1792:   //smnDrawChar3 (x, y, c)
  1793:   //  パレットコード3で文字を描く
  1794:   public static void smnDrawChar3 (int x, int y, int c) {
  1795:     byte[] bb = smnBitmap;
  1796:     x += SMN_OFFSET * y;
  1797:     c *= 5;
  1798:     bb[x                 ] = SMN_FONT_3[c    ];
  1799:     bb[x + SMN_OFFSET    ] = SMN_FONT_3[c + 1];
  1800:     bb[x + SMN_OFFSET * 2] = SMN_FONT_3[c + 2];
  1801:     bb[x + SMN_OFFSET * 3] = SMN_FONT_3[c + 3];
  1802:     bb[x + SMN_OFFSET * 4] = SMN_FONT_3[c + 4];
  1803:   }  //smnDrawChar3(int,int,int)
  1804: 
  1805:   //smnDrawString1 (x, y, s)
  1806:   //  パレットコード1で文字列を描く
  1807:   public static void smnDrawString1 (int x, int y, String s) {
  1808:     smnDrawString1 (x, y, s.toCharArray ());
  1809:   }  //smnDrawString1(int,int,String)
  1810:   public static void smnDrawString1 (int x, int y, char[] s) {
  1811:     byte[] bb = smnBitmap;
  1812:     x += SMN_OFFSET * y;
  1813:     for (char c : s) {
  1814:       c *= 5;
  1815:       bb[x                 ] = SMN_FONT_1[c    ];
  1816:       bb[x + SMN_OFFSET    ] = SMN_FONT_1[c + 1];
  1817:       bb[x + SMN_OFFSET * 2] = SMN_FONT_1[c + 2];
  1818:       bb[x + SMN_OFFSET * 3] = SMN_FONT_1[c + 3];
  1819:       bb[x + SMN_OFFSET * 4] = SMN_FONT_1[c + 4];
  1820:       x++;
  1821:     }
  1822:   }  //smnDrawString1(int,int,char[])
  1823: 
  1824:   //smnDrawString2 (x, y, s)
  1825:   //  パレットコード2で文字列を描く
  1826:   public static void smnDrawString2 (int x, int y, String s) {
  1827:     smnDrawString2 (x, y, s.toCharArray ());
  1828:   }  //smnDrawString2(int,int,String)
  1829:   public static void smnDrawString2 (int x, int y, char[] s) {
  1830:     byte[] bb = smnBitmap;
  1831:     x += SMN_OFFSET * y;
  1832:     for (char c : s) {
  1833:       c *= 5;
  1834:       bb[x                 ] = SMN_FONT_2[c    ];
  1835:       bb[x + SMN_OFFSET    ] = SMN_FONT_2[c + 1];
  1836:       bb[x + SMN_OFFSET * 2] = SMN_FONT_2[c + 2];
  1837:       bb[x + SMN_OFFSET * 3] = SMN_FONT_2[c + 3];
  1838:       bb[x + SMN_OFFSET * 4] = SMN_FONT_2[c + 4];
  1839:       x++;
  1840:     }
  1841:   }  //smnDrawString2(int,int,char[])
  1842: 
  1843:   //smnDrawString3 (x, y, s)
  1844:   //  パレットコード3で文字列を描く
  1845:   public static void smnDrawString3 (int x, int y, String s) {
  1846:     smnDrawString3 (x, y, s.toCharArray ());
  1847:   }  //smnDrawString3(int,int,String)
  1848:   public static void smnDrawString3 (int x, int y, char[] s) {
  1849:     byte[] bb = smnBitmap;
  1850:     x += SMN_OFFSET * y;
  1851:     for (char c : s) {
  1852:       c *= 5;
  1853:       bb[x                 ] = SMN_FONT_3[c    ];
  1854:       bb[x + SMN_OFFSET    ] = SMN_FONT_3[c + 1];
  1855:       bb[x + SMN_OFFSET * 2] = SMN_FONT_3[c + 2];
  1856:       bb[x + SMN_OFFSET * 3] = SMN_FONT_3[c + 3];
  1857:       bb[x + SMN_OFFSET * 4] = SMN_FONT_3[c + 4];
  1858:       x++;
  1859:     }
  1860:   }  //smnDrawString3(int,int,char[])
  1861: 
  1862:   //smnDrawSlider (slider, pattern)
  1863:   //  スライダを描く
  1864:   //  slider[0]  x0。4の倍数
  1865:   //  slider[1]  y0
  1866:   //  slider[2]  max。4の倍数
  1867:   //  slider[3]  palet
  1868:   //  slider[4]  value。0<=value<=max
  1869:   //  width=max+4
  1870:   //  height=5
  1871:   public static void smnDrawSlider (int[] slider, int[] pattern) {
  1872:     byte[] bb = smnBitmap;
  1873:     int x0 = slider[0];
  1874:     int y0 = slider[1];
  1875:     int max = slider[2];
  1876:     int palet = slider[3];
  1877:     int value = slider[4];
  1878:     int width = max + 4;
  1879:     for (int i = (x0 >> 2) + SMN_OFFSET * y0, l = i + (width >> 2); i < l; i++) {
  1880:       bb[  i                 ] = (
  1881:         bb[i + SMN_OFFSET * 1] =
  1882:         bb[i + SMN_OFFSET * 3] =
  1883:         bb[i + SMN_OFFSET * 4] = SMN_BLACK);
  1884:       bb[  i + SMN_OFFSET * 2] = (byte) palet;
  1885:     }
  1886:     smnDrawPattern (x0 + value, y0 + 2, pattern);
  1887:   }  //smnDrawSlider(int[],int[])
  1888: 
  1889:   //smnDrawPattern (x0, y0, pattern)
  1890:   //  パターンを描く
  1891:   //  pattern={n0,palet,u0,v0,u1,v1,...,n1,palet,...}
  1892:   //           <-----------n0---------->
  1893:   public static void smnDrawPattern (int x0, int y0, int[] pattern) {
  1894:     byte[] bb = smnBitmap;
  1895:     for (int k = 0; k < pattern.length; ) {
  1896:       int limit = k + pattern[k];
  1897:       int palet = pattern[k + 1] & 3;
  1898:       for (k += 2; k < limit; k += 2) {
  1899:         int x = x0 + pattern[k];
  1900:         int y = y0 + pattern[k + 1];
  1901:         int b = (~x & 3) << 1;  //0→6,1→4,2→2,3→0
  1902:         int i = SMN_OFFSET * y + (x >> 2);
  1903:         bb[i] = (byte) (bb[i] & ~(3 << b) | palet << b);
  1904:       }
  1905:     }
  1906:   }  //smnDrawPattern(int,int,int[])
  1907: 
  1908: }  //class SoundMonitor
  1909: 
  1910: 
  1911: