summaryrefslogtreecommitdiff
path: root/pysoundtouch/tools/Shifter.py
blob: 612fa7ac15dfdbca68e68ba342922d1c42389259 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
from ReadAudio import AudioReader, ConvertReader
import soundtouch, wave, audioop, math
from array import array

class Shifter:
    @staticmethod
    def shift_chunk(chunk, sampling_rate, channels, shift):
        """Shift the pitch of a chunk of audio up or down
        Width must be 2."""
        st = soundtouch.SoundTouch(sampling_rate, channels)
        st.set_pitch_shift(shift)

        ii = 0
        resstr = ""
        while ii + 4608 < len(chunk):
            st.put_samples(chunk[ii:ii+4608].tostring())
            ii += 4608
            while st.ready_count() > 0:
                resstr += st.get_samples(4608)

        st.put_samples(chunk[ii:])
        while st.ready_count() > 0:
            resstr += st.get_samples(11025)

        resstr += Shifter.get_flush(st, channels, len(chunk) - len(resstr) / 2)

        del st
            
        result = array(chunk.typecode)
        result.fromstring(resstr)

        return result

    @staticmethod
    def many_shift_chunk(chunk, sampling_rate, channels, shifts):
        """Produce harmonies by shifting a chunk of audio more than once and combining them."""
        shifteds = []
        maxlen = 0
        for jj in xrange(len(shifts)):
            if not shifts[jj]:
                shifted = chunk
            else:
                shifted = Shifter.shift_chunk(chunk, sampling_rate, channels, shifts[jj])

            shifteds.append(shifted)
            maxlen = max(maxlen, len(shifted))

        if len(shifteds) > 1:
            newchunk = [0] * maxlen
            for ii in xrange(maxlen):
                count = 0
                for jj in xrange(len(shifteds)):
                    if len(shifteds[jj]) > ii:
                        newchunk[ii] += shifteds[jj][ii]
                        count += 1
                newchunk[ii] /= count

            result = array(chunk.typecode)
            result.fromlist(newchunk)
            return result
        else:
            return shifteds[0]

    @staticmethod
    def raw_shift_reader(srcpath, dstpath, shift):
        """Shift an entire file up or down"""
        # Open the file and convert it to have SoundTouch's required 2-byte samples
        reader = AudioReader.open(srcpath)
        reader2 = ConvertReader(reader, set_raw_width=2)

        # Create the SoundTouch object and set the given shift
        st = soundtouch.SoundTouch(reader2.sampling_rate(), reader2.channels())
        st.set_pitch_shift(shift)

        # Create the .WAV file to write the result to
        writer = wave.open(dstpath, 'w')
        writer.setnchannels(reader2.channels())
        writer.setframerate(reader2.sampling_rate())
        writer.setsampwidth(reader2.raw_width())

        # Read values and feed them into SoundTouch
        while True:
            data = reader2.raw_read()
            if not data:
                break

            print len(data)
            st.put_samples(data)

            while st.ready_count() > 0:
                writer.writeframes(st.get_samples(11025))

        # Flush any remaining values
        writer.writeframes(Shifter.get_flush(st, reader2.channels()))

        # Clean up
        writer.close()
        reader2.close()

    @staticmethod
    def get_flush(st, channels, fade=0):
        """Like soundtouch's flush, don't require that all data comes through, just any.
        If fade > 0, only allow [fade] samples, and linearly scale volume to 0 over that length"""
        
        waiting = st.waiting_count()
        ready = st.ready_count()
        result = ""

        silence = array('h', [0] * 64)

        while st.ready_count() == ready:
            st.put_samples(silence)

        while st.ready_count() > 0:
            result += st.get_samples(11025)

        st.clear()

        if len(result) > 2 * channels * waiting:
            result = result[0:(2 * channels * waiting)]

        fade = min(fade, len(result) / 2)
        if fade > 0:
            resultstring = ""
            for ii in xrange(fade / channels):
                i0 = ii * 2*channels
                i1 = (ii+1) * 2*channels
                resultstring += audioop.mul(result[i0:i1], 2, 1 - float(ii) / (fade / channels))
            result = resultstring

        return result
    
    @staticmethod
    def bpm_detect_file(fullpath):
        """Detect the beat from an entire file"""
        reader = AudioReader.open(fullpath)
        reader2 = ConvertReader(reader, set_raw_width=2)

        bd = soundtouch.BPMDetect(reader2.sampling_rate(), reader2.channels())

        while True:
            data = reader2.raw_read()
            if not data:
                break

            bd.put_samples(data)

        reader2.close()

        return bd.get_bpm()

    @staticmethod
    def echocancel(outputdata, inputdata):
        """Try to identify an echo and remove it.
        Should contain 2-byte samples"""
        pos = audioop.findmax(outputdata, 800)
        out_test = outputdata[pos*2:]
        in_test = inputdata[pos*2:]
        ipos, factor = audioop.findfit(in_test, out_test)
        factor = audioop.findfactor(in_test[ipos*2:ipos*2+len(out_test)], out_test)
        prefill = '\0'*(pos+ipos)*2
        postfill = '\0'*(len(inputdata) - len(prefill) - len(outputdata))
        outputdata = prefill + audioop.mul(outputdata, 2, -factor) + postfill
        return audioop.add(inputdata, outputdata, 2)

    @staticmethod
    def beats_to_ms(bpm, beats):
        """Convert from bpm at a given beat rate to ms between beats."""
        return 60 * 1000 * beats / bpm

    @staticmethod
    def find_division_start(fullpath, bpm, beats_per):
        """Identify the start of the beats, by finding segments that fit together"""
        reader = AudioReader.open(fullpath)

        # This doesn't find the exact time of the max, but don't need it.
        max_value = 0
        max_time = 0
        while True:
            data = reader.raw_read()
            if data is None:
                break

            data_max = audioop.max(data, reader.raw_width())
            if data_max > max_value:
                max_value = data_max
                max_time = reader.current_time()

        before = max_time - Shifter.beats_to_ms(bpm, beats_per)
        after = max_time + 2 * Shifter.beats_to_ms(bpm, beats_per)
        if before < 0:
            after += -before
            before = 0
        if after > reader.duration():
            before -= after - reader.duration()
            after = reader.duration()
        if before < 0:
            if beats_per < 2:
                raise RuntimeError('This audio file is too short to be divided by beats.')
            else:
                reader.close()
                return Shifting.find_division_start(filepath, bpm, int(beats_per / 2))

        reader.seek_time(0)
        reader2 = ConvertReader(reader, set_raw_width=2, set_channels=1)
        region = reader2.raw_random_read(before, after)

        # both in bytes
        raw_length = 2 * int(len(region) / 6)
        beat_length = int(2 * Shifter.beats_to_ms(bpm, 1) * reader2.sampling_rate() / 1000.0)

        print "Around max: " + str(before) + " - " + str(after) + ": " + str(raw_length)

        min_factor = 0
        min_ii = 0
        # First determine time within a beat
        for ii in xrange(beat_length / 200):
            factor = audioop.findfactor(region[200*ii:200*ii+raw_length], region[200*ii+raw_length:200*ii+2*raw_length])
            if factor < min_factor:
                print "Samp: At " + str(ii) + " " + str(factor)
                min_factor = factor
                min_ii = ii

        # Second, determine which beat to use
        min_factor = 0
        min_jj = 0
        for jj in xrange(beats_per):
            factor = audioop.findfactor(region[jj*beat_length+200*min_ii:jj*beat_length+200*min_ii+raw_length], region[jj*beat_length+200*min_ii+raw_length:jj*beat_length+200*min_ii+2*raw_length])
            print "Beat: At " + str(jj) + " " + str(factor)
            if factor < min_factor:
                min_factor = factor
                min_jj = jj

        print "Best: Beat: " + str(min_jj) + ", Samp: " + str(100*min_ii)
        start_time = before + (min_jj*beat_length*2 + 100*min_ii) * 1000.0 / reader2.sampling_rate()
        reader2.close()

        return math.fmod(start_time, Shifter.beats_to_ms(bpm, beats_per))