Why am I unable to produce sound with this code?
    조회 수: 15 (최근 30일)
  
       이전 댓글 표시
    
I'm working on a project where I'm taking the intensities of an image and mapping them to produce frequencies and return an array of notes that will be used for a "song", but I'm unable to actually hear the pitches and frequencies of my song. I've gone through finding the intensities of my image correctly, and I format it into a histogram so as to show the "score" of the music. I'm getting a noise that sounds like a click for every "note" that is supposed to be heard, but there is no audible frequency when I try to listen to it. I don't know if it's a problem with the range I've chosen for the audible human range, but I'd really like some help if anyone is able to take a look at this! I have this main function and a custom function "mus306NoteFinder.m", so please let me know if there's anything else I should upload to help understand the issue better!
% MAIN FUNCTION WHERE MY CODE IS BEING RUN
audibleMin = 100; % changing min frequency from min frequency for humans (20)
audibleMax = 2500; % changing max frequency from max frequency for humans (200)
% creating matrix for all the notes
notes = histMtx(:,2);
songLength = length(notes);
% making sure all notes are within audible range
for i = 1:songLength
    audibleNote = notes(i);
    if ((audibleNote / audibleMin) < 1)
        audibleNote = audibleNote * 10;
    elseif ((audibleNote / audibleMax) > 1)
        audibleNote = audibleNote / 10;
    end
    notes(i) = audibleNote;
end
for i = 1:songLength
    currentNote = notes(i);
    % using notefinder from previous project to generate note??
    singleNote = mus306NoteFinder(currentNote);
    finalNotes(i) = singleNote;
end
fs = 44100;
time = 1;
values = 0:1/fs:time;
for i = 1:songLength
    noteSound = sin(2*pi*finalNotes(i)*values);
    sound(finalNotes(i), fs)
    % pause(1)
end
% CUSTOM FUNCTION TO MATCH THE FREQUENCY TO A PROPER PITCH
function [noteFreq] = mus306NoteFinder(f0)
% NoteFinder custom function for MUS306 project (taken from previous project)
%   Input: f0 - frequency to take in from intensity of image
%   Output: noteFreq - frequency to output for playable tone as music
m = floor(log2(f0/16.3516)); % octave
n = 12*(log2(f0/16.3615) - m) + 1; % scale degree
nRound = round(n);
noteFreq = 16.3516 * 2^(m + ((nRound - 1) / 12));
end
채택된 답변
  Walter Roberson
      
      
 2023년 11월 26일
        
      이동: Voss
      
      
 2023년 11월 26일
  
      for i = 1:songLength
    noteSound = sin(2*pi*finalNotes(i)*values);
    sound(finalNotes(i), fs)
    % pause(1)
end
You calculate nodeSound, but you ignore the value. Instead you sound() one single entry from finalNotes
추가 답변 (0개)
참고 항목
카테고리
				Help Center 및 File Exchange에서 Audio I/O and Waveform Generation에 대해 자세히 알아보기
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

