開発終了しました
クロスフェードをかける方法がよく用いられているように感じました、pyaudioでも簡単にできそうです。実際にPythonで書かれたコードもウェブで見つかりました。
自分のやってることにあんまり価値がなかったのでほどほどでほかのことに映ります。
リビジョン | b8f7562f1460fd27c98d91749ef0f44c403c7227 (tree) |
---|---|
日時 | 2018-05-13 18:52:58 |
作者 | yamat0jp <yamat0jp@yaho...> |
コミッター | yamat0jp |
chapter 9-2
とりあえずできた
@@ -41,7 +41,9 @@ type | ||
41 | 41 | channels: Byte; |
42 | 42 | bytesPerSec: LongWord; |
43 | 43 | posOfData: LongInt; |
44 | - startpos, endpos: LongInt; | |
44 | + startpos: LongInt; | |
45 | + endpos: LongInt; | |
46 | + cycleuSec: LongInt; | |
45 | 47 | end; |
46 | 48 | |
47 | 49 | const |
@@ -6,87 +6,100 @@ program wav_proj; | ||
6 | 6 | uses |
7 | 7 | System.SysUtils, |
8 | 8 | System.Classes, |
9 | - wav in 'wav.pas', | |
10 | - WriteHeader in 'WriteHeader.pas', | |
11 | 9 | spWav in 'spWav.pas'; |
12 | 10 | |
13 | -function checkRange(var sp: SpParam): integer; | |
14 | -begin | |
15 | - result := 0; | |
16 | - if sp.startpos * sp.bytesPerSec > sp.sizeOfData then | |
17 | - begin | |
18 | - Writeln('開始位置がファイルサイズを超えています'); | |
19 | - result := -1; | |
20 | - end | |
21 | - else if (sp.endpos + 1) * sp.bytesPerSec > sp.sizeOfData then | |
22 | - begin | |
23 | - Writeln('終了位置がファイルサイズを超えています'); | |
24 | - Writeln('終了をファイルの最後に調整しました'); | |
25 | - sp.endpos := (sp.sizeOfData div sp.bytesPerSec) - 1; | |
26 | - end; | |
27 | -end; | |
28 | - | |
29 | -function wavDataWrite(fpIn, fpOut: TFileStream; const sp: SpParam): integer; | |
11 | +function wavDataWrite(fpOut: TFileStream; const sp: SpParam): integer; | |
30 | 12 | var |
31 | - Buffer: array of ShortInt; | |
13 | + i: integer; | |
14 | + s, tempsamplePerCycle, deltaAdd, curLevel: Single; | |
15 | + curSampling, samplePerCycle: LongInt; | |
16 | + c: array [0..1] of ShortInt; | |
32 | 17 | begin |
33 | - result := 0; | |
34 | - fpIn.Position := sp.posOfData; | |
35 | - try | |
36 | - GetMem(Pointer(Buffer), sp.sizeOfData); | |
37 | - except | |
38 | - Writeln('メモリが確保できません'); | |
39 | - result := -1; | |
40 | - end; | |
41 | - if fpIn.Read(Pointer(Buffer)^, sp.sizeOfData) = -1 then | |
18 | + tempsamplePerCycle:=sp.samplePerSec*sp.cycleuSec div 1000000; | |
19 | + samplePerCycle:=Trunc(tempsamplePerCycle); | |
20 | + if samplePerCycle <= 0 then | |
42 | 21 | begin |
43 | - Writeln('読み込みに失敗'); | |
44 | - result := -1; | |
22 | + Writeln('周波数が高すぎ'); | |
23 | + result:=-1; | |
24 | + Exit; | |
45 | 25 | end; |
46 | - if fpOut.Write(Pointer(Buffer)^, sp.sizeOfData) = -1 then | |
26 | + deltaAdd:=65535/samplePerCycle; | |
27 | + curLevel:=0; | |
28 | + curSampling:=0; | |
29 | + i:=0; | |
30 | + s:=sp.sizeOfData/SizeOf(@c); | |
31 | + while i < s do | |
47 | 32 | begin |
48 | - Writeln('書き込みに失敗'); | |
49 | - result := -1; | |
33 | + inc(i); | |
34 | + c[0]:=ShortInt(Trunc(curLevel-32788)); | |
35 | + c[1]:=c[0]; | |
36 | + fpOut.WriteBuffer(c,SizeOf(@c)); | |
37 | + curLevel:=curLevel+deltaAdd; | |
38 | + inc(curSampling); | |
39 | + if curSampling > samplePerCycle then | |
40 | + begin | |
41 | + curLevel:=0; | |
42 | + curSampling:=0; | |
43 | + end; | |
50 | 44 | end; |
51 | - FreeMem(Pointer(Buffer)); | |
52 | 45 | end; |
53 | 46 | |
54 | -function wavWrite(inFile, outFile: PChar; var sp: SpParam): integer; | |
47 | +function wavWrite(outFile: PChar; const wHdr: WrSWaveFileHeader; | |
48 | + var sp: SpParam): integer; | |
55 | 49 | var |
56 | 50 | fpIn, fpOut: TFileStream; |
57 | 51 | begin |
52 | + result := 0; | |
58 | 53 | try |
59 | - fpIn := TFileStream.Create(inFile, fmOpenRead); | |
60 | 54 | fpOut := TFileStream.Create(outFile, fmCreate); |
61 | - sp.sizeOfData := (sp.endpos - sp.startpos + 1) * sp.bytesPerSec; | |
62 | - if waveHeaderWrite(fpOut, sp) > 44 then | |
63 | - raise EWriteError.Create('ヘッダを書き込めません'); | |
64 | - if wavDataWrite(fpIn, fpOut, sp) = -1 then | |
65 | - raise EWriteError.Create('エラー発生'); | |
55 | + fpOut.WriteBuffer(wHdr, SizeOf(WrSWaveFileHeader)); | |
56 | + if wavDataWrite(fpOut, sp) = -1 then | |
57 | + raise EWriteError.Create(''); | |
66 | 58 | except |
67 | 59 | on EFOpenError do |
68 | - Writeln(inFile, 'をオープンできません'); | |
69 | - on EFOpenError do | |
70 | - fpIn.Free; | |
71 | - else | |
72 | - | |
73 | 60 | begin |
74 | - fpIn.Free; | |
75 | - fpOut.Free; | |
61 | + Writeln(outFile, 'をオープンできません'); | |
62 | + result := -1; | |
63 | + end; | |
64 | + on EWriteError do | |
65 | + begin | |
66 | + Writeln('ヘッダを書き込めません'); | |
67 | + result := -1; | |
76 | 68 | end; |
77 | - result := -1; | |
78 | - Exit; | |
79 | 69 | end; |
80 | - result := 0; | |
70 | + fpOut.Free; | |
81 | 71 | end; |
82 | 72 | |
83 | 73 | procedure usage; |
84 | 74 | begin |
85 | - Writeln('引数<入力ファイル名><出力ファイル名><速度倍率>'); | |
75 | + Writeln('のこぎり波'); | |
76 | + Writeln('例:effect.wav 100 2000'); | |
77 | +end; | |
78 | + | |
79 | +procedure setupHeader(var wHdr: WrSWaveFileHeader; var sp: SpParam); | |
80 | +var | |
81 | + bytes: Byte; | |
82 | +begin | |
83 | + wHdr.hdrRiff := STR_RIFF; | |
84 | + wHdr.sizeOfFile := sp.sizeOfData + SizeOf(WrSWaveFileHeader) - 8; | |
85 | + wHdr.hdrWave := STR_WAVE; | |
86 | + wHdr.hdrFmt := STR_fmt; | |
87 | + wHdr.sizeOfFmt := SizeOf(tWaveFormatPcm); | |
88 | + wHdr.stWaveFormat.formatTag := 1; | |
89 | + wHdr.stWaveFormat.channels := sp.channels; | |
90 | + wHdr.stWaveFormat.sampleParSec := sp.samplePerSec; | |
91 | + bytes := sp.bitsPerSample div 8; | |
92 | + wHdr.stWaveFormat.bytesPerSec := bytes * sp.channels * sp.samplePerSec; | |
93 | + wHdr.stWaveFormat.blockAlign := bytes * sp.channels; | |
94 | + wHdr.stWaveFormat.bitsPerSample := sp.bitsPerSample; | |
95 | + wHdr.hdrData := STR_data; | |
96 | + wHdr.sizeOfData := sp.sizeOfData; | |
86 | 97 | end; |
87 | 98 | |
88 | 99 | var |
89 | 100 | sp: SpParam; |
101 | + totalLength: integer; | |
102 | + hdrHeader: WrSWaveFileHeader; | |
90 | 103 | |
91 | 104 | begin |
92 | 105 | try |
@@ -96,10 +109,15 @@ begin | ||
96 | 109 | usage; |
97 | 110 | Exit; |
98 | 111 | end; |
99 | - if wavHdrRead(PChar(ParamStr(1)), sp) = -1 then | |
100 | - Exit; | |
101 | - sp.samplePerSec := StrToInt(ParamStr(3)) * sp.samplePerSec; | |
102 | - if wavWrite(PChar(ParamStr(1)), PChar(ParamStr(2)), sp) = -1 then | |
112 | + totalLength := StrToInt(ParamStr(2)); | |
113 | + sp.cycleuSec := StrToInt(ParamStr(3)); | |
114 | + sp.channels := WAV_STEREO; | |
115 | + sp.samplePerSec := 44100; | |
116 | + sp.bitsPerSample := 16; | |
117 | + sp.sizeOfData := sp.bitsPerSample * sp.channels * sp.samplePerSec * | |
118 | + totalLength div 8; | |
119 | + setupHeader(hdrHeader, sp); | |
120 | + if wavWrite(PChar(ParamStr(1)), hdrHeader, sp) = -1 then | |
103 | 121 | Exit; |
104 | 122 | Writeln('完了'); |
105 | 123 | except |
@@ -127,7 +127,7 @@ | ||
127 | 127 | <DCC_RemoteDebug>true</DCC_RemoteDebug> |
128 | 128 | </PropertyGroup> |
129 | 129 | <PropertyGroup Condition="'$(Cfg_1_Win32)'!=''"> |
130 | - <Debugger_RunParams>toujyo.wav effect.wav 2</Debugger_RunParams> | |
130 | + <Debugger_RunParams>effect.wav 100 2000</Debugger_RunParams> | |
131 | 131 | <DCC_RemoteDebug>false</DCC_RemoteDebug> |
132 | 132 | </PropertyGroup> |
133 | 133 | <PropertyGroup Condition="'$(Cfg_1_Win64)'!=''"> |
@@ -146,8 +146,6 @@ | ||
146 | 146 | <DelphiCompile Include="$(MainSource)"> |
147 | 147 | <MainSource>MainSource</MainSource> |
148 | 148 | </DelphiCompile> |
149 | - <DCCReference Include="wav.pas"/> | |
150 | - <DCCReference Include="WriteHeader.pas"/> | |
151 | 149 | <DCCReference Include="spWav.pas"/> |
152 | 150 | <BuildConfiguration Include="Release"> |
153 | 151 | <Key>Cfg_2</Key> |
@@ -9,23 +9,23 @@ | ||
9 | 9 | <Transaction>2017/10/19 20:48:10.000.837,=C:\Users\yamat\Documents\Embarcadero\Studio\Projects\ServerMethodsUnit1.pas</Transaction> |
10 | 10 | <Transaction>2017/10/19 20:48:11.000.893,=C:\Users\yamat\Documents\Embarcadero\Studio\Projects\ServerContainerUnit1.pas</Transaction> |
11 | 11 | <Transaction>2017/10/19 20:49:15.000.988,=C:\Users\yamat\Documents\Embarcadero\Studio\Projects\Unit1.pas</Transaction> |
12 | - <Transaction>2017/10/19 20:49:16.000.218,=C:\Users\yamat\Documents\Embarcadero\Studio\Projects\ServerContainerUnit1.pas</Transaction> | |
13 | 12 | <Transaction>2017/10/19 20:49:16.000.125,=C:\Users\yamat\Documents\Embarcadero\Studio\Projects\ServerMethodsUnit1.pas</Transaction> |
13 | + <Transaction>2017/10/19 20:49:16.000.218,=C:\Users\yamat\Documents\Embarcadero\Studio\Projects\ServerContainerUnit1.pas</Transaction> | |
14 | 14 | <Transaction>2017/10/19 20:49:45.000.913,=C:\Users\yamat\Documents\Embarcadero\Studio\Projects\ServerContainerUnit1.pas</Transaction> |
15 | 15 | <Transaction>2017/10/19 20:49:45.000.816,=C:\Users\yamat\Documents\Embarcadero\Studio\Projects\ServerMethodsUnit1.pas</Transaction> |
16 | 16 | <Transaction>2017/10/19 20:50:18.000.999,=C:\Users\yamat\Documents\Embarcadero\Studio\Projects\ServerMethodsUnit1.pas</Transaction> |
17 | 17 | <Transaction>2017/10/19 20:50:19.000.049,=C:\Users\yamat\Documents\Embarcadero\Studio\Projects\ServerContainerUnit1.pas</Transaction> |
18 | - <Transaction>2017/10/19 21:37:00.000.271,=C:\Users\yamat\Documents\Embarcadero\Studio\Projects\Unit1.pas</Transaction> | |
19 | 18 | <Transaction>2017/10/19 21:37:00.000.652,=C:\Users\yamat\Documents\Embarcadero\Studio\Projects\ServerContainerUnit1.pas</Transaction> |
19 | + <Transaction>2017/10/19 21:37:00.000.271,=C:\Users\yamat\Documents\Embarcadero\Studio\Projects\Unit1.pas</Transaction> | |
20 | 20 | <Transaction>2017/10/19 21:37:00.000.365,=C:\Users\yamat\Documents\Embarcadero\Studio\Projects\ServerMethodsUnit1.pas</Transaction> |
21 | 21 | <Transaction>2017/10/28 22:58:31.000.296,=C:\Users\yamat\Documents\Embarcadero\Studio\Projects\Unit1.pas</Transaction> |
22 | 22 | <Transaction>2017/10/29 14:44:00.000.024,=C:\Users\yamat\Documents\Embarcadero\Studio\Projects\Unit1.pas</Transaction> |
23 | - <Transaction>2017/10/29 16:13:37.000.809,=C:\Users\yamat\Documents\Embarcadero\Studio\Projects\ServerContainerUnit1.pas</Transaction> | |
24 | 23 | <Transaction>2017/10/29 16:13:37.000.037,=C:\Users\yamat\Documents\Embarcadero\Studio\Projects\Unit1.pas</Transaction> |
25 | 24 | <Transaction>2017/10/29 16:13:37.000.174,=C:\Users\yamat\Documents\Embarcadero\Studio\Projects\ServerMethodsUnit1.pas</Transaction> |
25 | + <Transaction>2017/10/29 16:13:37.000.809,=C:\Users\yamat\Documents\Embarcadero\Studio\Projects\ServerContainerUnit1.pas</Transaction> | |
26 | 26 | <Transaction>2017/10/29 16:55:16.000.244,=C:\Users\yamat\Documents\Embarcadero\Studio\Projects\ServerMethodsUnit1.pas</Transaction> |
27 | - <Transaction>2017/10/29 16:55:16.000.109,=C:\Users\yamat\Documents\Embarcadero\Studio\Projects\Unit1.pas</Transaction> | |
28 | 27 | <Transaction>2017/10/29 16:55:16.000.343,=C:\Users\yamat\Documents\Embarcadero\Studio\Projects\ServerContainerUnit1.pas</Transaction> |
28 | + <Transaction>2017/10/29 16:55:16.000.109,=C:\Users\yamat\Documents\Embarcadero\Studio\Projects\Unit1.pas</Transaction> | |
29 | 29 | <Transaction>2017/10/30 15:04:38.000.106,=C:\Users\yamat\Documents\Embarcadero\Studio\Projects\WebModuleUnit1.pas</Transaction> |
30 | 30 | <Transaction>2017/11/01 16:00:52.000.956,=C:\Users\yamat\Documents\Embarcadero\Studio\Projects\ServerMethodsUnit1.pas</Transaction> |
31 | 31 | <Transaction>2017/11/01 16:00:53.000.058,=C:\Users\yamat\Documents\Embarcadero\Studio\Projects\ServerContainerUnit1.pas</Transaction> |
@@ -34,23 +34,23 @@ | ||
34 | 34 | <Transaction>2017/11/01 18:12:24.000.771,=C:\Users\yamat\Documents\Embarcadero\Studio\Projects\WebModuleUnit1.pas</Transaction> |
35 | 35 | <Transaction>2017/11/01 18:21:25.000.947,=C:\Users\yamat\Documents\Embarcadero\Studio\Projects\ServerMethodsUnit1.pas</Transaction> |
36 | 36 | <Transaction>2017/11/01 18:21:26.000.254,=C:\Users\yamat\Documents\Embarcadero\Studio\Projects\WebModuleUnit1.pas</Transaction> |
37 | + <Transaction>2017/11/01 18:23:22.000.440,=C:\Users\yamat\Documents\Embarcadero\Studio\Projects\WebModuleUnit1.pas</Transaction> | |
37 | 38 | <Transaction>2017/11/01 18:23:22.000.193,=C:\Users\yamat\Documents\Embarcadero\Studio\Projects\FormUnit1.pas</Transaction> |
38 | 39 | <Transaction>2017/11/01 18:23:22.000.344,=C:\Users\yamat\Documents\Embarcadero\Studio\Projects\ServerMethodsUnit1.pas</Transaction> |
39 | - <Transaction>2017/11/01 18:23:22.000.440,=C:\Users\yamat\Documents\Embarcadero\Studio\Projects\WebModuleUnit1.pas</Transaction> | |
40 | - <Transaction>2017/11/01 18:33:21.000.032,=C:\Users\yamat\Documents\Embarcadero\Studio\Projects\ServerMethodsUnit1.pas</Transaction> | |
41 | 40 | <Transaction>2017/11/01 18:33:21.000.133,=C:\Users\yamat\Documents\Embarcadero\Studio\Projects\ServerContainerUnit1.pas</Transaction> |
41 | + <Transaction>2017/11/01 18:33:21.000.032,=C:\Users\yamat\Documents\Embarcadero\Studio\Projects\ServerMethodsUnit1.pas</Transaction> | |
42 | 42 | <Transaction>2017/11/01 18:33:39.000.795,=C:\Users\yamat\Documents\Embarcadero\Studio\Projects\WebModuleUnit1.pas</Transaction> |
43 | 43 | <Transaction>2017/11/01 19:08:03.000.193,=C:\Users\yamat\Documents\Embarcadero\Studio\Projects\WebModuleUnit1.pas</Transaction> |
44 | 44 | <Transaction>2017/11/30 19:11:41.000.772,=C:\Users\yamat\Documents\Embarcadero\Studio\Projects\Unit1.pas</Transaction> |
45 | 45 | <Transaction>2018/01/01 12:40:34.000.125,=C:\Users\yamat\Documents\Embarcadero\Studio\Projects\Unit1.pas</Transaction> |
46 | 46 | <Transaction>2018/01/01 12:40:55.000.044,=C:\Users\yamat\Documents\Embarcadero\Studio\Projects\Unit1.pas</Transaction> |
47 | 47 | <Transaction>2018/05/07 18:00:45.000.726,=C:\Users\yamat\Documents\Embarcadero\Studio\Projects\Unit1.pas</Transaction> |
48 | - <Transaction>2018/05/07 18:01:10.000.795,C:\Users\yamat\Documents\Embarcadero\Studio\Projects\myapp\wav\wav.pas=C:\Users\yamat\Documents\Embarcadero\Studio\Projects\Unit1.pas</Transaction> | |
49 | - <Transaction>2018/05/07 18:01:39.000.274,C:\Users\yamat\Documents\Embarcadero\Studio\Projects\myapp\wav\wav_proj.dproj=C:\Users\yamat\Documents\Embarcadero\Studio\Projects\Project1.dproj</Transaction> | |
48 | + <Transaction>2018/05/07 18:01:10.000.795,C:\Users\yamat\Documents\Embarcadero\Studio\Projects\Unit1.pas=C:\Users\yamat\Documents\Embarcadero\Studio\Projects\myapp\wav\wav.pas</Transaction> | |
49 | + <Transaction>2018/05/07 18:01:39.000.274,C:\Users\yamat\Documents\Embarcadero\Studio\Projects\Project1.dproj=C:\Users\yamat\Documents\Embarcadero\Studio\Projects\myapp\wav\wav_proj.dproj</Transaction> | |
50 | 50 | <Transaction>2018/05/07 21:20:21.000.796,=C:\Users\yamat\Documents\Embarcadero\Studio\Projects\myapp\wav\Unit1.pas</Transaction> |
51 | - <Transaction>2018/05/07 21:20:27.000.646,C:\Users\yamat\Documents\Embarcadero\Studio\Projects\myapp\wav\Unit1.pas=C:\Users\yamat\Documents\Embarcadero\Studio\Projects\myapp\wav\spWave.pas</Transaction> | |
51 | + <Transaction>2018/05/07 21:20:27.000.646,C:\Users\yamat\Documents\Embarcadero\Studio\Projects\myapp\wav\spWave.pas=C:\Users\yamat\Documents\Embarcadero\Studio\Projects\myapp\wav\Unit1.pas</Transaction> | |
52 | 52 | <Transaction>2018/05/09 21:56:39.000.226,=C:\Users\yamat\Documents\Embarcadero\Studio\Projects\myapp\wav\Unit1.pas</Transaction> |
53 | - <Transaction>2018/05/09 21:56:54.000.027,C:\Users\yamat\Documents\Embarcadero\Studio\Projects\myapp\wav\Unit1.pas=C:\Users\yamat\Documents\Embarcadero\Studio\Projects\myapp\wav\WriteHeader.pas</Transaction> | |
53 | + <Transaction>2018/05/09 21:56:54.000.027,C:\Users\yamat\Documents\Embarcadero\Studio\Projects\myapp\wav\WriteHeader.pas=C:\Users\yamat\Documents\Embarcadero\Studio\Projects\myapp\wav\Unit1.pas</Transaction> | |
54 | 54 | <Transaction>2018/05/13 12:45:57.000.431,=C:\Users\yamat\Documents\Embarcadero\Studio\Projects\myapp\wav\spWav.pas</Transaction> |
55 | 55 | </Transactions> |
56 | 56 | </BorlandProject> |