回答編集履歴

1

エンディアン変換について追記

2017/10/20 01:03

投稿

can110
can110

スコア38266

test CHANGED
@@ -5,3 +5,53 @@
5
5
 
6
6
 
7
7
  参考:[Pythonでサイン波(正弦波)をつくる](http://www.non-fiction.jp/2015/08/17/sin_wave/)
8
+
9
+
10
+
11
+ #### 追記
12
+
13
+
14
+
15
+ 上記例で正しいWaveファイルが作成できない場合は、生PCMのエンディアンをbig->littleに変換して書き込んでみてはいかがでしょうか?
16
+
17
+
18
+
19
+ 以下はbig -> littleにエンディアン変換する例です。
20
+
21
+ ```Python
22
+
23
+ # サンプルデータ big
24
+
25
+ from struct import *
26
+
27
+ dat_big = pack( '>4h', 0, 1, 255, 256) # > = big (signed short)
28
+
29
+ print(dat_big) # b'\x00\x00\x00\x01\x00\xff\x01\x00'
30
+
31
+
32
+
33
+ # big -> little
34
+
35
+ import array
36
+
37
+ dat_little = array.array('h',dat_big)
38
+
39
+ dat_little.byteswap() # big -> little
40
+
41
+ print(bytes(dat_little)) # b'\x00\x00\x01\x00\xff\x00\x00\x01'
42
+
43
+
44
+
45
+ # テスト書込
46
+
47
+ with open('dat_big.dat', 'wb') as f:
48
+
49
+ f.write(dat_big)
50
+
51
+
52
+
53
+ with open('dat_little.dat', 'wb') as f:
54
+
55
+ f.write(dat_little)
56
+
57
+ ```