1from __future__ import print_function
2import os
3import sys
4import numpy as np
5
6from pysndfile_inst_dir.pysndfile import get_sndfile_version
7from pysndfile_inst_dir.pysndfile import *
8import pysndfile_inst_dir.pysndfile as pysndfile
9
10
11mydir = os.path.dirname(__file__)
12
13print("pysndfile version:",get_pysndfile_version())
14print("libsndfile version:",get_sndfile_version())
15
16majors = get_sndfile_formats()
17print( "majors", majors)
18for mm in majors:
19    if mm in fileformat_name_to_id:
20        print("format {0:x}".format(fileformat_name_to_id[mm]), "->", mm)
21    else:
22        print("format {0}".format(mm), "-> not supported by pysndfile")
23
24print( get_sndfile_encodings('wav'))
25
26try:
27    a = PySndfile(os.path.join(mydir,'test1.wav'))
28except IOError as e:
29    print("++++++++++++++++++++++++++++++++++++++++++++++++++++++++")
30    print(e)
31    print("++++++++++++++++++++++++++++++++++++++++++++++++++++++++")
32
33a = PySndfile(os.path.join(mydir,'test.wav'))
34for d in [np.float64, np.float32, np.int32, np.short]:
35    print("d:",d)
36    ff=a.read_frames(dtype=d)
37    a.rewind()
38    b = PySndfile(os.path.join(mydir,'test{0}.wav'.format(str(d).split("'")[1])), "w", a.format(), a.channels(), a.samplerate())
39    print(b)
40    b.write_frames(ff)
41    b.close()
42    del b
43
44ff=a.read_frames(dtype=np.float64)
45ff2 = np.concatenate(((ff,),(ff,))).T
46
47print("ff2.shape    ",ff2.shape)
48b = PySndfile(os.path.join(mydir,'test_2cC.wav'), "w", a.format(), 2, a.samplerate())
49b.write_frames(np.require(ff2, requirements='C'))
50
51b = PySndfile(os.path.join(mydir,'test_2cF.wav'), "w", a.format(), 2, a.samplerate())
52b.write_frames(np.require(ff2, requirements='F'))
53del b
54
55b= PySndfile(os.path.join(mydir,'test_2cF.wav'))
56bff=b.read_frames()
57with PySndfile(os.path.join(mydir,'test_2cC.wav')) as b:
58    bfc=b.read_frames()
59
60read_error= False
61write_error =False
62if np.any (ff2 != bff):
63    print('error in test_2cF.wav')
64    print("ff2", ff2)
65    print("bff", bff)
66    write_error = True
67elif np.any (ff2 != bfc):
68    print('error in test_2cC.wav')
69    print("ff2", ff2)
70    print("bfc", bfc)
71    write_error = True
72else:
73    print("no errors detected for io with different sample encodings")
74
75# check reading part of file
76ss,_,_ =  pysndfile.sndio.read(os.path.join(mydir,'test.wav'), force_2d=True)
77ssstart,_,_ =  pysndfile.sndio.read(os.path.join(mydir,'test.wav'), end=100, force_2d=True)
78ssend,_,_ =  pysndfile.sndio.read(os.path.join(mydir,'test.wav'), start=100, force_2d=True)
79
80if np.any(ss != np.concatenate((ssstart, ssend), axis=0)):
81    read_error = True
82    print("error reading file segments with sndio")
83
84ww = PySndfile(os.path.join(mydir,'test.wav'))
85wwstart = ww.read_frames(100, force_2d=True)
86wwend = ww.read_frames(force_2d=True)
87
88if np.any(ss != np.concatenate((wwstart, wwend), axis=0)):
89    read_error = True
90    print("error reading file segments with class")
91
92# check writing flac
93if "flac" in majors:
94    print('test writing flac')
95    ss, sr, enc = pysndfile.sndio.read(os.path.join(mydir,'test.wav'), force_2d=True)
96    flac_file = PySndfile(os.path.join(mydir,'test.flac'), "w", construct_format("flac", "pcm16"), ss.shape[1], sr)
97    flac_file.command("SFC_SET_COMPRESSION_LEVEL", 1.)
98    flac_file.write_frames(ss)
99    flac_file.close()
100
101    ss_flac, sr_flac, enc_flac = pysndfile.sndio.read(os.path.join(mydir,'test.flac'), force_2d=True)
102    if sr != sr_flac:
103        print('error::flac writing sample rate')
104        write_error = True
105    if enc != enc_flac:
106        print('error::flac writing enc')
107        write_error = True
108    if np.any (ss != ss_flac):
109        print('error in test_2cF.wav')
110        write_error = True
111else:
112    print('your libsndfile version does not support flac format, skip flac writing test')
113
114if write_error or read_error:
115    if write_error:
116        print("write errors encountered")
117    if read_error:
118        print("read errors encountered")
119    sys.exit(1)
120else:
121    print("all seems ok")
122    sys.exit(0)
123