回答編集履歴
1
エンディアン変換について追記
answer
CHANGED
@@ -1,4 +1,29 @@
|
|
1
1
|
[wave](https://docs.python.jp/3/library/wave.html#module-wave)モジュールの[Wave_write.setparams](https://docs.python.jp/3/library/wave.html#wave.Wave_write.setparams)でヘッダ部分は作ってくれるようなので、あとは[Wave_write.writeframes](https://docs.python.jp/3/library/wave.html#wave.Wave_write.writeframes)で生PCMを書き出せばよいと思います。
|
2
2
|
上述の`setparams`でエンディアン指定できるかは未調査です。
|
3
3
|
|
4
|
-
参考:[Pythonでサイン波(正弦波)をつくる](http://www.non-fiction.jp/2015/08/17/sin_wave/)
|
4
|
+
参考:[Pythonでサイン波(正弦波)をつくる](http://www.non-fiction.jp/2015/08/17/sin_wave/)
|
5
|
+
|
6
|
+
#### 追記
|
7
|
+
|
8
|
+
上記例で正しいWaveファイルが作成できない場合は、生PCMのエンディアンをbig->littleに変換して書き込んでみてはいかがでしょうか?
|
9
|
+
|
10
|
+
以下はbig -> littleにエンディアン変換する例です。
|
11
|
+
```Python
|
12
|
+
# サンプルデータ big
|
13
|
+
from struct import *
|
14
|
+
dat_big = pack( '>4h', 0, 1, 255, 256) # > = big (signed short)
|
15
|
+
print(dat_big) # b'\x00\x00\x00\x01\x00\xff\x01\x00'
|
16
|
+
|
17
|
+
# big -> little
|
18
|
+
import array
|
19
|
+
dat_little = array.array('h',dat_big)
|
20
|
+
dat_little.byteswap() # big -> little
|
21
|
+
print(bytes(dat_little)) # b'\x00\x00\x01\x00\xff\x00\x00\x01'
|
22
|
+
|
23
|
+
# テスト書込
|
24
|
+
with open('dat_big.dat', 'wb') as f:
|
25
|
+
f.write(dat_big)
|
26
|
+
|
27
|
+
with open('dat_little.dat', 'wb') as f:
|
28
|
+
f.write(dat_little)
|
29
|
+
```
|