波形生成

 投稿者:しばっち  投稿日:2011年11月13日(日)20時00分9秒
  サンプル波形の生成

OPTION CHARACTER BYTE
LET  CHANNEL=1 !'モノラル
LET  SAMPLEBIT=16 !'ビット数
LET  HEADERSIZE=16
LET  WAVETYPE=1
!'LET PATH$="C:\WINDOWS\TEMP\" !'作業用フォルダ、RAMディスクなど
DIM A$(7),FR(6),TYPE$(8)
MAT READ A$
DATA "8kHz","11.25kHz","22.5kHz","32kHz","44.1kHz","48kHz","プログラム終了"
MAT READ FR
DATA 8000,11025,22050,32000,44100,48000
MAT READ TYPE$
DATA 正弦波,三角波,方形波,台形波,のこぎり波,階段状,パルス波,ワイヤストラス
DO
   LOCATE CHOICE (A$):MODE
   IF MODE=7 THEN STOP
   LOCATE VALUE ,RANGE 0 TO FR(MODE)/2 ,AT 440 : FREQ
   SELECT CASE MODE !'サンプリング周波数
   CASE 1
      LET SAMPLINGFREQ=8000
   CASE 2
      LET SAMPLINGFREQ=11025
   CASE 3
      LET SAMPLINGFREQ=22050
   CASE 4
      LET SAMPLINGFREQ=32000
   CASE 5
      LET SAMPLINGFREQ=44100
   CASE 6
      LET SAMPLINGFREQ=48000
   END SELECT
   LOCATE CHOICE (TYPE$):MODE
   LET  SAMPLESIZE = SAMPLEBIT / 8 * CHANNEL
   LET  DATARATE = SAMPLESIZE * SAMPLINGFREQ
   LET  VOL=.1  !'音量  ※スピーカ破損の可能性あり。音量に気をつけること
   LET  LEVEL=2^(SAMPLEBIT-1)*VOL
   LET  SECOND=3 !'再生時間(3秒間)
   !' LOCATE VALUE ,RANGE 1 TO 10,AT 3 : SECOND
   LET  PCMSIZE=INT(DATARATE*SECOND)
   LET  WAVEFILESIZE = PCMSIZE + 36
   OPEN #1:NAME PATH$ & TYPE$(MODE) & ".wav"
   ERASE #1
   PRINT #1:"RIFF";
   PRINT #1:MKL$(WAVEFILESIZE);
   PRINT #1:"WAVEfmt ";
   PRINT #1:MKL$(HEADERSIZE);
   PRINT #1:MKI$(WAVETYPE);
   PRINT #1:MKI$(CHANNEL);
   PRINT #1:MKL$(SAMPLINGFREQ);
   PRINT #1:MKL$(DATARATE);
   PRINT #1:MKI$(SAMPLESIZE);
   PRINT #1:MKI$(SAMPLEBIT);
   PRINT #1:"data";
   PRINT #1:MKL$(PCMSIZE);
   FOR I=0 TO INT(SAMPLINGFREQ*SECOND)-1
      LET X=I*FREQ/SAMPLINGFREQ*2*PI
      SELECT CASE MODE
      CASE 1
         LET DAT=SIN(X)
      CASE 2
         LET DAT=TRIANGLEW(X)
      CASE 3
         LET DAT=SQUAREW(X)
      CASE 4
         LET DAT=TRAPEZOIDALW(X)
      CASE 5
         LET DAT=SAW(X)
      CASE 6
         LET DAT=STEPWISE(X,4)
      CASE 7
         LET DAT=PULSEW(X)
      CASE 8
         LET DAT=WEIERSTRASS(X,15)
      END SELECT
      LET DAT=DAT*LEVEL
      PRINT #1:MKI$(INT(DAT));
   NEXT I
   CLOSE #1
   PLAYSOUND PATH$ & TYPE$(MODE) & ".wav" !'再生
   !'FILE DELETE PATH$ & TYPE$(MODE) & ".wav"
LOOP
END

EXTERNAL  FUNCTION PULSEW(X) !'パルス波 PULSE WAVE
IF ABS(ABS(SIN(X))-1)<.0001 THEN LET PULSEW=SGN(SIN(X)) ELSE LET PULSEW=0
END FUNCTION

EXTERNAL  FUNCTION SQUAREW(X) !'方形波 SQUARE WAVE
LET SQUAREW=SGN(SIN(X))
END FUNCTION

EXTERNAL  FUNCTION TRIANGLEW(X) !'三角波 TRIANGLE WAVE
LET NN=MOD(X,PI)
IF NN>PI/2 THEN LET NN=PI-NN
LET TRIANGLEW=NN/(PI/2)*SGN(SIN(X))
END FUNCTION

EXTERNAL  FUNCTION SAW(X) !'のこぎり波
LET  SAW=MOD(X,PI)/(PI/2)-1
END FUNCTION

EXTERNAL  FUNCTION TRAPEZOIDALW(X) !'台形波 TRAPEZOIDAL WAVE
LET  NN=X-INT(X/PI)*PI
IF NN>PI/2 THEN LET  NN=PI-NN
LET  TRAPEZOIDALW=MIN(1,MAX(-1,NN*SGN(SIN(X))) )
END FUNCTION

EXTERNAL  FUNCTION STEPWISE(X,NN) !'階段状
LET STEPWISE=INT(NN*(SIN(X)+1))/NN-1
END FUNCTION

EXTERNAL  FUNCTION WEIERSTRASS(X,N) !'ワイヤストラス
LET B=11
FOR I=0 TO N
   LET S=S+.5^(I+1)*SIN(B^I*X)
NEXT I
LET WEIERSTRASS=S
END FUNCTION

EXTERNAL  FUNCTION MKI$(A)
OPTION CHARACTER BYTE
DECLARE STRING A$,B$
IF A<0 THEN LET  A=A+65536
LET  A$=CHR$(MOD(A,256))
LET  B$=CHR$(INT(A/256))
LET  MKI$=A$ & B$
END FUNCTION

EXTERNAL  FUNCTION MKL$(A)
OPTION CHARACTER BYTE
DECLARE STRING A$,B$,C$,D$
IF A<0 THEN LET  A=A+2^32
LET  A$=CHR$(MOD(A,256))
LET  B$=CHR$(MOD(INT(A/256),256))
LET  C$=CHR$(MOD(INT(A/65536),256))
LET  D$=CHR$(MOD(INT(A/16777216),256))
LET  MKL$=A$ & B$ & C$ & D$
END FUNCTION
 

戻る