<commit_before>#!/usr/bin/env python
'''
CREATED:2013-01-23 09:26:25 by <NAME> <<EMAIL>>

Utility functions for analysis output, eg:

    - sonic visualizer output for clustering/beat tracking
    - ???

'''

import librosa
import numpy, scipy
import csv

def segment_csv(outfile, segments, sr, hop_length):
    '''
    Save beat tracker or segmentation output in CSV format

    Input:
        outfile:            path to the output file
        segments:           1-by-n list of frame numbers for beat events
        sr:                 sample rate of the beat detector (eg 8000)
        hop_length:         hop length of the beat detector (32)
    '''

    with open(outfile, 'w') as f:
        CW = csv.writer(f)

        t = 0.0
        for t_new in librosa.frames_to_time(segments, sr=sr, hop_length=hop_length):
            CW.writerow([t_new, '%.2f BPM' % (60.0 / (t_new - t))])
            t = t_new
            pass
        pass
    pass<commit_msg>added a wav writer to librosa.output<commit_after>#!/usr/bin/env python
'''
CREATED:2013-01-23 09:26:25 by <NAME> <<EMAIL>>

Utility functions for analysis output, eg:

    - sonic visualizer output for clustering/beat tracking
    - ???

'''

import librosa
import numpy, scipy
import scipy.io.wavfile
import csv

def segment_csv(outfile, segments, sr, hop_length):
    '''
    Save beat tracker or segmentation output in CSV format

    Input:
        outfile:            path to the output file
        segments:           1-by-n list of frame numbers for beat events
        sr:                 sample rate of the beat detector (eg 8000)
        hop_length:         hop length of the beat detector (32)
    '''

    with open(outfile, 'w') as f:
        CW = csv.writer(f)

        t = 0.0
        for t_new in librosa.frames_to_time(segments, sr=sr, hop_length=hop_length):
            CW.writerow([t_new, '%.2f BPM' % (60.0 / (t_new - t))])
            t = t_new
            pass
        pass
    pass


def write_wav(y, sr, filename):
    '''
    Output a time series as a .wav file

    Input:
        y:          time-series audio data (floating point) (mono)
        sr:         sample rate of y
        filename:   path to save the wav file. 

    '''

    w = y / numpy.max(numpy.abs(y))
    scipy.io.wavfile.write(filename, sr, (w * 32768.0).astype('<i2'))
    pass
{'fraction_non_alphanumeric': 0.10350727117194183, 'fraction_numerical': 0.026090675791274595, 'mean_word_length': 2.0297927461139897, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5585528', 'n_tokens_mistral': 822, 'n_tokens_neox': 746, 'n_words': 244}