0
#!/usr/bin/env python

import numpy
import os
import struct
import sys

class STF:
    def __init__(self):
        self.endian = '>'
        self.chunks = ['APSG']

    def loadfile(self, filename):
        with open(RESULT20191219.txt, 'rb') as stf_file:
            self.load(stf_file)

    def load(self, stf_file):
        filesize = os.fstat(stf_file.fileno()).st_size

        while stf_file.tell() < filesize:
            chunk = stf_file.read(4)

            if chunk == 'STRT':
                if stf_file.read(2) == '\xff\xfe':
                    self.endian = '<'
                chunk_size, self.version, self.channel, self.frequency = struct.unpack(self.endian + 'IHHI', stf_file.read(12))
            else:
                chunk_size, = struct.unpack(self.endian + 'I', stf_file.read(4))

                if chunk == 'CHKL' or chunk == 'NXFL':
                    data = stf_file.read(chunk_size)
                    if chunk == 'CHKL':
                        self.chunks += [data[i: i + 4] for i in range(0, chunk_size, 4) if data[i: i + 4] not in self.chunks]
                else:
                    self.shift_length, frame_count, argument, self.bit_size, self.weight, data_size = struct.unpack(self.endian + 'dIIHdI', stf_file.read(30))
                    data = stf_file.read(data_size)

                    element = data_size / (self.bit_size / 8)
                    matrix = numpy.fromstring(data, count = element)

                    for c in self.chunks:
                        if chunk == c:
                            if element / frame_count == 1:
                                self.__dict__[c.strip()] = matrix
                            else:
                                self.__dict__[c.strip()] = matrix.reshape((frame_count, element / frame_count))
                            break

        for c in self.chunks:
            if c.strip() not in self.__dict__:
                self.__dict__[c.strip()] = None

    def savefile(self, filename):
        with open(filename, 'wb') as stf_file:
            self.save(stf_file)

    def save(self, stf_file):
        stf_file.write('STRT')
        if self.endian == '>':
            stf_file.write('\xfe\xff')
        elif self.endian == '<':
            stf_file.write('\xff\xfe')
        stf_file.write(struct.pack(self.endian + 'IHHI', 8, self.version, self.channel, self.frequency))

        stf_file.write('CHKL')
        stf_file.write(struct.pack(self.endian + 'I', len(''.join(self.chunks))) + ''.join(self.chunks))

        for c in self.chunks:
            if self.__dict__[c.strip()] is None:
                continue

            matrix = self.__dict__[c.strip()]
            if len(matrix.shape) == 1:
                argument = 1
            else:
                argument = matrix.shape[1]
            data_size = matrix.shape[0] * argument * 8

            header = struct.pack(self.endian + 'dIIHdI', self.shift_length, matrix.shape[0], argument, self.bit_size, self.weight, data_size)
            stf_file.write(c + struct.pack(self.endian + 'I', len(header) + data_size) + header)

            for i in xrange(matrix.shape[0]):
                if argument == 1:
                    stf_file.write(struct.pack(self.endian + 'd', matrix[i]))
                else:
                    for j in xrange(matrix.shape[1]):
                        stf_file.write(struct.pack(self.endian + 'd', matrix[i, j]))

if __name__ == '__main__':
    if len(sys.argv) < 2:
        print 'Usage: %s <stf_file>' % sys.argv[0]
        sys.exit()

    stf = STF(sys.argv[1])
    print stf.F0
TypeError                                 Traceback (most recent call last)
<ipython-input-5-5827b493a099> in <module>()
      4         sys.exit()
      5 
----> 6     stf = STF(sys.argv[1])
      7     print stf.F0

TypeError: __init__() takes exactly 1 argument (2 given)

実行はjupyter notebookです。pycharmで実行するとエラーは出なかったです

3
  • Jupyter や PyCharm でそれぞれどのように実行されましたか? おそらく PyCharm の方では単に print だけされて終わっているのではないかと思うのですが……。手順を質問文に追記していただけませんでしょうか。質問文下の「編集」から自由に追記できます :)
    – nekketsuuu
    2020年1月7日 9:30
  • 何か元記事のプログラムを(意図してか否かは不明ですが)変更して動作出来なくなっているのでしょう。第3章 声質変換のための様々なモジュールの実装 「リスト3.1: stf.py」
    – kunif
    2020年1月7日 9:44
  • pycharmの方は解決いたしました
    – kkk
    2020年1月7日 11:17

1 件の回答 1

3

クラス STF のインスタンスを作るにあたって STF(sys.argv[1]) と書くと、内部処理の途中で STF__init__ メソッドが __init__(self, sys.argv[1]) と呼び出されます。ここで引数は 2 つ与えられていますが、STF__init__ は引数が 1 つであることを期待しているので今回のエラーが出ています。

def __init__(self):
    self.endian = '>'
    self.chunks = ['APSG']

この質問に回答するには、ログインする必要があります。

求めていた回答ではありませんか? のタグが付いた他の質問を参照する。