<commit_before>require 'spnet'
require 'wavefile'

module SPBlocks
class WavOutBlock < SPNet::Block

  include Hashmake::HashMakeable
  
  HASHED_ARG_SPECS = [
    Hashmake::ArgSpec.new(:reqd => true, :key => :sample_rate, :type => Float, :validator => ->(a){ (a > 0.0) && (a.to_i == a)} ),
    Hashmake::ArgSpec.new(:reqd => true, :key => :file_name, :type => String, :validator => ->(a){ !a.empty?() } ),
  ]
  
  attr_reader :file_name

  BITS_PER_SAMPLE = 32
  MAX_SAMPLE_VALUE = (2 **(0.size * 8 - 2) - 2)

  FILE_COMMANDS = [ :open, :close ]
  
  def initialize hashed_args = {}
    hash_make HASHED_ARG_SPECS, hashed_args

    @format = WaveFile::Format.new(:mono, 32, @sample_rate.to_i)
    @writer = WaveFile::Writer.new(@file_name, @format)

    input = SPNet::SignalInPort.new(:name => "INPUT", :limits => (-1.0...1.0))
    
    exec_command_handler = lambda do |command,data|
      case command
      when :close
        @writer.close
      when :open
        unless @writer.closed?
          @writer.close
        end
        @file_name = data
        @writer = WaveFile::Writer.new(@file_name, @format)
      end
    end
    
    file = SPNet::CommandInPort.new(
      :name => "FILE",
      :list_commands_handler => lambda { FILE_COMMANDS },
      :exec_command_handler => exec_command_handler
    )
    
    algorithm = lambda do |count|
      values = input.dequeue_values count
      unless @writer.closed?
        int_values = values.map { |value| (value * MAX_SAMPLE_VALUE).to_i }
        buffer = WaveFile::Buffer.new(int_values, @format)
        @writer.write(buffer)
      end
    end<commit_msg>Update to use spnet gem v0.1.4, so use a command map instead of a list and exec handler on any CommandInPort.<commit_after>require 'spnet'
require 'wavefile'

module SPBlocks
class WavOutBlock < SPNet::Block

  include Hashmake::HashMakeable
  
  HASHED_ARG_SPECS = [
    Hashmake::ArgSpec.new(:reqd => true, :key => :sample_rate, :type => Float, :validator => ->(a){ (a > 0.0) && (a.to_i == a)} ),
    Hashmake::ArgSpec.new(:reqd => true, :key => :file_name, :type => String, :validator => ->(a){ !a.empty?() } ),
  ]
  
  attr_reader :file_name

  BITS_PER_SAMPLE = 32
  MAX_SAMPLE_VALUE = (2 **(0.size * 8 - 2) - 2)

  FILE_COMMANDS = [ :open, :close ]
  
  def initialize hashed_args = {}
    hash_make HASHED_ARG_SPECS, hashed_args

    @format = WaveFile::Format.new(:mono, 32, @sample_rate.to_i)
    @writer = WaveFile::Writer.new(@file_name, @format)

    input = SPNet::SignalInPort.new(:name => "INPUT", :limits => (-1.0...1.0))

    file = SPNet::CommandInPort.new(
      :name => "FILE",
      :command_map => {
        :open => lambda do |data|
          unless @writer.closed?
            @writer.close
          end
          @file_name = data
          @writer = WaveFile::Writer.new(@file_name, @format)          
        end,
        :close => lambda {|data| @writer.close }
      }
    )
    
    algorithm = lambda do |count|
      values = input.dequeue_values count
      unless @writer.closed?
        int_values = values.map { |value| (value * MAX_SAMPLE_VALUE).to_i }
        buffer = WaveFile::Buffer.new(int_values, @format)
        @writer.write(buffer)
      end
    end
{'fraction_non_alphanumeric': 0.14569742155949053, 'fraction_numerical': 0.010251630941286114, 'mean_word_length': 2.752913752913753, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 36, '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': '2747378', 'n_tokens_mistral': 1139, 'n_tokens_neox': 1108, 'n_words': 241}