library(sound) #---------------------------------------------------------------------- mysplit = function(d,nb.el.per.seg) { return(split(d, ceiling(seq_along(d)/nb.el.per.seg))) } #---------------------------------------------------------------------- next.T = function (splits, start) { for (i in (start:length(splits))) { if (splits[i] == T) { return(i) } } return(NA) # no more trues } #---------------------------------------------------------------------- # once the function is tested, create a similar function for next.F (next false) next.F = function (splits, start) { for (i in (start:length(splits))) { if (splits[i] == F) { return(i) } } return(NA) # no more falses } #---------------------------------------------------------------------- create.word = function(split.signal, start, end) { return(unlist(split.signal[start:end])) } #---------------------------------------------------------------------- next.word = function(splits, above.silence, start) { signal.start = next.T(above.silence, start) if (is.na(signal.start)) { return(list(c(0), NA)) } signal.end = next.F(above.silence, signal.start) if (is.na(signal.end)) { return(list(c(0), NA)) } word = create.word(splits, signal.start, signal.end-1) cat("signal start/end ", signal.start, signal.end, '\n') return(list(word, signal.end)) # signal.end is next false } #---------------------------------------------------------------------- breakUpSoundWave = function(file, thresh, samples.per.segment) { snd=loadSample(file) snd = noSilence(snd,.0001) wave = snd$sound[1,] wave.abs = abs(wave) # if vol > thresh ,then "psychology" # if vol < thresh, then silence vol = wave.abs > thresh # split the wave into pieces of 1000 samples each # 1000 samples is about 0.05 seconds (1/20 sec). splits = mysplit(wave.abs, samples.per.segment) # calculate the mean of every sample means = lapply(splits, mean) plot(1:length(means), means) above.silence = means > thresh # print(above.silence) # redo the split to make sure that the words saved are not based on absolute value splits = mysplit(wave, samples.per.segment) #------------------------------- sig.start = 1 words = list() how.many.words = 0 while (T) { ret = next.word(splits, above.silence, sig.start) word = ret[[1]] end = ret[[2]] sig.start = end if (is.na(end)) { break # exit loop } how.many.words = how.many.words + 1 words[[how.many.words]] = word } return(words) } print("****") #---------------------------------------------------------------------- #thresh = .001 #file = "psychology.wav" #file = "assignment_sentence_instructor.wav" #samples.per.seg = 3000 #words = breakUpSoundWave(file, thresh, samples.per.seg) #----------------------------------------------------------------------